用途
1.使用jmeter做dubbo接口的性能测试
2.使用jmeter做一些小工具,例如发送mq来构造测试数据等等
POM依赖
<properties>
<jmeter.version>5.0</jmeter.version>
</properties>
<!--JMeter-->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>${jmeter.version}</version>
<exclusions>
<exclusion>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>${jmeter.version}</version>
<exclusions>
<exclusion>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
</exclusions>
</dependency>
Demo
package jmerterDubbo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
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 util.HttpUtil;
/**
* Create by shenpengfei on 2019/1/24 11:34
*/
public class jmeterDemo extends AbstractJavaSamplerClient {
/**
* 这个方法是用来自定义java方法入参的
* params.addArgument("num1","");表示入参名字叫num1,默认值为空。
* @return
*/
@Override
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("url", "");
return params;
}
/**
* 每个线程测试前执行一次,做一些初始化工作
* 获取输入的参数,赋值给变量
* @param arg0
*/
@Override
public void setupTest(JavaSamplerContext arg0) {
System.out.println("Test Start...");
}
/**
* 真正执行逻辑的方法
* @param javaSamplerContext
* @return
*/
@Override
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
SampleResult sr = new SampleResult();
//获取之前的请求参数
String url = javaSamplerContext.getParameter("url");
sr.setSamplerData("request url:"+url);
try {
// jmeter 开始统计响应时间标记
sr.sampleStart();
HttpResponse res = HttpUtil.doGet(url);
String resultData = EntityUtils.toString(res.getEntity(),"utf-8");
sr.setResponseData(resultData,"utf-8");
sr.setDataType(SampleResult.TEXT);
System.out.println(resultData);
//返回正确
sr.setSuccessful(true);
} catch (Throwable e) {
sr.setSuccessful(false);
e.printStackTrace();
} finally {
// jmeter 结束统计响应时间标记
sr.sampleEnd();
}
return sr;
}
/**
* 测试结束后调用
* @param arg0
*/
@Override
public void teardownTest(JavaSamplerContext arg0) {
System.out.println("Test End...");
}
}
mvn打jar包
依赖
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
执行mvn install即可
JMETER测试一下
放到ext目录下
选中demo类
查看结果
原文:https://www.cnblogs.com/hzxiaobai/p/10826814.html