在Java代码中运行JMeter - JMeterThread不运行采样器

时间:2017-02-15 08:31:33

标签: java jmeter

所以我在我的maven项目中使用这个例子:

public class JMeterFromScratch {

public static void main(String[] argv) throws Exception {

    //JMeter Engine
    StandardJMeterEngine jmeter = new StandardJMeterEngine();

    //JMeter initialization (properties, log levels, locale, etc)
    JMeterUtils.loadJMeterProperties("C:/git/jbehave-snbtracker/src/main/resources/config/jmeter.properties");
    //JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
    JMeterUtils.initLocale();

    // JMeter Test Plan
    HashTree testPlanTree = new HashTree();

    // HTTP Sampler
    HTTPSampler httpSampler = new HTTPSampler();
    httpSampler.setDomain("example.com");
    httpSampler.setPort(80);
    httpSampler.setPath("/");
    httpSampler.setMethod("GET");

    // Loop Controller
    TestElement loopController = new LoopController();
    ((LoopController)loopController).setLoops(1);
    loopController.addTestElement(httpSampler);
    ((LoopController)loopController).setFirst(true);
    ((LoopController)loopController).initialize();

    // Thread Group

    SetupThreadGroup threadGroup = new SetupThreadGroup();
    threadGroup.setNumThreads(1);
    threadGroup.setRampUp(1);
    threadGroup.setSamplerController(((LoopController)loopController));

    // Test Plan
    TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");

    // Construct Test Plan from previously initialized elements
    testPlanTree.add("testPlan", testPlan);
    testPlanTree.add("loopController", loopController);
    testPlanTree.add("threadGroup", threadGroup);
    testPlanTree.add("httpSampler", httpSampler);

    // Run Test Plan
    jmeter.configure(testPlanTree);
    jmeter.run();

}

}

我有以下依赖项:

    <dependency>
        <groupId>org.apache.jmeter</groupId>
        <artifactId>ApacheJMeter_core</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.jmeter</groupId>
        <artifactId>ApacheJMeter_http</artifactId>
        <version>2.13</version>
    </dependency>

奇怪的是,我看到ThreadGroup类,在makeThread()方法中,有这一行:

JMeterThread jmeterThread = new JMeterThread(this.cloneTree(threadGroupTree), this, notifier);

cloneTree的调用似乎确实克隆了树,但是在LoopController中没有Sampler。所以当JMeterThread到达此处时:

Sampler sampler = this.threadGroupLoopController.next(); // returns null

            while(true) {
                while(this.running && sampler != null) {

sampler为空,因此无需执行任何操作。

那么我做错了什么?

感谢。

2 个答案:

答案 0 :(得分:2)

  1. 我不喜欢您pom.xml中的版本不匹配,我相信您应该将<version>3.1</version>用于两个组件
  2. 我不喜欢您构建测试计划的方式,它应该是这样的:

    testPlanTree.add(testPlan);
    HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
    threadGroupHashTree.add(httpSampler);
    
  3. 我认为你应该在.jtl文件中添加日志以便能够看到结果

  4. 完整代码以防万一:

    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.engine.StandardJMeterEngine;
    import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
    import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
    import org.apache.jmeter.reporters.ResultCollector;
    import org.apache.jmeter.reporters.Summariser;
    import org.apache.jmeter.save.SaveService;
    import org.apache.jmeter.testelement.TestElement;
    import org.apache.jmeter.testelement.TestPlan;
    import org.apache.jmeter.threads.SetupThreadGroup;
    import org.apache.jmeter.threads.gui.SetupThreadGroupGui;
    import org.apache.jmeter.util.JMeterUtils;
    import org.apache.jorphan.collections.HashTree;
    
    import java.io.FileOutputStream;
    
    public class JMeterFromScratch {
    
        public static void main(String[] args) throws Exception {
            //JMeter Engine
            StandardJMeterEngine jmeter = new StandardJMeterEngine();
    
            //JMeter initialization (properties, log levels, locale, etc)
            JMeterUtils.loadJMeterProperties("/path/to/jmeter/bin/jmeter.properties");
            JMeterUtils.setJMeterHome("/path/to/jmeter/");            
            JMeterUtils.initLocale();
    
            // JMeter Test Plan
            HashTree testPlanTree = new HashTree();
    
            // HTTP Sampler
            HTTPSamplerProxy httpSampler = new HTTPSamplerProxy();
            httpSampler.setName("HTTP Request");
            httpSampler.setDomain("example.com");
            httpSampler.setPort(80);
            httpSampler.setPath("/");
            httpSampler.setMethod("GET");
            httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
            httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
    
    
            // Loop Controller
            TestElement loopController = new LoopController();
            ((LoopController) loopController).setLoops(1);
            loopController.addTestElement(httpSampler);
            ((LoopController) loopController).setFirst(true);
            loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
            loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
            ((LoopController) loopController).initialize();
    
            // Thread Group
    
            SetupThreadGroup threadGroup = new SetupThreadGroup();
            threadGroup.setNumThreads(1);
            threadGroup.setRampUp(1);
            threadGroup.setName("setUp Thread Group");
            threadGroup.setSamplerController(((LoopController) loopController));
            threadGroup.setProperty(TestElement.TEST_CLASS, SetupThreadGroup.class.getName());
            threadGroup.setProperty(TestElement.GUI_CLASS, SetupThreadGroupGui.class.getName());
    
            // Test Plan
            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());
    
            // Construct Test Plan from previously initialized elements
            testPlanTree.add(testPlan);
            HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
            threadGroupHashTree.add(httpSampler);
    
    
            // save generated test plan to JMeter's .jmx file format
            SaveService.saveTree(testPlanTree, new FileOutputStream("test.jmx"));
    
            //add Summarizer output to get test progress in stdout like:
            // summary =      2 in   1.3s =    1.5/s Avg:   631 Min:   290 Max:   973 Err:     0 (0.00%)
            Summariser summer = null;
            String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
            if (summariserName.length() > 0) {
                summer = new Summariser(summariserName);
            }
    
    
            // Store execution results into a .jtl file
            String logFile = "test.jtl";
            ResultCollector logger = new ResultCollector(summer);
            logger.setFilename(logFile);
            testPlanTree.add(testPlanTree.getArray()[0], logger);
    
            // Run Test Plan
            jmeter.configure(testPlanTree);
            jmeter.run();
    
            System.out.println("Test completed. See test.jtl file for results");
            System.out.println("Open test.jmx file in JMeter GUI to validate the code");
            System.exit(0);
        }
    }
    

    您可以使用https://bitbucket.org/blazemeter/jmeter-from-code/文章中提到的Five Ways To Launch a JMeter Test without Using the JMeter GUI repo来获取参考工作代码。

答案 1 :(得分:1)

我尝试了几种从Java创建测试计划的方法,但始终面临着相同的问题: 创建并触发了threadGroup,但未运行HTTPSampler和LoopController。

只有上面的答案对我有帮助(所有JMeter依赖项都是Maven中的版本5)。

请注意,Blazemeter和其他重新发布相同Java配置以使其正常工作的网站已经过时,您需要根据@Dmirti T方法编写代码。

这是我与JMeter示例请求的仓库enter link description here