我的测试计划在UI
中是这样的Test Plan
Thread Group1
Sampler 1
header manager
result collector
bean shell post processor
Sampler 2
bean shell pre processor
header manager
result collector
bean shell post processor
Thread Group2
Bean shell pre processor
Throughput 1
Sampler 3
header manager
result collector
Throughput 2
Sampler 4
bean shell pre processor
header manager
result collector
bean shell post processor
Sampler 5
header manager
result collector
Result Collector
在Java Code中,我尝试添加像这样的采样器
ThroughputController throughput1 = new ThroughputController();
throughput1.setPercentThroughput(100.0f);
ThroughputController throughput2 = new ThroughputController();
throughput1.setPercentThroughput(0.0f);
TestPlan testPlan = new TestPlan();
testPlan.setSerialized(true);
HashTree throughputTree1 = new HashTree();
HashTree throughputTree2 = new HashTree();
HashTree testPlanTree = new HashTree();
HashTree threadGroupTree1 = new HashTree();
HashTree samplerTree1 = new HashTree();
samplerTree1.add(sampler1,resultCollector1);
samplerTree1.add(sampler1,header1);
但输出测试计划树不符合预期。
我如何以与上面提到的相同的顺序获得测试计划,就像在UI中一样?
2.测试计划树是否会因Java版本而异?
答案 0 :(得分:0)
以下代码依赖于JMeter API:
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.ThroughputController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.control.gui.ThroughputControllerGui;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.testelement.property.FloatProperty;
import org.apache.jmeter.testelement.property.IntegerProperty;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import java.io.FileOutputStream;
public class JMeterFromCode {
public static void main(String[] args) throws Exception {
String jmeterHome = "/tmp/jmeter";
JMeterUtils.setJMeterHome(jmeterHome);
JMeterUtils.loadJMeterProperties(jmeterHome + "/bin/jmeter.properties");
JMeterUtils.initLocale();
HashTree testPlanTree = new HashTree();
HTTPSamplerProxy sampler1 = new HTTPSamplerProxy();
sampler1.setName("Sampler 1");
sampler1.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
sampler1.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
HTTPSamplerProxy sampler2 = new HTTPSamplerProxy();
sampler2.setName("Sampler 2");
sampler2.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
sampler2.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
HTTPSamplerProxy sampler3 = new HTTPSamplerProxy();
sampler3.setName("Sampler 3");
sampler3.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
sampler3.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
HTTPSamplerProxy sampler4 = new HTTPSamplerProxy();
sampler4.setName("Sampler 4");
sampler4.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
sampler4.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();
ThreadGroup threadGroup1 = new ThreadGroup();
threadGroup1.setName("Thread Group 1");
threadGroup1.setNumThreads(1);
threadGroup1.setRampUp(1);
threadGroup1.setSamplerController(loopController);
threadGroup1.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup1.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
ThreadGroup threadGroup2 = new ThreadGroup();
threadGroup2.setName("Thread Group 2");
threadGroup2.setNumThreads(1);
threadGroup2.setRampUp(1);
threadGroup2.setSamplerController(loopController);
threadGroup2.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup2.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
ThroughputController throughputController1 = new ThroughputController();
throughputController1.setName("throughput 1");
IntegerProperty style = new IntegerProperty();
style.setName("ThroughputController.style");
style.setValue(1);
FloatProperty throughput1 = new FloatProperty();
throughput1.setValue(100);
throughputController1.setProperty(throughput1);
throughputController1.setProperty(style);
throughputController1.setProperty(TestElement.TEST_CLASS, ThroughputController.class.getName());
throughputController1.setProperty(TestElement.GUI_CLASS, ThroughputControllerGui.class.getName());
ThroughputController throughputController2 = new ThroughputController();
throughputController2.setName("throughput 2");
IntegerProperty style2 = new IntegerProperty();
style2.setName("ThroughputController.style");
style2.setValue(1);
FloatProperty throughput2 = new FloatProperty();
throughput2.setValue(0);
throughputController2.setProperty(throughput2);
throughputController2.setProperty(style2);
throughputController2.setProperty(TestElement.TEST_CLASS, ThroughputController.class.getName());
throughputController2.setProperty(TestElement.GUI_CLASS, ThroughputControllerGui.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());
testPlanTree.add(testPlan);
HashTree threadGroup1HashTree = testPlanTree.add(testPlan, threadGroup1);
threadGroup1HashTree.add(sampler1);
HashTree threadGroup2HashTree = testPlanTree.add(testPlan, threadGroup2);
threadGroup2HashTree.add(throughputController1, sampler2);
threadGroup2HashTree.add(throughputController2, sampler3);
threadGroup2HashTree.add(throughputController2, sampler4);
SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + "/bin/example.jmx"));
}
}
生成JMeter Test Plan,如:
这应该是您正在寻找的东西。
另请注意,在不使用JMeter GUI的情况下创建JMeter测试计划有一种更简单的选择,Taurus工具提供了使用简单YAML语法创建和运行JMeter测试的可能性,请查看{{ 3}}文章了解更多细节。