Launch jmeter from junit test
You can execute existing JMeter test using JMeter Java API, example code would look like:
import org.apache.jmeter.engine.StandardJMeterEngine;import org.apache.jmeter.reporters.ResultCollector;import org.apache.jmeter.reporters.Summariser;import org.apache.jmeter.save.SaveService;import org.apache.jmeter.util.JMeterUtils;import org.apache.jorphan.collections.HashTree;import java.io.File;public class JMeterFromCode { public static void main(String[] args) throws Exception { // JMeter Engine StandardJMeterEngine jmeter = new StandardJMeterEngine(); // Initialize Properties, logging, locale, etc. JMeterUtils.loadJMeterProperties("/tmp/jmeter/bin/jmeter.properties"); JMeterUtils.setJMeterHome("/tmp/jmeter"); JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level JMeterUtils.initLocale(); // Initialize JMeter SaveService SaveService.loadProperties(); // Load Test Plan HashTree testPlanTree = SaveService.loadTree(new File("/tmp/jmeter/bin/test.jmx")); 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 = "/tmp/jmeter/bin/test.jtl"; ResultCollector logger = new ResultCollector(summer); logger.setFilename(logFile); testPlanTree.add(testPlanTree.getArray()[0], logger); // Run JMeter Test jmeter.configure(testPlanTree); jmeter.run(); }}
Replace:
- all occurrences of
/tmp/jmeter
- with path to your JMeter installation /tmp/jmeter/bin/test.jmx
- with path to the .jmx file, containing the recorded JMeter script/tmp/jmeter/bin/test.jtl
- with the desired location of the .jtl results file
See Five Ways To Launch a JMeter Test without Using the JMeter GUI article for more information on the possibilities of executing a JMeter test, maybe you will find an easier integration solution as JMeter tests can be executed via Maven plugin or Ant Task along with Selenium tests.