使用Jmeter测试thrift接口

术语描述

jmeter:一款性能压力测试工具,支持多种协议,java 、http 等,但是不支持thrift

thrift:跨语言的RPC调用框架,提供编译器,可以将thrift接口生成不同语言的接口服务类

此处以一个Jmeter测试thrift接口的为案例记录下如何做!

案例描述:

一个thrift接口 ,入参String, 返回结果为入参String ,接口逻辑,打印入参内容。

操作步骤概述:

1、准备thrift接口文件HelloWorld.thrift

2、使用thrift.exe  将HelloWorld.thrift 编译生成 java 文件

3、创建maven项目,添加依赖包,添加步骤2生成的java文件

4、创建接口实现类

5、创建thrift server

6、创建thrift client

7、创建Jmeter Java测试类

8、运行thrift server

9、运行thrift client测试

10、打包放到jmeter\lib\ext下面

11、运行Jmeter 创建java测试请求

详细操作步骤:

1、创建thrift接口文件(文件扩展名是“.thrift”)HelloWorld.thrift

namespace java com.joker.thrift   // 定义生成java类文件的包名字,以及语言种类 
  
//定义入参  
struct Request {    
    1: optional string msg;                 
}  

//定义返回参数
struct Response {    
    1: optional string msg;                 
}

//定义异常  
exception OutOfLengthException {  
    1: optional string reason;  
}  
  
// 定义接口名称  
service HelloWorldService {  
    Response say(1: Request request) throws (1:OutOfLengthException outOfLengthException); // 可能抛出异常。  
} 

2、使用thrift.exe  将HelloWorld.thrift 编译生成 java 文件,执行完下面命令会产生java文件 (https://mirror.bit.edu.cn/apache/thrift/0.13.0/thrift-0.13.0.exe)

D:\thrift> .\thrift-0.13.0.exe -r -gen java .\HelloWorld.thrift   // 这是在win下面的命令行执行的执行的,不要复制粘贴就执行,看自己的 thrift-0.13.0.exe 和 HelloWorld.thrift 文件的位置  

3、创建maven项目,添加依赖包,添加步骤2生成的java文件 (不会的请百度)

最终的maven工程的目录结构:(lib包是要自己新建一个lib文件夹,然后将jmeter安装目录下面的lib/ext/拷贝到lib下面来)

 

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.hhl</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>demo</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
	<!-- junit测试依赖,可以不要,创建maven项目自带 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		
		<!-- thrift依赖 -->
		<dependency>
			<groupId>org.apache.thrift</groupId>
			<artifactId>libthrift</artifactId>
			<version>0.13.0</version>
		</dependency>

        <!-- 将lib文件夹下面的包添加依赖 -->
		<dependency>
          <groupId>org.apache</groupId>
          <artifactId>jmeter-java</artifactId>
          <version>1.0</version>
          <scope>system</scope>
          <systemPath>${basedir}/lib/ApacheJMeter_java.jar</systemPath>
        </dependency>
        
         <!-- 将lib文件夹下面的包添加依赖 -->
        <dependency>
          <groupId>org.apache</groupId>
          <artifactId>jmeter-core</artifactId>
          <version>1.0</version>
          <scope>system</scope>
          <systemPath>${basedir}/lib/ApacheJMeter_core.jar</systemPath>
        </dependency>
        
	</dependencies>

	<build>
		<plugins>
			<!-- 指定jdk -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

4、创建接口实现类HelloWorldServiceImpl.java

package com.joker.thrift.impl;

import org.apache.thrift.TException;

import com.joker.thrift.HelloWorldService;
import com.joker.thrift.OutOfLengthException;
import com.joker.thrift.Request;
import com.joker.thrift.Response;

public class HelloWorldServiceImpl implements HelloWorldService.Iface{

	@Override
	public Response say(Request request) throws OutOfLengthException, TException {
		
		Response response = new Response();
		response.setMsg(request.getMsg());
		System.out.print("HelloWorldServiceImpl.say = "+request.getMsg());
		if(request.getMsg().length() > 20) {
			throw new OutOfLengthException();
		}
		
		return response;
	}

}

5、创建thrift server

package com.joker.thrift.server;

import java.net.ServerSocket;

import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;

import com.joker.thrift.HelloWorldService;
import com.joker.thrift.HelloWorldService.Processor;
import com.joker.thrift.impl.HelloWorldServiceImpl;

public class HelloWorldServer {
	public static void main(String[] args) throws Exception{

		  // Transport
        ServerSocket socket = new ServerSocket(8888);
        TServerSocket serverTransport = new TServerSocket(socket);

        // Processor
        HelloWorldService.Processor processor = new Processor(new HelloWorldServiceImpl());

        TServer.Args tServerArgs = new TServer.Args(serverTransport);
        tServerArgs.processor(processor);

        // Server
        TServer server = new TSimpleServer(tServerArgs);
        System.out.println("Starting the simple server...");
        server.serve();
		
	}
} 

6、创建thrift client

package com.joker.thrift.client;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import com.joker.thrift.HelloWorldService;
import com.joker.thrift.Request;
import com.joker.thrift.Response;

public class HelloWorldClient {
	public static void main(String[] args) {
		TTransport transport = null;
		try {
			// 创建TTransport
			transport = new TSocket("localhost", 8888);

			// 创建TProtocol 协议要与服务端一致
			TProtocol protocol = new TBinaryProtocol(transport);

			// 创建client
			HelloWorldService.Client client = new HelloWorldService.Client(protocol);

			transport.open(); // 建立连接

			Request request = new Request();  // 构造请求参数
			request.setMsg("Hello world!!");

			Response response = client.say(request);  // client调用server端方法
			
			System.out.println("result = "+response.msg);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			transport.close(); // 请求结束,断开连接

		}
	}
}

7、创建Jmeter Java测试类

package com.joker.thrift.jmeter;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

import com.joker.thrift.HelloWorldService;
import com.joker.thrift.Request;
import com.joker.thrift.Response;

public class HelloWorldServiceTest extends AbstractJavaSamplerClient{

	 private TTransport transport;
	 private HelloWorldService.Client helloWorldService;
	 private TBinaryProtocol binaryProtcol;
	
	@Override
	public SampleResult runTest(JavaSamplerContext context) {  // 需要关注的是这一块
		System.out.println(" runTest start");
		Request request = new Request();
		request.setMsg(context.getParameter("msg"));		 
		
		SampleResult result = new SampleResult();  // 记录响应结果
		result.sampleStart(); // 开始记录
		try {
			Response response = helloWorldService.say(request);  // 调用thrift生成的 service类中的方法
			result.setResponseData(response.getMsg().getBytes());
			result.setSuccessful(true);  // 设置成功
		} catch (TException e) {
			result.setSuccessful(false);  // 设置失败
			e.printStackTrace();
		}
		result.sampleEnd();  // 记录结束 用于统计耗时的
		return result;
	}

	@Override
	public Arguments getDefaultParameters() {  // 这个数获取默认参数的,应该是可以定制化的,在jmeter上面配置
		System.out.println(" getDefaultParameters start");
		Arguments args = new Arguments();
		args.addArgument("msg","LoveHHL");
		return args;
	}

	@Override
	public void setupTest(JavaSamplerContext context) { // 创建请求
		
		super.setupTest(context);		
		System.out.println(" setupTest start");
		transport =  new TSocket("localhost",8888);
		binaryProtcol = new TBinaryProtocol(transport);
		helloWorldService = new HelloWorldService.Client(binaryProtcol);
		
		try {
			transport.open();
		} catch (TTransportException e) {
			transport.close();
			e.printStackTrace();
		}
		
	}

	@Override
	public void teardownTest(JavaSamplerContext context) {  // 关闭资源
		super.teardownTest(context);
		System.out.println(" teardownTest start");
		transport.close();
		
	}

}

8、运行thrift server

 

 

9、运行thrift client测试

 

查看下thrift server

10、打包放入到jmeter\lib\ext下面(libthrift-0.13.0.jar 和 新建的工程打的包)

mvn clean install  // 将创建的maven工程打成jar包,在项目的根目录下面执行

11、运行Jmeter 创建java测试请求 (thrift server 需要启动--IDE里面的main函数启动)

 

测试结果

 

 

 

 

 

posted @ 2020-07-01 20:54  M_Joker  阅读(2061)  评论(0编辑  收藏  举报