当我从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;
}
答案 0 :(得分:3)
Using JMeter listeners to store the test results is not the best idea为:
因此,与其尝试添加侦听器,不如考虑在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);
它将Summariser和ResultCollector添加到Test Plan中,因此在运行代码时,结果将存储在.jtl结果文件中,以后可以使用{{ 3}},或者您可以从中生成Listeners