Home > Software engineering >  JMeter test from Java Code - Generate Console/File Reports matching GUI Reports
JMeter test from Java Code - Generate Console/File Reports matching GUI Reports

Time:09-24

I run the jmx file from Java code and would like to get console/file reports.

public static void main(String[] args) throws Exception {

    StandardJMeterEngine jmeter = new StandardJMeterEngine();

    JMeterUtils.loadJMeterProperties("path/jmeter.properties");
    JMeterUtils.setJMeterHome("path/jmeter");
    JMeterUtils.initLocale();

    SaveService.loadProperties();

    File in = new File("test.jmx");
    HashTree testPlanTree = SaveService.loadTree(in);

    Summariser summer = null;
    String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
    if (summariserName.length() > 0) {
        summer = new Summariser(summariserName);
    }
    String logFile = "result.html";
    ResultCollector logger = new ResultCollector(summer);
    logger.setFilename(logFile);
    testPlanTree.add(testPlanTree.getArray()[0], logger);

    jmeter.configure(testPlanTree);
    jmeter.run();

    ReportGenerator generator = new ReportGenerator(logFile, null);
    generator.generate();
}

Running this code I have a html file generated:

timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
1632316570010,1745,Created,500,,Requests 1-1,text,false,,417,1228,1,1,https://my.url,1740,0,1371

But for example I need the report like on GUI - Aggregate Graph: enter image description here

How can I achieve this?

EDIT: Running into Null Pointer exception with this code:

public static void main(String[] args) throws Exception {

    StandardJMeterEngine jmeter = new StandardJMeterEngine();

    JMeterUtils.loadJMeterProperties("bla/jmeter.properties");
    JMeterUtils.setJMeterHome("bla/apache-jmeter-5.4.1");
    JMeterUtils.initLocale();

    File reportOutputDir = new File("report-output");
    if (reportOutputDir.exists()) {
        FileUtils.forceDelete(reportOutputDir);
    }

    File results = new File("result.html");
    if (results.exists()) {
        FileUtils.forceDelete(results);
    }

    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");

    SaveService.loadProperties();

    HashTree testPlanTree = new ListedHashTree();

    HTTPSampler httpSampler = new HTTPSampler();
    httpSampler.setDomain("host");
    httpSampler.setPort(443);
    httpSampler.setPath("/api/v1/stock-count/created");
    httpSampler.setMethod("POST");

    LoopController loopController = new LoopController();
    loopController.setLoops(1);
    loopController.addTestElement(httpSampler);
    loopController.setFirst(true);
    loopController.initialize();

    ThreadGroup threadGroup = new ThreadGroup();
    threadGroup.setNumThreads(10);
    threadGroup.setRampUp(1);
    threadGroup.setSamplerController(loopController);
    threadGroup.initialize();

    TestPlan testPlan = new TestPlan();
    testPlan.addThreadGroup(threadGroup);

    String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
    Summariser summer = new Summariser(summariserName);
    ResultCollector logger = new ResultCollector(summer);
    logger.setFilename("result.html");

    testPlanTree.add(testPlan, logger);

    jmeter.configure(testPlanTree);
    jmeter.run();

    ReportGenerator generator = new ReportGenerator("result.html", null);
    generator.generate();
}

15:39:26.906 [main] INFO org.apache.jmeter.report.dashboard.JsonExporter - Creating statistics for overall 15:39:26.906 [main] DEBUG org.apache.jmeter.report.dashboard.JsonExporter - Creating statistics for result data:null Exception in thread "main" java.lang.NullPointerException at org.apache.jmeter.report.dashboard.JsonExporter.createStatistic(JsonExporter.java:121) at org.apache.jmeter.report.dashboard.JsonExporter.export(JsonExporter.java:72) at org.apache.jmeter.report.dashboard.ReportGenerator.exportData(ReportGenerator.java:379) at org.apache.jmeter.report.dashboard.ReportGenerator.generate(ReportGenerator.java:257) at com.nordstrom.inventory.test.api.passport.JMeterTest.main(JMeterTest.java:87)

CodePudding user response:

  1. It's not "html", it's JMeter's .jtl results file in CSV format.

  2. If you want to see the "Aggregate Graph" - Open your result.html file using "Browse" button of the Aggregate Graph listener and it will load it and render the chart

  3. You might also want to consider using i.e. JMeter Plugins Command Line Tool which allows generation of various tables/charts from command line (can be also done programatically if you have the .jars in your project CLASSPATH)

  4. You might want to generate JMeter's HTML Reporting Dashboard, you're almost there, you just need to add a couple of lines to your script somewhere before test execution:

    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");
    

    and then go to report-output\dashboard folder and open index.html with your favourite browser.

  • Related