JMeter save results to file

1

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

enter image description here

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();
  1. is there a convenient premade test element to simple add to the thread group and save the results straight to a file (in the same way the 'View Results in Table' listener does from the gui).
  2. what does implementing a listener that would do this task look like

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%)
java
jmeter
asked on Stack Overflow Apr 11, 2018 by Grayden Hormes • edited Apr 16, 2018 by Grayden Hormes

1 Answer

1

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:

answered on Stack Overflow Apr 16, 2018 by Dmitri T

User contributions licensed under CC BY-SA 3.0