问题描述
我正在尝试使用 ReportGenerator 使用 JMeter 创建一个 statistics.json 文件,其中填充了我的 .jmx 测试的结果。是否可以使用 JMeter 执行此操作?
我已经完成了本教程:https://jmeter.apache.org/usermanual/generating-dashboard.html,它侧重于使用报告生成器创建 html 仪表板,但我也有创建/更新 statstics.json 文件的项目要求。我已经使用 JSON Extractor 后处理器提取了必要的数据,并且我可以从该提取器中获取自定义变量以显示在我的调试响应和我的 CSV 文件中(在向 user.properties 添加一些 sample_variables 之后)。不幸的是,我未能找到有关如何使用这些响应创建 JSON 文件的更多信息。
在我的 reportgenerator.properties 文件中,我看到的与 json 相关的唯一部分是:
jmeter.reportgenerator.exporter.json.classname=org.apache.jmeter.report.dashboard.JsonExporter
jmeter.reportgenerator.exporter.json.property.output_dir=report-output
我正在寻找一些设置以允许我编辑该 JSON 文件中的内容,但我无法在文档中查找信息。我是否需要在另一个设置文件中发送或设置我的自定义变量?任何帮助澄清这一点将不胜感激!
解决方法
查看 JMeter source code,您无法有效地控制外部导出到 statistics.json 文件中的内容,您将不得不修改 JsonExporter 类代码或提出您自己的 AbstractDataExporter 实现并选择存储什么、在哪里以及如何存储。
private void createStatistic(Map<String,SamplingStatistic> statistics,MapResultData resultData) {
LOGGER.debug("Creating statistics for result data:{}",resultData);
SamplingStatistic statistic = new SamplingStatistic();
ListResultData listResultData = (ListResultData) resultData.getResult("data");
statistic.setTransaction((String) ((ValueResultData)listResultData.get(0)).getValue());
statistic.setSampleCount((Long) ((ValueResultData)listResultData.get(1)).getValue());
statistic.setErrorCount((Long) ((ValueResultData)listResultData.get(2)).getValue());
statistic.setErrorPct(((Double) ((ValueResultData)listResultData.get(3)).getValue()).floatValue());
statistic.setMeanResTime((Double) ((ValueResultData)listResultData.get(4)).getValue());
statistic.setMinResTime((Long) ((ValueResultData)listResultData.get(5)).getValue());
statistic.setMaxResTime((Long) ((ValueResultData)listResultData.get(6)).getValue());
statistic.setMedianResTime((Double) ((ValueResultData)listResultData.get(7)).getValue());
statistic.setPct1ResTime((Double) ((ValueResultData)listResultData.get(8)).getValue());
statistic.setPct2ResTime((Double) ((ValueResultData)listResultData.get(9)).getValue());
statistic.setPct3ResTime((Double) ((ValueResultData)listResultData.get(10)).getValue());
statistic.setThroughput((Double) ((ValueResultData)listResultData.get(11)).getValue());
statistic.setReceivedKBytesPerSec((Double) ((ValueResultData)listResultData.get(12)).getValue());
statistic.setSentKBytesPerSec((Double) ((ValueResultData)listResultData.get(13)).getValue());
statistics.put(statistic.getTransaction(),statistic);
}
更简单的选择是使用 Flexible File Writer
将示例变量写入单独的文件中 ,我要留下接受的答案,因为它是正确的。但是,我想补充一点,我能够通过使用 JSR223 后处理器编写一个 groovy 脚本来完成我的要求,该脚本在我需要的任何地方创建一个 csv 文件,并用我需要的任何数据填充它。