'Jmeter result file is not getting generated when executed test on slave from java code

I am running Jmeter performance test from java application in distributed mode (2 slaves + master). In my test script I have configured Summary Report to store result data to csv file. This file location is configured with fixed name "result/summary.csv" value but the csv result file is not getting generated.

Result file is created when the test is run only on one master but not when using master + slave combination

public static void main(String[] argv) throws Exception {

    File jmeterHome = new File(System.getProperty("jmeter.home"));
    String slash = System.getProperty("file.separator");

    if (jmeterHome.exists()) {
        File jmeterProperties = new File(jmeterHome.getPath() + slash + "bin" + slash + "jmeter.properties");
        if (jmeterProperties.exists()) {
            //JMeter Engine
            DistributedRunner distributedRunner = new DistributedRunner();


            //JMeter initialization (properties, log levels, locale, etc)
            JMeterUtils.setJMeterHome(jmeterHome.getPath());
            JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
            JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
            JMeterUtils.initLocale();

            // JMeter Test Plan, basically JOrphan HashTree
            HashTree testPlanTree = new HashTree();

            // First HTTP Sampler - open example.com
            HTTPSamplerProxy examplecomSampler = new HTTPSamplerProxy();
            examplecomSampler.setDomain("example.com");
            examplecomSampler.setPort(80);
            examplecomSampler.setPath("/");
            examplecomSampler.setMethod("GET");
            examplecomSampler.setName("Open example.com");
            examplecomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
            examplecomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());


            // Second HTTP Sampler - open blazemeter.com
            HTTPSamplerProxy blazemetercomSampler = new HTTPSamplerProxy();
            blazemetercomSampler.setDomain("blazemeter.com");
            blazemetercomSampler.setPort(80);
            blazemetercomSampler.setPath("/");
            blazemetercomSampler.setMethod("GET");
            blazemetercomSampler.setName("Open blazemeter.com");
            blazemetercomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
            blazemetercomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());


            // Loop Controller
            LoopController loopController = new LoopController();
            loopController.setLoops(1);
            loopController.setFirst(true);
            loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
            loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
            loopController.initialize();

            // Thread Group
            ThreadGroup threadGroup = new ThreadGroup();
            threadGroup.setName("Example Thread Group");
            threadGroup.setNumThreads(1);
            threadGroup.setRampUp(1);
            threadGroup.setSamplerController(loopController);
            threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
            threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

            // Test Plan
            TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
            testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
            testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
            testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

            // Construct Test Plan from previously initialized elements
            testPlanTree.add(testPlan);
            HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
            threadGroupHashTree.add(blazemetercomSampler);
            threadGroupHashTree.add(examplecomSampler);

            // save generated test plan to JMeter's .jmx file format
            SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + slash + "example.jmx"));

            //add Summarizer output to get test progress in stdout like:
            // summary =      2 in   1.3s =    1.5/s Avg:   631 Min:   290 Max:   973 Err:     0 (0.00%)
            Summariser summer = null;
            String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
            if (summariserName.length() > 0) {
                summer = new Summariser(summariserName);
            }


            // Store execution results into a .jtl file
            String logFile = jmeterHome + slash + "example.jtl";
            
            // Store execution results into a .csv file
            //String logFile = jmeterHome + slash + "csv.jtl";

            ResultCollector logger = new ResultCollector(summer);
            logger.setFilename(logFile);
            testPlanTree.add(testPlanTree.getArray()[0], logger);
            
            ArrayList<String> remoteHostList = new ArrayList<String>();
            remoteHostList.add("0.0.0.127");
            remoteHostList.add("0.0.0.128");
            
            List<JMeterEngine> engines = new LinkedList<JMeterEngine>();
            distributedRunner.setStdout(System.out);
            distributedRunner.setStdErr(System.err);
            distributedRunner.init(remoteHostList, testPlanTree);
            engines.addAll(distributedRunner.getEngines());
            distributedRunner.start();
      
            System.out.println("Test completed. See " + jmeterHome + slash + "example.jtl file for results");
            System.out.println("JMeter .jmx script is available at " + jmeterHome + slash + "example.jmx");
            System.exit(0);

        }
    }
    System.exit(1);
}


Solution 1:[1]

When you're running JMeter in distributed mode the master node:

  1. Sends .jmx test plan to the slaves
  2. Collects results from the slaves and merges then into a single .jtl results file

It's hard to say what's wrong without seeing your code, but my expectation is that you need to use ClientJMeterEngine instead of StandardJMeterEngine

Solution 2:[2]

Adding below code helped me to resolve the issue.

 String logFile = jmeterHome + slash + "example.csv";
ResultCollector resultCollector = new ResultCollector()
resultCollector.setProperty(TestElement.TEST_CLASS, ResultCollector.class.getName());
resultCollector.setProperty(TestElement.GUI_CLASS, "SummaryReport");
resultCollector.setFilename(logFile);

testPlanTree.add(testPlanTree.getArray()[0], logger);

Test class and GUI class was not added before, adding it worked me. Now I can download the result from both master and slave machines.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitri T
Solution 2 swati chiniwal