我想使用JMeterAPI在java中动态创建jmeter(.jmx)文件,并使用非gui模式运行该文件。但是在尝试生成".jmx"文件没有按预期生成。如果能给我一份工作样品就太好了。
我试过下面的代码:
JMeterUtils.loadJMeterProperties("jmeter.properties");
HashTree hashTree = new HashTree();
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain("localhost");
httpSampler.setPort(5000);
httpSampler.setPath("/api/v1/data");
httpSampler.setMethod("GET");
TestElement loopCtrl = new LoopController();
((LoopController)loopCtrl).setLoops(1);
((LoopController)loopCtrl).addTestElement(httpSampler);
((LoopController)loopCtrl).setFirst(true);
SetupThreadGroup threadGroup = new SetupThreadGroup();
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController((LoopController)loopCtrl);
TestPlan testPlan = new TestPlan("MY TEST PLAN");
hashTree.add("testPlan", testPlan);
hashTree.add("loopCtrl", loopCtrl);
hashTree.add("threadGroup", threadGroup);
hashTree.add("httpSampler", httpSampler);
try {SaveService.saveTree(hashTree, new FileOutputStream("jmxFile.jmx"));}
catch(Exception ex) {ex.printStackTrace();}
但是我没有得到预期的JMX文件。相反,我得到的结果在文件如下:生成的Jmx文件
你错过了一些重要的东西:
- 你需要调用JMeterUtils.setJMeterHome()函数并提供JMeter安装路径
- 你需要设置TestElement。TEST_CLASS和TestElement。将gu_class设置为各自的值
生成.jmx脚本的示例工作代码,该脚本可以在JMeter GUI中打开而没有问题(截至JMeter 5.5):
File jmeterHome = new File("/Users/dtikhanski/Applications/jmeter");
String slash = System.getProperty("file.separator");
File jmeterProperties = new File(jmeterHome.getPath() + slash + "bin" + slash + "jmeter.properties");
JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
JMeterUtils.setJMeterHome(jmeterHome.getPath());
HashTree hashTree = new HashTree();
HTTPSamplerProxy examplecomSampler = new HTTPSamplerProxy();
examplecomSampler.setDomain("localhost");
examplecomSampler.setPort(5000);
examplecomSampler.setPath("/api/v1/data");
examplecomSampler.setMethod("GET");
examplecomSampler.setName("GET http://localhost:5000/api/v1/data");
examplecomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
examplecomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();
SetupThreadGroup threadGroup = new SetupThreadGroup();
threadGroup.setName("Example Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, SetupThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, SetupThreadGroupGui.class.getName());
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
hashTree.add(testPlan);
HashTree threadGroupHashTree = hashTree.add(testPlan, threadGroup);
threadGroupHashTree.add(examplecomSampler);
SaveService.saveTree(hashTree, new FileOutputStream("jmxFile.jmx"));
更多信息:
- JMeter命令行概述:启动测试的5种方法
- jmeter-from-code示例项目