在JMeter GUI中,可以轻松添加监听器,例如“查看结果树”或“在表中查看结果”并添加文件名
我有一个创建并运行看起来像这个
的测试计划的方法//JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.loadJMeterProperties("/path/to/apache-jmeter-4.0/bin/jmeter.properties");
JMeterUtils.setJMeterHome("/path/to/apache-jmeter-4.0");
JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
// JMeter Test Plan, basic all u JOrphan HashTree
HashTree testPlanTree = new HashTree();
// HTTP Sampler
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain("example.com");
httpSampler.setPort(80);
httpSampler.setPath("/");
httpSampler.setMethod("GET");
// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.addTestElement(httpSampler);
loopController.setFirst(true);
loopController.initialize();
// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
// Construct Test Plan from previously initialized elements
testPlanTree.add("testPlan", testPlan);
testPlanTree.add("loopController", loopController);
testPlanTree.add("threadGroup", threadGroup);
testPlanTree.add("httpSampler", httpSampler);
// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
编辑:我使用ResultCollector添加了更改并成功保存了文件data.csv但是,它只创建了csv列名但从未创建任何结果。
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");//$NON-NLS-1$
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
ResultCollector rc = new ResultCollector(summer);
rc.setFilename("C:\\Users\\user\\data.csv");
rc.setErrorLogging(true);
threadGroup.addTestElement(rc);
testPlanTree.add("rc", rc);
我的完整输出看起来像这样
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
Apr 16, 2018 1:40:32 PM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5. summary = 0 in 00:00:00 = ******/s Avg: 0 Min: 9223372036854775807 Max: -9223372036854775808 Err: 0 (0.00%)`
summary = 0 in 00:00:00 = ******/s Avg: 0 Min: 9223372036854775807 Max: -9223372036854775808 Err: 0 (0.00%)
答案 0 :(得分:1)
从代码中删除这些行:
threadGroup.addTestElement(rc);
testPlanTree.add("rc", rc);
并将其替换为以下内容:
testPlanTree.add(testPlanTree.getArray()[0], rc);
另请注意,如果你离开这一行:
rc.setErrorLogging(true);
您的.jtl结果文件中只有仅错误。
我也没有看到任何采样器被执行,因此请使用以下参考资料仔细检查您的测试计划: