In the JMeter GUI it is easy to add a listener such as 'View Results Tree' or 'View Results in Table' and add a file name
I have a method that creates and runs a test plan that looks like this
//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();
EDIT: I added the changes using ResultCollector and it successfully saved the file data.csv However, it only created csv column names but never created any results.
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);
My full output looks like this
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%)
Remove these lines from your code:
threadGroup.addTestElement(rc);
testPlanTree.add("rc", rc);
and substitute it with the following:
testPlanTree.add(testPlanTree.getArray()[0], rc);
Also be aware that if you leave this line:
rc.setErrorLogging(true);
you will have only errors in your .jtl results file.
I also fail to see any sampler executed so double check your test plan using following reference material:
User contributions licensed under CC BY-SA 3.0