我创建了一个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 Sampler。 我需要配置我的Java请求,而不是HTTPSampler。 请协助。
答案 0 :(得分:1)
添加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接口,只需替换classname并在需要时提供你自己的参数。
完整代码以防万一:
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测试的不同方法的全面信息,请参阅Five Ways To Launch a JMeter Test without Using the JMeter GUI。