JMeter API
初始化jmeter环境
JMeterUtils.loadJMeterProperties("E:\\apache-jmeter-5.4.3\\bin\\jmeter.properties");
JMeterUtils.setJMeterHome("E:\\apache-jmeter-5.4.3");
JMeterUtils.initLocale();
创建测试计划,TestElement.TEST_CLASS和TestElement.GUI_CLASS的配置一定要正确,否则GUI工具无法打开脚本
TestPlan testPlan = new TestPlan("Test Plan");
testPlan.setFunctionalMode(false);
testPlan.setTearDownOnShutdown(true);
testPlan.setComment("");
testPlan.setSerialized(false);
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
创建一个线程组,setNumThreads设置线程数,setRampUp设置线程间隔时间
SetupThreadGroup threadGroup = new SetupThreadGroup();
threadGroup.setName("线程组");
threadGroup.isEnabled();
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
threadGroup.setNumThreads(1);
threadGroup.setRampUp(0);
创建请求头管理器
HeaderManager headerManager = new HeaderManager();
headerManager.setName("请求头管理器");
headerManager.isEnabled();
headerManager.add(new Header("Content-Type", "application/json"));
headerManager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
headerManager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());
创建循环控制器
LoopController loopController = new LoopController();
loopController.setName("循环控制器");
loopController.setLoops(10);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
创建http请求
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain("127.0.0.1");
httpSampler.setPort(8080);
httpSampler.setPath("/apiPath");
httpSampler.setMethod("POST");
httpSampler.setName("HTTP请求");
httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
创建总结报告
Summariser summariser = new Summariser();
ResultCollector resultCollector = new ResultCollector(summariser);
resultCollector.setName("汇总报告");
resultCollector.setProperty(TestElement.TEST_CLASS,ResultCollector.class.getName());
resultCollector.setProperty(TestElement.GUI_CLASS, SummaryReport.class.getName());
resultCollector.setFilename("D:\\result\\result.jtl");
创建响应时间图
ResultCollector responseTime = new ResultCollector(summariser);
responseTime.setName("响应时间图");
responseTime.setProperty(TestElement.TEST_CLASS,ResultCollector.class.getName());
responseTime.setProperty(TestElement.GUI_CLASS, RespTimeGraphVisualizer.class.getName());
responseTime.setFilename("D:\\result\\result.jtl");
创建察看结果树
ResultCollector resultTree = new ResultCollector(summariser);
resultTree.setName("察看结果树");
resultTree.setProperty(TestElement.TEST_CLASS,ResultCollector.class.getName());
resultTree.setProperty(TestElement.GUI_CLASS, ViewResultsFullVisualizer.class.getName());
resultTree.setFilename("D:\\result\\result.jtl");
创建响应断言,setToContainsType()为响应文本包含校验
ResponseAssertion responseAssertion = new ResponseAssertion();
responseAssertion.setName("响应断言");
responseAssertion.setAssumeSuccess(false);
responseAssertion.addTestString("\"success\":true");
responseAssertion.setToContainsType();
responseAssertion.setTestFieldResponseData();
responseAssertion.setProperty(TestElement.TEST_CLASS,ResponseAssertion.class.getName());
responseAssertion.setProperty(TestElement.GUI_CLASS, AssertionGui.class.getName());
将循环控制器添加到线程组
threadGroup.setSamplerController(loopController);
将http请求和请求头管理器添加到线程组
threadGroup.addTestElement(httpSampler);
threadGroup.addTestElement(headerManager);
将线程组添加到测试计划中
testPlan.addThreadGroup(threadGroup);
创建HashTree,生成jmx脚本
HashTree subTree = new HashTree();
subTree.add(threadGroup,httpSampler);
subTree.add(threadGroup,headerManager);
subTree.add(threadGroup,resultCollector);
subTree.add(threadGroup,responseTime);
subTree.add(threadGroup,resultTree);
subTree.add(threadGroup,responseAssertion);
HashTree tree = new HashTree();
tree.add(testPlan,subTree);
SaveService.saveTree(tree, new FileOutputStream("D:\\result\\test.jmx"));
不同参数类型的添加方式
//json
// 创建一个JSONObject对象,用于构造JSON参数
String[] content = {"content"};
HashMap<String,Object> map = new HashMap();
map.put("key", "value");
String jsonStr = JSONObject.toJSONString(map);
// 创建一个HTTPArgument对象,用于封装JSON参数
HTTPArgument argument = new HTTPArgument();
argument.setValue(jsonStr);
argument.setContentType("application/json;charset=UTF-8");
//将json参数添加到http请求中
httpSampler.getArguments().addArgument(argument);
//Parameters
httpSampler.addArgument("key","value");
//上传文件
HTTPFileArg httpFileArg = new HTTPFileArg();
httpFileArg.setPath("file");
httpFileArg.setParamName("d://"); //文件的本地路径
//将文件添加到http请求中
httpSampler.setHTTPFiles(new HTTPFileArg[] {httpFileArg});