JMeter 生成 jmx 文件
发布网友
发布时间:2023-01-11 14:44
我来回答
共1个回答
热心网友
时间:2023-10-29 00:10
/**
* jmx 文件生产者
* @author zhanghang
* @date 2018-12-25
*/
public class JMXCreator {
public static void main(String[] argv) throws Exception {
// Initialize the configuration variables
String jmeterHome = "C:\\Users\\Wu\\Desktop\\apache-jmeter-5.0";
JMeterUtils.setJMeterHome(jmeterHome);
JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir() + "\\jmeter.properties");
JMeterUtils.initLogging();
JMeterUtils.initLocale();
// TestPlan
TestPlan testPlan = new TestPlan();
testPlan.setName("Test Plan");
testPlan.setEnabled(true);
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
// ThreadGroup controller
LoopController loopController = new LoopController();
loopController.setEnabled(true);
loopController.setLoops(5);
loopController.setProperty(TestElement.TEST_CLASS,LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS,LoopControlPanel.class.getName());
// ThreadGroup
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Thread Group");
threadGroup.setEnabled(true);
threadGroup.setSamplerController(loopController);
threadGroup.setNumThreads(5);
threadGroup.setRampUp(10);
threadGroup.setProperty(TestElement.TEST_CLASS,ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS,ThreadGroupGui.class.getName());
// HTTPSamplerProxy
HTTPSamplerProxy httpSamplerProxy = new HTTPSamplerProxy();
httpSamplerProxy.setProperty(TestElement.GUI_CLASS,"ThreadGroupGui");
httpSamplerProxy.setDomain("site.baidu.com");
// Create TestPlan hash tree
HashTree testPlanHashTree = new HashTree();
testPlanHashTree.add(testPlan);
// Add ThreadGroup to TestPlan hash tree
HashTree threadGroupHashTree = new HashTree();
threadGroupHashTree = testPlanHashTree.add(testPlan, threadGroup);
// Add Java Sampler to ThreadGroup hash tree
HashTree javaSamplerHashTree = new HashTree();
javaSamplerHashTree = threadGroupHashTree.add(httpSamplerProxy);
// Save to jmx file
SaveService.saveTree(testPlanHashTree, new FileOutputStream("C:\\Users\\Wu\\Desktop\\test.jmx"));
}
}