【问题标题】:Creating a New JMeter Test Purely in Java ans save as valid *.jmx纯粹用 Java 创建一个新的 JMeter 测试并保存为有效的 *.jmx
【发布时间】:2018-03-12 13:22:18
【问题描述】:

我按照this thread中介绍的方法,然后保存hashTree如下:

    FileOutputStream out = new FileOutputStream("test.jmx");
    SaveService.saveTree(hashTree, out);

但是,JMeter 无法打开文件:

是否可以保存和恢复创建的测试?


另一方面,有a project on GitHub 来创建和保存测试。结果如何不是有效的 JMX 文件。


我将创建的文件与一个简单的 JMeter 有效测试脚本进行比较。未添加这些属性:

guiclass="TestPlanGui" testclass="TestPlan" 

我手动添加它们并修复所有错误?!


测试和 GUI 类必须设置为测试元素中的属性。这是 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?

【问题讨论】:

    标签: java jmeter


    【解决方案1】:

    您很可能忘记设置以下属性:

    如果您省略它们,您仍然可以使用JMeterEngine 运行测试,但是尝试在 GUI 中打开生成的脚本将会失败。

    这是一个包含Thread GroupHTTP 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"));
    
    
        }
    }
    

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多