使用 JSR223 采样器创建 JMeter 测试计划

Create JMeter Test Plan using JSR223 Sampler

我是 Jmeter 的新手,我目前的要求是在 Java 中使用 JSR223 采样器创建一个 JMeter 测试计划,然后触发它。

我尝试的第一步是 运行 jmx 文件(在脚本区域使用 JSR223 采样器和 java 代码片段)通过 Java 但出现以下错误 - javax.scripts.ScriptException: 找不到名为 : ' 的引擎 java',确保在 JSR223 测试元素中设置语言字段:JSR223 采样器。

我的代码:

File jmeterHome = new File(System.getProperty("jmeter.home")); 字符串斜杠 = System.getProperty("file.separator");

    if (jmeterHome.exists()) {
        File jmeterProperties = new File(jmeterHome.getPath() + slash + "bin" + slash + "jmeter.properties");
        if (jmeterProperties.exists()) {
            //JMeter Engine
            StandardJMeterEngine jmeter = new StandardJMeterEngine();

            //JMeter initialization (properties, log levels, locale, etc)
            JMeterUtils.setJMeterHome(jmeterHome.getPath());
            JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
            JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
            JMeterUtils.initLocale();

            SaveService.loadProperties();

            // JMeter Test Plan, basically JOrphan HashTree
            HashTree testPlanTree = SaveService.loadTree(new File(JMX FILE PATH))   

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


        }
    }

如果你想“创建”一个测试计划,你真的应该“创建”它,这听起来像是你想从在线资源复制和粘贴现成的解决方案,或者请别人为你编写脚本.

我希望您展示您的代码并突出显示有问题的地方,而不是要求实施测试计划。


随便。

以下是使用 Java 代码实例化 JSR223 Sampler 的方法:

JSR223Sampler jsr223Sampler = new JSR223Sampler();
jsr223Sampler.setName("JSR223 Sampler");
jsr223Sampler.setProperty("cacheKey", "true");
jsr223Sampler.setProperty("script", "println('Hello')");
jsr223Sampler.setProperty("scriptLanguage", "groovy");
jsr223Sampler.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
jsr223Sampler.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());

完整 class 以防万一:

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.sampler.JSR223Sampler;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testbeans.gui.TestBeanGUI;
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[] argv) throws Exception {

        File jmeterHome = new File(System.getProperty("jmeter.home"));
        String slash = System.getProperty("file.separator");

        if (jmeterHome.exists()) {
            File jmeterProperties = new File(jmeterHome.getPath() + slash + "bin" + slash + "jmeter.properties");
            if (jmeterProperties.exists()) {
                //JMeter Engine
                StandardJMeterEngine jmeter = new StandardJMeterEngine();

                //JMeter initialization (properties, log levels, locale, etc)
                JMeterUtils.setJMeterHome(jmeterHome.getPath());
                JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
                JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
                JMeterUtils.initLocale();

                // JMeter Test Plan, basically JOrphan HashTree
                HashTree testPlanTree = new HashTree();

                // JSR223 Sampler

                JSR223Sampler jsr223Sampler = new JSR223Sampler();
                jsr223Sampler.setName("JSR223 Sampler");
                jsr223Sampler.setProperty("cacheKey", "true");
                jsr223Sampler.setProperty("script", "println('Hello')");
                jsr223Sampler.setProperty("scriptLanguage", "groovy");
                jsr223Sampler.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
                jsr223Sampler.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());


                // Loop Controller
                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();

                // Thread Group
                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());

                // 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(jsr223Sampler);


                // save generated test plan to JMeter's .jmx file format
                SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + slash + "example.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 = jmeterHome + slash + "example.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 " + jmeterHome + slash + "example.jtl file for results");
                System.out.println("JMeter .jmx script is available at " + jmeterHome + slash + "example.jmx");
                System.exit(0);

            }
        }

        System.err.println("jmeter.home property is not set or pointing to incorrect location");
        System.exit(1);


    }
}

参考文献: