lesson3:使用java代码的方式对不能识别的协议进行压力测试
在我们的实际环境中,我们所使用的协议肯定不只是http的方式,对于rpc等调用协议,目前jmeter没有相应的sampler支持,这时就需要通过引入我们自己写的jar包的方式来解决这个问题。例如:当我们的服务方是采用netty+pb、thrift、dubbo等rpc方式时,本文采用thrift的方式来展示。
下载thrift的编译器:https://thrift.apache.org/download
thrfit服务代码:https://github.com/mantuliu/thriftServerDemo
jmeter的sdk代码:https://github.com/mantuliu/jMetterLessons
1.首先,我们先来实现一个非常简单的thrift服务,maven的pom文件如下:需要依赖thrift等开发包
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mantu</groupId> <artifactId>thriftServerDemo</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>thriftServerDemo</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.9.0</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> </dependencies> </project>
2.实现thrift的接口定义文件,服务有一个输入参数,完成一个sayHello的动作:
namespace java com.mantu
service HelloWorldService {
string sayHello(1:string username)
}
3.使用thrift的编译器编译接口文件后,得到java文件HelloWorldService
4.服务端实现HelloWorldService的实现类,代码如下:
package com.mantu; import org.apache.thrift.TException; /** * blog http://www.cnblogs.com/mantu/ * * @author mantu * */ public class HelloWorldImpl implements HelloWorldService.Iface { public HelloWorldImpl() { } @Override public String sayHello(String username) throws TException { return "Hello," + username + " welcome to http://www.cnblogs.com/mantu/ "; } }
5.在HelloServerDemo中启动thrift服务:
package com.mantu; import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.transport.TServerSocket; /** * blog http://www.cnblogs.com/mantu/ * * @author mantu * */ public class HelloServerDemo { public static final int SERVER_PORT = 8090; public void startServer() { try { System.out.println("thrift server start ...."); TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>( new HelloWorldImpl()); TServerSocket serverTransport = new TServerSocket(SERVER_PORT); TServer.Args tArgs = new TServer.Args(serverTransport); tArgs.processor(tprocessor); tArgs.protocolFactory(new TBinaryProtocol.Factory()); TServer server = new TSimpleServer(tArgs); server.serve(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { HelloServerDemo server = new HelloServerDemo(); server.startServer(); } }
6.启动thrift服务,监听端口为8090;
7.本文中的jmeter的sdk所使用的项目管理工具为maven,各位在使用的过程中,可能会发生相关的依赖包下载不到的情况,如出现此种情况,请在开发工具中直接引用jmeter相关的开发包,不要使用maven的方式;jMetterLessons的pom文件如下,引用的jmeter的相关包,及thrift的相关包:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <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/maven-v4_0_0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.mantu</groupId> <artifactId>jMeterLessons</artifactId> <packaging>jar</packaging> <version> 1.0 -SNAPSHOT</version> <name>jMeterLessons</name> <url>http: //maven.apache.org</url> <dependencies> <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_java</artifactId> <version> 3.0 </version> </dependency> <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_core</artifactId> <version> 3.0 </version> </dependency> <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_components</artifactId> <version> 3.0 </version> </dependency> <dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version> 0.9 . 0 </version> </dependency> </dependencies> </project> |
8.sdk的主要代码实现参加类Lesson3,此类继承jmeter的AbstractJavaSamplerClient:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | package com.mantu.jmeter; 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.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import com.mantu.HelloWorldService; /** * blog http://www.cnblogs.com/mantu/ * * @author mantu * */ public class Lesson3 extends AbstractJavaSamplerClient{ public static void main(String [] args){ } @Override public SampleResult runTest(JavaSamplerContext arg0) { // TODO Auto-generated method stub String userName = arg0.getParameter( "uName" ); SampleResult sr = new SampleResult(); sr.setSampleLabel( "thrift娴嬭瘯" ); try { sr.sampleStart(); HelloClientDemo helloClient = new HelloClientDemo(); helloClient.startClient(userName); sr.setResponseData( "success" ); sr.setDataType(SampleResult.TEXT); sr.setSuccessful( true ); } catch (Exception ex){ sr.setSuccessful( false ); ex.printStackTrace(); } finally { sr.sampleEnd(); } return sr; } class HelloClientDemo { public static final String SERVER_IP = "localhost" ; public static final int SERVER_PORT = 8090 ; public static final int TIMEOUT = 30000 ; /** * * @param userName */ public void startClient(String userName) { TTransport transport = null ; try { transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT); TProtocol protocol = new TBinaryProtocol(transport); HelloWorldService.Client client = new HelloWorldService.Client( protocol); transport.open(); System.out.println(client.sayHello(userName)); } catch (Exception e) { e.printStackTrace(); } finally { if ( null != transport) { transport.close(); } } } } } |
9.在eclipse中导出jMetterLessons工程为jar包到jmeter的\lib\ext目录,并将libthrift-0.9.0.jar拷贝到\lib\ext目录
10.启动jmeter,并添加一个【java请求】的sampler,类名称选择我们刚刚开发的com.mantu.jmeter.Lesson3,并添加一个参数:uName,如图:
11.启动测试后,我们能发现测试结果为成功。
本文的协议采用了thrift协议,各位也可以使用其它的协议来模拟实现。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?