2

I have run the Jmeter Script using Jmeter dependency in eclipse using Java code, fortunately, my script is running fine but now I'm unable to store the result for the same. Can anyone please tell me how can I achieve this ? please see the following code that I have tried.

package com.solitera.automation.controller;

import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

import java.io.File;
import java.io.FileInputStream;

public class JMeterFromExistingJMX {

    public static void main(String[] argv) throws Exception {
        // JMeter Engine
        StandardJMeterEngine jmeter = new StandardJMeterEngine();


        // Initialize Properties, logging, locale, etc.
        JMeterUtils.loadJMeterProperties("D:/apache-jmeter-5.1.1/bin/jmeter.properties");
        JMeterUtils.setJMeterHome("D:/apache-jmeter-5.1.1");
        JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
        JMeterUtils.initLocale();

        // Initialize JMeter SaveService
        SaveService.loadProperties();

        // Load existing .jmx Test Plan
       /* FileInputStream in = new FileInputStream("D:/Ecllipse_project_workspace2/slt_automation/src/test/jmeter/slt_autoMa_Test.jmx");
        HashTree testPlanTree = SaveService.loadTree(in);
        in.close();*/

        HashTree testPlanTree = SaveService.loadTree(new File("D:/apache-jmeter-5.1.1/extras/slt_auto_test_java_blaze_script.jmx"));

        // Run JMeter Test
        jmeter.configure(testPlanTree);
        jmeter.run();
    }


}
Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24

2 Answers2

1

I have solved it by myself..

if (summariserName.length() > 0) {  
    summer = new Summariser(summariserName);  
  }  

  String logFile = "D:/apache-jmeter-5.1.1/extras/resultss.xml";
  ResultCollector logger = new ResultCollector(summer);  
  logger.setFilename(logFile);
  testPlanTree.add(testPlanTree.getArray()[0], logger); 
0

You need to add a ResultCollector instance to your Test Plan in order to get the .jtl file written like:

ResultCollector collector = new ResultCollector();
collector.setFilename("result.jtl");
testPlanTree.add(testPlanTree.getArray()[0], collector);

the code needs to be added before the jmeter.configure(testPlanTree); line

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Hi, Thank you for your reply, I am now able to print the logs in .jtl file, but now I have to do multiple logins using CSV file that I have shared my tried code above. Can you please tell me where I'm wrong in that code or what code I have to add to achieve the same. –  Jan 21 '20 at 11:49
  • Just add a [CSV Data Set Config](https://guide.blazemeter.com/hc/en-us/articles/206733689-Using-CSV-DATA-SET-CONFIG) to your `D:/apache-jmeter-5.1.1/extras/slt_auto_test_java_blaze_script.jmx` script and perform the configuration using GUI, once done you should be able to normally run the test from Java code. If you're looking for a way to add a CSV Data Set Config to the existing test programmatically it's better to ask a new question as comment field is too limited in order to provide the comprehensive answer. Most probably you will need to share your Test Plan as well – Dmitri T Jan 21 '20 at 11:58
  • I have created an another question, Please refer to the same, and please give me the solution as I am struggle to get this from the past 3 days. –  Jan 21 '20 at 12:41