gpt4 book ai didi

java - 纯粹用 Java 为 Java Sampler 创建新的 JMeter 测试

转载 作者:行者123 更新时间:2023-11-30 02:38:51 26 4
gpt4 key购买 nike

我创建了一个 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采样器。我看到例子 here指出如何配置 HTTP 采样器。我需要配置 Java 请求,而不是 HTTPSampler。请帮忙。

最佳答案

添加 Java Request 的示例代码采样器将执行SleepTest看起来像:

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接口(interface)只需替换类名并根据需要提供您自己的参数。

完整代码以防万一:

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);
}
}

参见Five Ways To Launch a JMeter Test without Using the JMeter GUI有关运行 JMeter 测试的不同方法的全面信息。

关于java - 纯粹用 Java 为 Java Sampler 创建新的 JMeter 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42344726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com