如何通过Java编码生成Jmeter仪表板报告。(不是Windows或Shell命令)

  1. 首先,您需要在生成报告仪表板之前生成一个.jtl结果文件,因此请确保将以下代码块添加到您的函数中:
        Summariser summer = null;
    String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
    if (summariserName.length() > 0) {
        summer = new Summariser(summariserName);
    }
    String logFile = "/path/to/result.jtl";
    ResultCollector logger = new ResultCollector(summer);
    logger.setFilename(logFile);

    testPlanTree.add(testPlanTree.getArray()[0], logger);
  1. 然后,您需要设置HTML Reporting Dashboard目标:
        JMeterUtils.setProperty(JMETER_REPORT_OUTPUT_DIR_PROPERTY, "/where/you/want/the/dashboard/to/be/generated");
  1. 最后,您可以像这样调用ReportGenerator类:
        ReportGenerator generator = new ReportGenerator(logFile,null);
    generator.generate();

完整的代码,以防万一:

    import org.apache.jmeter.engine.StandardJMeterEngine;
    import org.apache.jmeter.report.dashboard.ReportGenerator;
    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;

    import static org.apache.jmeter.JMeter.JMETER_REPORT_OUTPUT_DIR_PROPERTY;

    public class RunJMeterTest {

        public static void main(String[] args) throws Exception {
            StandardJMeterEngine jmeter = new StandardJMeterEngine();
            JMeterUtils.loadJMeterProperties("/path/to/your/jmeter/bin/jmeter.properties");
            JMeterUtils.setJMeterHome("/path/to/your/jmeter");
            JMeterUtils.initLocale();
            SaveService.loadProperties();
            hashtree testPlanTree = SaveService.loadTree(new File("/path/to/your/jmeter/bin/test.jmx"));
            Summariser summer = null;
            String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
            if (summariserName.length() > 0) {
                summer = new Summariser(summariserName);
            }
            String logFile = "/path/to/your/jmeter/bin/result.jtl";
            ResultCollector logger = new ResultCollector(summer);
            logger.setFilename(logFile);
            testPlanTree.add(testPlanTree.getArray()[0], logger);
            jmeter.configure(testPlanTree);
            jmeter.run();
            JMeterUtils.setProperty(JMETER_REPORT_OUTPUT_DIR_PROPERTY, "/path/to/dashboard");
            ReportGenerator generator = new ReportGenerator(logFile, null);
            generator.generate();
        }
    }

参考文献:

  • 生成报告仪表板
  • 在不使用JMeter GUI的情况下启动JMeter测试的五种方法

我必须通过Java编码生成Jmeter仪表板报告。在Java代码下面,我曾用来生成普通的jmeter报告。我必须通过Java生成仪表板报告。

  public class JMeterFromExistingJMX {

    public static void main(String[] argv) throws Exception {
        // JMeter Engine
        StandardJMeterEngine jmeter = new StandardJMeterEngine();
        // Initialize Properties,logging,locale,etc.
        // JMeterUtils.loadJMeterProperties
        ("/path/to/your/jmeter/bin/jmeter.properties");
        JMeterUtils.setJMeterHome("/path/to/your/jmeter");
        JMeterUtils.initLogging();// log
        JMeterUtils.initLocale();
        // Initialize JMeter SaveService
        SaveService.loadProperties();
        // Load existing .jmx Test Plan
        FileInputStream in = new 
         FileInputStream
       ("/path/to/your/jmeter/extras/Test.jmx");
        HashTree testPlanTree = SaveService.loadTree(in);
        in.close();

        // Run JMeter Test
        jmeter.configure(testPlanTree);
        jmeter.run();
    }
    }

    }

你可能想看:
标签: shell
分享给朋友: