创建Apache-Jmeter项目文件-使用java

dxpyg8gm  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(106)

我想使用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 File Genrated

xtfmy6hx

xtfmy6hx1#

你错过了一些重要的东西:
1.您需要调用JMeterUtils.setJMeterHome()函数并提供JMeter安装路径
1.您需要将TestElement. TEST_CLASS和TestElement. GUI_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"));

更多信息:

相关问题