I’ve created a JMeter Java request and copied the jar to ext directory (apache-jmeter-3.1libext
).
I’m trying to run the test on a remote machine. From the GUI I’m able to configure and run the Java request.
Also able to invoke the test via Java code using the JMX file as below.
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();
Now I want to creating a New JMeter Test Purely in Java. But I don’t know how to configure the Java sampler. I see example here pointing how to configure the HTTP Sampler. Instead of HTTPSampler, I need to configure my Java Request. Please assist.
Advertisement
Answer
Example code to add a Java Request sampler which will execute SleepTest will look like:
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());
In case if you need execute your own class implementing JavaSamplerClient interface just substitute the classname and provide your own arguments if needed.
Full code just in case:
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); } }
See Five Ways To Launch a JMeter Test without Using the JMeter GUI for comprehensive information on different ways of running a JMeter test.