有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java测试计划结构在JMeter UI(预期)和API(错误)之间变化

我的测试计划在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代码中,我尝试像这样添加采样器

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中一样,按照上面提到的顺序获得测试计划

二,。测试计划树会随着Java版本的不同而变化吗


共 (1) 个答案

  1. # 1 楼答案

    以下代码依赖于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 Test Plan from Java Code

    这应该是你要找的东西


    还要注意,有一种更简单的方法可以在不使用JMeter GUI的情况下创建JMeter测试计划,Taurus工具提供了使用简单的YAML语法创建和运行JMeter测试的可能性,有关详细信息,请参阅Taurus: A New Star in the Test Automation Tools Constellation文章