我已经创建了简单的 JMeter 测试,但我不知道如何生成某种 JMeter 报告并将其保存到某个文件中(摘要报告、查看表中的结果等)。
我的代码如下所示:
public class JMeterTest extends BaseScenario{
protected void scenario() throws Exception {
// Engine
StandardJMeterEngine jm = new StandardJMeterEngine();
// jmeter.properties
JMeterUtils.loadJMeterProperties("D:/Java Tools/JMeter/apache-jmeter-2.11/apache-jmeter-2.11/bin/jmeter.properties");
HashTree hashTree = new HashTree();
// HTTP Sampler
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain("192.168.67.98");
httpSampler.setPort(8080);
httpSampler.setPath("/gks-server");
httpSampler.setMethod("GET");
// Loop Controller
TestElement loopCtrl = new LoopController();
((LoopController)loopCtrl).setLoops(1);
((LoopController)loopCtrl).addTestElement(httpSampler);
((LoopController)loopCtrl).setFirst(true);
// Thread Group
SetupThreadGroup threadGroup = new SetupThreadGroup();
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController((LoopController)loopCtrl);
TestPlan testPlan = new TestPlan("MY TEST PLAN");
testPlan.setTestPlanClasspath("D:/JMeterTestPlan/test.jmx");
hashTree.add("testPlan", testPlan);
hashTree.add("loopCtrl", loopCtrl);
hashTree.add("threadGroup", threadGroup);
hashTree.add("httpSampler", httpSampler);
jm.configure(hashTree);
//Run TEST
jm.run();
}
}
在
jm.run()
语句之前添加以下几行:
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");//$NON-NLS-1$
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
String logFile = "/path/to/output/file.jtl";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(logFile);
hashTree.add(hashTree.getArray()[0], logger);
所有输出都将以 CSV 格式发送到
/path/to/output/file.jtl
。您可以查看 JMeter 安装的 /bin 文件夹中的 jmeter.properties
文件,以确定需要哪些“有趣”值并适当地覆盖它们。
可用选项有:
jmeter.save.saveservice.output_format=csv
jmeter.save.saveservice.assertion_results_failure_message=false
jmeter.save.saveservice.assertion_results=none
jmeter.save.saveservice.data_type=true
jmeter.save.saveservice.label=true
jmeter.save.saveservice.response_code=true
jmeter.save.saveservice.response_data=false
jmeter.save.saveservice.response_data.on_error=false
jmeter.save.saveservice.response_message=true
jmeter.save.saveservice.successful=true
jmeter.save.saveservice.thread_name=true
jmeter.save.saveservice.time=true
jmeter.save.saveservice.subresults=true
jmeter.save.saveservice.assertions=true
jmeter.save.saveservice.latency=true
jmeter.save.saveservice.samplerData=false
jmeter.save.saveservice.responseHeaders=false
jmeter.save.saveservice.requestHeaders=false
jmeter.save.saveservice.encoding=false
jmeter.save.saveservice.bytes=true
jmeter.save.saveservice.url=false
jmeter.save.saveservice.filename=false
jmeter.save.saveservice.hostname=false
jmeter.save.saveservice.thread_counts=false
jmeter.save.saveservice.sample_count=false
jmeter.save.saveservice.idle_time=false
jmeter.save.saveservice.timestamp_format=ms
jmeter.save.saveservice.timestamp_format=yyyy/MM/dd HH:mm:ss.SSS
jmeter.save.saveservice.default_delimiter=,
jmeter.save.saveservice.default_delimiter=\t
jmeter.save.saveservice.print_field_names=false
jmeter.save.saveservice.xml_pi=<?xml-stylesheet type="text/xsl" href="../extras/jmeter-results-detail-report_21.xsl"?>
jmeter.save.saveservice.base_prefix=~/
jmeter.save.saveservice.autoflush=false
请参阅不使用 JMeter GUI 启动 JMeter 测试的 5 种方法指南,了解有关如何从 Java 代码运行 JMeter 测试等的详细信息。
当文件小于 100MB、较大的文件需要花费大量时间来处理或者它们不被 xpath 处理时,此报告就可以了。
更好的解决方案是使用 jtl 文件和 CMDRunner.ext 创建不同的文件。
通过此功能,您可以使用 cmdrunner 为特定服务轻松创建不同的图表: http://performancewebautoamtionother.blogspot.si/2015/12/jmeter-create-graphs-with-cmdrunner.html