
最后,在研究了jmeter源代码之后,我发现除了我在做什么之外,我还需要显式设置guiClass和testClass参数。
testPlan.setProperty(TestElement.TEST_CLASS,TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS,TestPlanGui.class.getName());
类似地,对于其他测试元素,例如ThreadGroup,JavaSampler等。
完整的代码如下,
package com.test;import java.io.FileOutputStream;import org.apache.jmeter.control.LoopController;import org.apache.jmeter.control.gui.LoopControlPanel;import org.apache.jmeter.control.gui.TestPlanGui;import org.apache.jmeter.protocol.java.control.gui.JavaTestSamplerGui;import org.apache.jmeter.protocol.java.sampler.JavaSampler;import org.apache.jmeter.save.SaveService;import org.apache.jmeter.testelement.TestElement;import org.apache.jmeter.testelement.TestPlan;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;public class JMXCreator { public static void main(String[] argv) throws Exception { // Initialize the configuration variables String jmeterHome = "D:\apache-jmeter-2.11"; 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()); // JavaSampler JavaSampler javaSampler = new JavaSampler(); javaSampler.setClassname("my.example.sampler"); javaSampler.setEnabled(true); javaSampler.setProperty(TestElement.TEST_CLASS, JavaSampler.class.getName()); javaSampler.setProperty(TestElement.GUI_CLASS, JavaTestSamplerGui.class.getName()); // 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(javaSampler); // Save to jmx file SaveService.saveTree(testPlanHashTree, new FileOutputStream( "d:\test.jmx")); }}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)