Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and run Apache JMeter Test Scripts from a Java program?

I want to use the API provided by Apache JMeter to create and run test scripts from a Java program. I have understood the basics of ThreadGroup and Samplers. I can create those in my Java class by using the JMeter API.

ThreadGroup threadGroup = new ThreadGroup();     LoopController lc = new LoopController();     lc.setLoops(5);     lc.setContinueForever(true);     threadGroup.setSamplerController(lc);     threadGroup.setNumThreads(5);     threadGroup.setRampUp(1);  HTTPSampler sampler = new HTTPSampler();     sampler.setDomain("localhost");     sampler.setPort(8080);     sampler.setPath("/jpetstore/shop/viewCategory.shtml");     sampler.setMethod("GET");      Arguments arg = new Arguments();     arg.addArgument("categoryId", "FISH");      sampler.setArguments(arg); 

However, I am not getting any idea on how to create a test script combining the thread group and sampler and then execute it from the same program. Any ideas?

like image 433
Alim Ul Gias Avatar asked Oct 02 '13 21:10

Alim Ul Gias


People also ask

Can Java be used with JMeter?

Apache JMeter is a software tool that is used for load testing applications on various protocols and technologies. It is written in Java and can be run on any Java-supported platforms such as macOS, Windows, etc. It even offers IDE for the load test development.

How do I run a JMX file in Java?

Try to open your /home/ksahu/MyScreenshots/k. jmx in JMeter GUI. If it does not open - there is a problem with the code, you generated the JMX file with. In that case update your question with the code, you used to create the k.


1 Answers

If I understand correctly, you want to run an entire test plan programmatically from within a Java program. Personally, I find it easier to create a test plan .JMX file and run it in JMeter non-GUI mode :)

Here is a simple Java example based on the controller and sampler used in the original question.

import org.apache.jmeter.control.LoopController; import org.apache.jmeter.engine.StandardJMeterEngine; import org.apache.jmeter.protocol.http.sampler.HTTPSampler; import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.TestPlan; import org.apache.jmeter.threads.SetupThreadGroup; import org.apache.jmeter.util.JMeterUtils; import org.apache.jorphan.collections.HashTree;  public class JMeterTestFromCode {      public static void main(String[] args){         // Engine         StandardJMeterEngine jm = new StandardJMeterEngine();         // jmeter.properties         JMeterUtils.loadJMeterProperties("c:/tmp/jmeter.properties");          HashTree hashTree = new HashTree();               // HTTP Sampler         HTTPSampler httpSampler = new HTTPSampler();         httpSampler.setDomain("www.google.com");         httpSampler.setPort(80);         httpSampler.setPath("/");         httpSampler.setMethod("GET");          // Loop Controller         TestElement loopCtrl = new LoopController();         ((LoopController)loopCtrl).setLoops(1);         ((LoopController)loopCtrl).addTestElement(httpSampler);         ((LoopController)loopCtrl).setFirst(true);          // Thread Group         SetupThreadGroup threadGroup = new SetupThreadGroup();         threadGroup.setNumThreads(1);         threadGroup.setRampUp(1);         threadGroup.setSamplerController((LoopController)loopCtrl);          // Test plan         TestPlan testPlan = new TestPlan("MY TEST PLAN");          hashTree.add("testPlan", testPlan);         hashTree.add("loopCtrl", loopCtrl);         hashTree.add("threadGroup", threadGroup);         hashTree.add("httpSampler", httpSampler);                 jm.configure(hashTree);          jm.run();     } } 

Dependencies

These are the bare mininum JARs required based on JMeter 2.9 and the HTTPSampler used. Other samplers will most likely have different library JAR dependencies.

  • ApacheJMeter_core.jar
  • jorphan.jar
  • avalon-framework-4.1.4.jar
  • ApacheJMeter_http.jar
  • commons-logging-1.1.1.jar
  • logkit-2.0.jar
  • oro-2.0.8.jar
  • commons-io-2.2.jar
  • commons-lang3-3.1.jar

Note

  • I also hardwired the path to jmeter.properties in c:\tmp on Windows after first copying it from the JMeter installation /bin directory.
  • I wasn't sure how to set a forward proxy for the HTTPSampler.
like image 103
T.P. Avatar answered Oct 04 '22 13:10

T.P.