0

I am trying to execute Jmeter with Java so as to integrate Performance/Load Testing with Jmeter within our Test Automation Framework. I did some research and found (successfully able to execute as well) the code mentioned below.

I'm stuck with the dashboard results. I looked it up on the internet but wasn't able to find anything regarding how can I generate the HTML Dashboard Report of Jmeter with Java.

package JmeterJava;
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 Jmeter {

    public static void main(String[] argv) throws Exception {
        // JMeter Engine
        StandardJMeterEngine jmeter = new StandardJMeterEngine();


        // Initialize Properties, logging, locale, etc.
        JMeterUtils.loadJMeterProperties("/Path/jmeter/apache-jmeter-5.1.1/bin/jmeter.properties");
        JMeterUtils.setJMeterHome("/Path/jmeter/apache-jmeter-5.1.1/");
        //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 existing .jmx Test Plan
        HashTree testPlanTree = SaveService.loadTree(new File("/Path/Test-Plan.jmx"));

      //add Summarizer output to get test progress in stdout like:
        // summary =      2 in   1.3s =    1.5/s Avg:   631 Min:   290 Max:   973 Err:     0 (0.00%)
        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 = "/Path/results.jtl";
        ResultCollector logger = new ResultCollector(summer);
        logger.setFilename(logFile);
        testPlanTree.add(testPlanTree.getArray()[0], logger);
        
        // Run JMeter Test
        jmeter.configure(testPlanTree);
        jmeter.run();
    }

}

1 Answer 1

2

Add the following lines to the end of your code:

JMeterUtils.setProperty("jmeter.reportgenerator.exporter.html.classname", "org.apache.jmeter.report.dashboard.HtmlTemplateExporter");
JMeterUtils.setProperty("jmeter.reportgenerator.exporter.html.property.output_dir","report-output/dashboard");

ReportGenerator generator = new ReportGenerator("/Path/results.jtl", null);
generator.generate();

And upon the next execution you will see the HTML Reporting Dashboard under report-output/dashboard folder

More information:

4
  • Thank you Dmitri. I tried your code and got this error: "Exception in thread "main" java.lang.IllegalArgumentException: Cannot read test results file : index.html at org.apache.jmeter.report.dashboard.ReportGenerator.<init>(ReportGenerator.java:134) at JmeterJava.Jmeter.main(Jmeter.java:53)" Oct 4, 2021 at 9:32
  • I changed null to logger in: "ReportGenerator generator = new ReportGenerator("index.html", logger);" and got this error, "Exception in thread "main" org.apache.jmeter.report.core.SampleException: Could not read metadata ! at org.apache.jmeter.report.core.CsvSampleReader.readMetadata(CsvSampleReader.java:174) at org.apache.jmeter.report.core.CsvSampleReader.<init>(CsvSampleReader.java:119) at org.apache.jmeter.report.core.CsvSampleReader.<init>(CsvSampleReader.java:87) (Part 1/2) Oct 4, 2021 at 9:32
  • at org.apache.jmeter.report.processor.CsvFileSampleSource.<init>(CsvFileSampleSource.java:113) at org.apache.jmeter.report.dashboard.ReportGenerator.generate(ReportGenerator.java:211) at JmeterJava.Jmeter.main(Jmeter.java:54) Caused by: java.lang.IllegalArgumentException: File is empty at org.apache.jmeter.report.core.CsvSampleReader.readMetadata(CsvSampleReader.java:144)" (Part 2/2) Oct 4, 2021 at 9:33
  • It was a copy-paste issue, try new ReportGenerator("/Path/results.jtl", null);
    – Dmitri T
    Oct 4, 2021 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.