我按照this thread中介绍的方法,然后保存hashTree,如下所示:
FileOutputStream out = new FileOutputStream("test.jmx");
SaveService.saveTree(hashTree, out);
是否可以保存和恢复创建的测试?
另一方面,有a project on GitHub来创建和保存测试。结果如何无效JMX文件。
我将创建的文件与简单的JMeter有效测试脚本进行比较。这些属性未添加:
guiclass="TestPlanGui" testclass="TestPlan"
我手动添加它们并修复了所有错误?!
测试和GUI类必须在test元素中设置为属性。这是AbstractJMeterGuiComponent的一部分:
mc.setProperty(new StringProperty(TestElement.GUI_CLASS, this.getClass().getName()));
mc.setProperty(new StringProperty(TestElement.TEST_CLASS, mc.getClass().getName()));
因此,GUI和核心相互联系,GUI中有一些主要配置。
如何构建有效的JMX whiteout知道GUI?
答案 0 :(得分:1)
您很可能忘记设置以下属性:
如果省略它们,您仍然可以使用JMeterEngine运行测试,但尝试在GUI中打开生成的脚本将失败。
以下是包含Thread Group和HTTP Request采样器的示例测试计划,您可以将其用作参考。
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
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.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.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import java.io.File;
import java.io.FileOutputStream;
public class JMeterFromJava {
public static void main(String[] args) throws Exception {
String jmeterHome = "/path/to/your/jmeter/installation";
JMeterUtils.setJMeterHome(jmeterHome);
JMeterUtils.loadJMeterProperties(jmeterHome + "/bin/jmeter.properties");
JMeterUtils.initLocale();
HashTree testPlanTree = new HashTree();
HTTPSamplerProxy examplecomSampler = new HTTPSamplerProxy();
examplecomSampler.setDomain("example.com");
examplecomSampler.setPort(80);
examplecomSampler.setPath("/");
examplecomSampler.setMethod("GET");
examplecomSampler.setName("Open example.com");
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();
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Example Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.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 threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(examplecomSampler);
SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + "/bin/test.jmx"));
}
}
参考文献: