一尘不染

完全使用Java for Java Sampler创建新的JMeter测试

jmeter

我创建了一个JMeter Java请求,并将jar复制到了ext目录(apache- jmeter-3.1\lib\ext)。我正在尝试在远程计算机上运行测试。通过GUI,我可以配置和运行Java请求。也可以使用JMX文件通过Java代码调用测试,如下所示。

ClientJMeterEngine jmeter = new ClientJMeterEngine("myclient");
File file = new File(RemoteJmeterTest.class.getResource("/finalSample.jmx").getFile());
JMeterUtils.loadJMeterProperties("D:/apache-jmeter-3.1/bin/jmeter.properties");
JMeterUtils.setJMeterHome("D:/Tools/apache-jmeter-3.1");

HashTree testPlanTree = SaveService.loadTree(file); 
jmeter.configure(testPlanTree); 
jmeter.runTest();

现在,我想纯粹用Java创建一个新的JMeter测试。但是我不知道如何配置Java采样器。我在这里看到了有关如何配置HTTP Sampler的示例。代替HTTPSampler,我需要配置Java请求。请协助。


阅读 527

收藏
2020-07-24

共1个答案

一尘不染

添加将执行SleepTestJava
Request
采样器的示例代码如下所示:

JavaSampler javaSampler = new JavaSampler();
javaSampler.setName("Java Request");
javaSampler.setClassname("org.apache.jmeter.protocol.java.test.SleepTest");
Arguments arguments = new Arguments();
arguments.addArgument("SleepTime", "1000");
arguments.addArgument("SleepMask", "0x33F");
javaSampler.setArguments(arguments);
javaSampler.setProperty(TestElement.TEST_CLASS, JavaSampler.class.getName());
javaSampler.setProperty(TestElement.GUI_CLASS, JavaTestSamplerGui.class.getName());

如果您需要执行自己的实现JavaSamplerClient接口的类,只需替换类名并在需要时提供您自己的参数即可。

完整的代码,以防万一:

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.java.control.gui.JavaTestSamplerGui;
import org.apache.jmeter.protocol.java.sampler.JavaSampler;
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.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 JMeterFromScratch {

    public static void main(String[] args) throws Exception {
        //JMeter Engine
        StandardJMeterEngine jmeter = new StandardJMeterEngine();

        //JMeter initialization (properties, log levels, locale, etc)
        JMeterUtils.loadJMeterProperties("C:/jmeter/bin/jmeter.properties");
        JMeterUtils.setJMeterHome("C:/jmeter");
        //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();

        // Java Request
        JavaSampler javaSampler = new JavaSampler();
        javaSampler.setName("Java Request");
        javaSampler.setClassname("org.apache.jmeter.protocol.java.test.SleepTest");
        Arguments arguments = new Arguments();
        arguments.addArgument("SleepTime", "1000");
        arguments.addArgument("SleepMask", "0x33F");
        javaSampler.setArguments(arguments);
        javaSampler.setProperty(TestElement.TEST_CLASS, JavaSampler.class.getName());
        javaSampler.setProperty(TestElement.GUI_CLASS, JavaTestSamplerGui.class.getName());


        // Loop Controller
        TestElement loopController = new LoopController();
        ((LoopController) loopController).setLoops(1);
        loopController.addTestElement(javaSampler);
        ((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

        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setNumThreads(1);
        threadGroup.setRampUp(1);
        threadGroup.setName("Thread Group");
        threadGroup.setSamplerController(((LoopController) loopController));
        threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
        threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.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(javaSampler);


        // 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("JMeter .jmx script is available at test.jmx");
        System.exit(0);
    }
}

有关运行JMeter测试的不同方式的全面信息,请参见不使用JMeter
GUI来启动JMeter测试的五种方法

2020-07-24