gpt4 book ai didi

java - 如何通过java编码生成Jmeter仪表板报告。 (不是 windows 或 shell 命令)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:46:51 29 4
gpt4 key购买 nike

我必须通过 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();
}
}

}

最佳答案

  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);
  2. 然后您需要设置 HTML Reporting Dashboard 目标:

    JMeterUtils.setProperty(JMETER_REPORT_OUTPUT_DIR_PROPERTY, "/where/you/want/the/dashboard/to/be/generated");
  3. 最后您可以调用 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();
}
}

引用资料:

关于java - 如何通过java编码生成Jmeter仪表板报告。 (不是 windows 或 shell 命令),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51968942/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com