Skip to content

Instantly share code, notes, and snippets.

@budhash
Created April 8, 2014 22:19
Show Gist options
  • Select an option

  • Save budhash/10201700 to your computer and use it in GitHub Desktop.

Select an option

Save budhash/10201700 to your computer and use it in GitHub Desktop.
How to create and run Apache JMeter Test Scripts from a Java program?
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 JMeterCodeRunner {
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
Original Reference: http://stackoverflow.com/questions/19147235/how-to-create-and-run-apache-jmeter-test-scripts-from-a-java-program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment