简体   繁体   中英

Launch jmeter from junit test

Is there any way to integrate a recorded jmeter script( which tests the load exerted by a recorded series of actions executed by multiple users) in a selenium/junit test case). such that i just execute that selenium/junit test case with java, and it gives me the performance results of the junit report? I have found posts telling how to integrate selenium webdriver into jmeter, but not the other way around.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM