Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
    JMeterUtils.loadJMeterProperties("../jmeter.properties");
    JMeterUtils.setJMeterHome("../apache-jmeter-2.11");
    JMeterUtils.initLogging();
    JMeterUtils.initLocale();

    SaveService.loadProperties();

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

    <how to capture result here?????????????????>
    jmeter.exit();
like image 577
user3923767 Avatar asked Jan 18 '26 14:01

user3923767


1 Answers

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) {
        super(summer);
    }

    @Override
    public void sampleOccurred(SampleEvent e) {
        super.sampleOccurred(e);
        SampleResult r = e.getResult();
        if (r.isSuccessful()) {
            System.out.println("Response time in milliseconds: " + r.getTime());
        }
    }
}
like image 140
FrankSchulz Avatar answered Jan 21 '26 05:01

FrankSchulz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!