前提准备
我们使用jmeter发送POST请求
# url:
localhost:8088/mongo/insert
# method:
POST
# headers
{
"Content-Type" : "application/json"
}
# body
{
"name": "liuyiyang",
"password": "123456"
}
下载jmeter
我用的是5.3的版本
新建一个maven项目
我用的是spring boot项目
引入相关依赖
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.apache.commons
commons-lang3
3.9
org.projectlombok
lombok
1.18.12
org.springframework.boot
spring-boot-starter-log4j
1.3.8.RELEASE
org.apache.jmeter
ApacheJMeter_core
5.3
org.apache.jmeter
ApacheJMeter_java
5.3
org.apache.jmeter
ApacheJMeter_http
5.3
org.apache.httpcomponents
httpclient
4.5.6
编写JmterTest测试类
设置使用的jmeter
String jemterHome = "/Users/liufei/Downloads/apache-jmeter-5.3";
JMeterUtils.setJMeterHome(jemterHome);
JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir() + "/jmeter.properties");
创建测试计划
private static TestPlan getTestPlan() {
TestPlan testPlan = new TestPlan("Test Plan");
testPlan.setFunctionalMode(false);
testPlan.setSerialized(false);
testPlan.setTearDownOnShutdown(true);
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setProperty(new BooleanProperty(TestElement.ENABLED, true));
testPlan.setProperty(new StringProperty(TestElement.COMMENTS, ""));
testPlan.setTestPlanClasspath("");
Arguments arguments = new Arguments();
testPlan.setUserDefinedVariables(arguments);
return testPlan;
}
设置循环控制器
private static LoopController getLoopController() {
LoopController loopController = new LoopController();
loopController.setContinueForever(false);
loopController.setProperty(new StringProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName()));
loopController.setProperty(new StringProperty(TestElement.TEST_CLASS, LoopController.class.getName()));
loopController.setProperty(new StringProperty(TestElement.NAME, "循环控制器"));
loopController.setProperty(new StringProperty(TestElement.ENABLED, "true"));
loopController.setProperty(new StringProperty(LoopController.LOOPS, "1"));
return loopController;
}
创建线程组
/
/***
* 创建线程组
* @param loopController 循环控制器
* @param numThreads 线程数量
* @return
*/
private static ThreadGroup getThreadGroup(LoopController loopController, int numThreads) {
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setNumThreads(numThreads);
threadGroup.setRampUp(1);
threadGroup.setDelay(0);
threadGroup.setDuration(0);
threadGroup.setProperty(new StringProperty(ThreadGroup.ON_SAMPLE_ERROR, "continue"));
threadGroup.setScheduler(false);
threadGroup.setName("回放流量");
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
threadGroup.setProperty(new BooleanProperty(TestElement.ENABLED, true));
threadGroup.setProperty(new TestElementProperty(ThreadGroup.MAIN_CONTROLLER, loopController));
return threadGroup;
}
创建Http请求信息
/***
* 创建http请求信息
* @param url ip地址
* @param port 端口
* @param api url
* @param request 请求参数(请求体)
* @return
*/
private static HTTPSamplerProxy getHttpSamplerProxy(String url, String port, String api, String request) {
HTTPSamplerProxy httpSamplerProxy = new HTTPSamplerProxy();
Arguments HTTPsamplerArguments = new Arguments();
HTTPArgument httpArgument = new HTTPArgument();
httpArgument.setProperty(new BooleanProperty("HTTPArgument.always_encode", false));
httpArgument.setProperty(new StringProperty("Argument.value", request));
httpArgument.setProperty(new StringProperty("Argument.metadata", "="));
ArrayList list1 = new ArrayList<>();
list1.add(new TestElementProperty("", httpArgument));
HTTPsamplerArguments.setProperty(new CollectionProperty("Arguments.arguments", list1));
httpSamplerProxy.setProperty(new TestElementProperty("HTTPsampler.Arguments", HTTPsamplerArguments));
httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.domain", url));
httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.port", port));
httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.protocol", "http"));
httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.path", api));
httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.method", "POST"));
// JMETER_ENCODING这个是我定义的常量,设置的编码是UTF-8,后面还有其他地方用到这个常量
httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.contentEncoding", JMETER_ENCODING));
httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.follow_redirects", true));
httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.postBodyRaw", true));
httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.auto_redirects", false));
httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.use_keepalive", true));
httpSamplerProxy.setProperty(new