2

当我从 gui 调用 jmeter test 时,它可以正常工作,但是当我调用标准 JMeter 引擎的runrunTests时,它不会在/tmp/jmeter中保存任何内容。

我怎样才能让它发生?如何从保存响应保存到文件侦听器的 Java 代码中执行和获取 api 有效负载。

我使用 JMeter 5.0

    JMeterUtils.setJMeterHome(getJmeterHome());
    JMeterUtils.loadJMeterProperties(jmeterPropertiesFile.toString());
    JMeterUtils.initLocale();

    StandardJMeterEngine standardJMeterEngine = new StandardJMeterEngine();
    standardJMeterEngine.configure(rootTree);
    standardJMeterEngine.run();

生成器类

public class MySaveResponseToFileBuilder {

private String name;
private String comment;
private boolean enabled;
private String fileName;
private String variableName;

public MySaveResponseToFileBuilder() {
    this.name = "Save Response to File";
    this.enabled = true;
    this.comment = "";
    this.fileName = "";
    this.variableName = "";
}

public String getName() {
    return name;
}

public MySaveResponseToFileBuilder setName(String name) {
    this.name = name;
    return this;
}

public String getComment() {
    return comment;
}

public MySaveResponseToFileBuilder setComment(String comment) {
    this.comment = comment;
    return this;
}

public boolean isEnabled() {
    return enabled;
}

public MySaveResponseToFileBuilder setEnabled(boolean enabled) {
    this.enabled = enabled;
    return this;
}

public String getFileName() {
    return fileName;
}

public MySaveResponseToFileBuilder setFileName(String fileName) {
    this.fileName = fileName;
    return this;
}

public String getVariableName() {
    return variableName;
}

public MySaveResponseToFileBuilder setVariableName(String variableName) {
    this.variableName = variableName;
    return this;
}

public ResultSaver build() {
    ResultSaver resultSaver = new ResultSaver();
    resultSaver.setName(this.getName());
    resultSaver.setComment(this.getComment());
    resultSaver.setEnabled(this.isEnabled());
    resultSaver.setVariableName(this.getVariableName());
    resultSaver.setFilename(this.getFileName());
    resultSaver.setProperty(TestElement.TEST_CLASS, ResultSaver.class.getName());
    resultSaver.setProperty(TestElement.GUI_CLASS, ResultSaverGui.class.getName());
    return resultSaver;
}

}

ResultSaver resultSaver = new MySaveResponseToFileBuilder().setName("Save Response to File").setFileName("/tmp/jmeter/${__samplerName()}").build();
4

1 回答 1

2

使用 JMeter 监听器来存储测试结果并不是最好的主意,因为:

  • 与 .jtl 结果文件相比,它们没有增加任何价值
  • 它们在每个采样器之后被触发,而 .jtl 结果文件子系统分批存储结果,减少磁盘 IO 负面影响
  • 由于它们的性质和以上几点,它们会消耗 CPU、RAM 和磁盘资源

因此,与其尝试添加侦听器,不如考虑在该standardJMeterEngine.configure(rootTree);行之前使用以下代码:

String jtlFile = "/path/to/results.jtl";
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
    summer = new Summariser(summariserName);
}
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(jtlFile );
rootTree.add(rootTree.getArray()[0], logger);

它将SummariserResultCollector添加到您的测试计划中,因此当您运行代码时,结果将存储在 .jtl 结果文件中,以后可以使用侦听器进行分析,或者您可以从中生成HTML 报告仪表板

于 2018-12-03T09:18:24.170 回答