从Java代码执行时,JMeter未将结果保存到文件中

时间:2018-11-30 21:43:48

标签: java api jmeter jmeter-5.0

当我从gui调用jmeter test时,它可以正常运行,但是当我调用 run runTests 是标准JMeter Engine的功能,它不会在 / tmp / jmeter 中保存任何内容。

我该如何实现?如何从保存在“将响应保存到文件侦听器”中的Java代码中执行和获取api有效负载。

我正在使用JMeter 5.0

discriminated unions

Builder类

    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;
}

1 个答案:

答案 0 :(得分:3)

Using JMeter listeners to store the test results is not the best idea为:

  • 与.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添加到Test Plan中,因此在运行代码时,结果将存储在.jtl结果文件中,以后可以使用{{ 3}},或者您可以从中生成Listeners