六灶网站建设,国外扁平化网站设计欣赏,哪里有给网站做,怎么注册个人工作室前言
如果想要压测一些三方组件#xff0c;比如MQ#xff0c;redis什么的#xff0c;jmeter本身是不支持的。 本文以开发一个压测netty的echo示例#xff0c;说明如何自定义jmeter的sampler。
开发
本文以idea示例#xff0c;
新建工程
打开idea新建一个空的maven工程…前言
如果想要压测一些三方组件比如MQredis什么的jmeter本身是不支持的。 本文以开发一个压测netty的echo示例说明如何自定义jmeter的sampler。
开发
本文以idea示例
新建工程
打开idea新建一个空的maven工程
pom依赖
jmeter的核心依赖 dependencygroupIdorg.apache.jmeter/groupIdartifactIdApacheJMeter_core/artifactIdversion5.5/version/dependencydependencygroupIdorg.apache.jmeter/groupIdartifactIdApacheJMeter_java/artifactIdversion5.5/version/dependency
三方依赖比如我要压测netty开发一个netty客户端必然要引入netty相关的依赖 dependencygroupIdio.netty/groupIdartifactIdnetty-handler/artifactIdversion${netty.version}/version/dependencydependencygroupIdio.netty/groupIdartifactIdnetty-transport-native-epoll/artifactIdversion${netty.version}/version/dependency
Echo客户端
这部分代码可以从netty的示例里[io.netty.example.echo.EchoClient]拿过来改改就行
public class EchoClient {static final int SIZE Integer.parseInt(System.getProperty(size, 256));private Channel channel;public EchoClient(String host, int port) {EventLoopGroup group new NioEventLoopGroup();Bootstrap b new Bootstrap();b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_SNDBUF, 1024 * 1024).handler(new ChannelInitializerSocketChannel() {Overridepublic void initChannel(SocketChannel ch) throws Exception {ChannelPipeline p ch.pipeline();p.addLast(flushHandler, new FlushConsolidationHandler(1024, true));p.addLast(new EchoClientHandler());}});// Start the client.try {channel b.connect(host, port).sync().channel();} catch (InterruptedException e) {throw new RuntimeException(e);}}public void write(String message) {channel.writeAndFlush(message);}
}开发Jmeter的JavaSampler
Slf4j
public class EchoTest extends AbstractJavaSamplerClient {private String label echo;private String host;private int port;private String content;private AtomicInteger index new AtomicInteger(0);public static EchoClient client;public EchoTest() {log.info(this.whoAmI() \tConstruct);}Overridepublic void setupTest(JavaSamplerContext context) {// 读取设置的请求参数this.setupValues(context);// 注意如果client不是静态的类变量在jmeter指定并发数的时候每个线程会创建一个client对象所以如果需要多少个客户端根据自己场景调整if (client null) {synchronized (EchoTest.class) {if (client null) {client new EchoClient(this.host, this.port);}}}}Overridepublic SampleResult runTest(JavaSamplerContext javaSamplerContext) {SampleResult results new SampleResult();results.setSentBytes(content.length());results.setDataType(text);// 用来计算一个请求的耗时的results.sampleStart();try {// 除了这行业务代码其它可以算是模板范式client.write(content);results.setResponseOK();results.setResponseCodeOK();results.setSuccessful(true);} finally {results.sampleEnd();}results.setSampleLabel(this.label);return results;}private void setupValues(JavaSamplerContext context) {this.host context.getParameter(Host);this.port context.getIntParameter(Port);this.content context.getParameter(Content);}/*** 这个方法就是在Jmeter上设置的请求参数*/Overridepublic Arguments getDefaultParameters() {Arguments params new Arguments();params.addArgument(Host, 127.0.0.1);params.addArgument(Port, 8007);params.addArgument(Content, 输入内容);return params;}private String whoAmI() {return Thread.currentThread() Integer.toHexString(this.hashCode());}
}
关键地方已经加上注释了其它场景可以照这个模板走就行。
打包
因为有三方依赖打包的时候需要把这些依赖也打包进行来因此使用maven-assembly-plugin插件 buildfinalName${artifactId}/finalNamepluginspluginartifactIdmaven-assembly-plugin/artifactIdconfigurationdescriptorRefsdescriptorRefjar-with-dependencies/descriptorRef/descriptorRefs/configurationexecutionsexecutionidmake-assembly/idphasepackage/phasegoalsgoalsingle/goal/goals/execution/executions/plugin/plugins/build打包命令
mvn clean package打包完成在target目录下生成如下jar包
部署
将打包出来的带有*-with-dependencies.jar的jmeter-echo-jar-with-dependencies.jar放到jmeter的lib/ext目录下
启动jmeter
如果打的包没问题的话启动Jmeter增加Sampler的时候选择Java Request: 然后选择我们定义的EchoTest: 运行下看下效果