how to get Jmeter test result from JAVA Class by using JMeter API

user3923767 Source

I'm new to JMeter. I found the following code to run a JMeter test plan(.jmx) from JAVA project by using JMeter API StandardJMeterEngine.

But how can I get the execution result as return?

I saw many articles about SampleResult. If this is a solution, can you please provide details about how can I integrate my sampleResult into the existing JMeter test plan.

    // JMeter Engine
    StandardJMeterEngine jmeter = new StandardJMeterEngine();

    // Initialize Properties, logging, locale, etc.


    // Load existing .jmx Test Plan
    FileInputStream in = new FileInputStream("../Integ.jmx");
    HashTree testPlanTree = SaveService.loadTree(in);
    // Run JMeter Test

    <how to capture result here?????????????????>


answered 4 years ago UBIK LOAD PACK #1

With this code, your plan woukd contain a View Results Tree that mentions a file.

Jmeter Would write results there.

answered 4 years ago Dmitri T #2

Add the following lines before stanza:

Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("", "summary");//$NON-NLS-1$
if (summariserName.length() > 0) {
    summer = new Summariser(summariserName);

String logFile = "/path/to/output/file.jtl";
ResultCollector logger = new ResultCollector(summer);
testPlanTree.add(testPlanTree.getArray()[0], logger);

The results will be going to /path/to/output/file.jtl file. Which can be opened after test execution via listener of your choice in JMeter GUI.

You can control what values to store via file. As per JMeter Performance and Tuning Tips guide recommended values are:; 

answered 3 years ago FrankSchulz #3

If you want to access the test results directly in Java without the workaround via file output, you can redefine the result collector and get notified for each sample event:

public class MyResultCollector extends ResultCollector {

    public MyResultCollector(Summariser summer) {

    public void sampleOccurred(SampleEvent e) {
        SampleResult r = e.getResult();
        if (r.isSuccessful()) {
            System.out.println("Response time in milliseconds: " + r.getTime());

comments powered by Disqus