2

I have successfully run the Jmeter test using a program i.e written in java code. And I am sharing the link of that question's solution here as well => How to set CSV file in java code by running the Jmeter test using a program (Java Code)?

Now, I got the results in my CSV file but the problem in that file is, it is not showing the column names, it just fetched the results and store it into the file. Please refer to the below picture for more information.

enter image description here

Can anyone please help me how can I store the results also with its column names ??

My java Code is :

package com.automation;
import org.apache.jmeter.engine.StandardJMeterEngine;  
import org.apache.jmeter.reporters.ResultCollector;  
import org.apache.jmeter.reporters.Summariser;  
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testbeans.gui.TestBeanGUI;
import org.apache.jmeter.testelement.TestElement;
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 {  

    //Set jmeter home for the jmeter utils to load  
    String jmeterHomelocation = "D:/apache-jmeter-5.1.1";  
    String jmeterPropertieslocation = jmeterHomelocation + "/bin/jmeter.properties";  

    // JMeter Engine  
    StandardJMeterEngine jmeter = new StandardJMeterEngine();  


    // Initialize Properties, logging, locale, etc.  
    JMeterUtils.loadJMeterProperties(new File(jmeterPropertieslocation).getPath());  
    JMeterUtils.setJMeterHome(new File(jmeterHomelocation).getPath());  
    // you can comment this line out to see extra log messages of i.e. DEBUG level  
    JMeterUtils.initLogging();  
    JMeterUtils.initLocale();  

    // Initialize JMeter SaveService  
    SaveService.loadProperties();  

   /* // Load existing .jmx Test Plan  
    FileInputStream in = new FileInputStream(new File(jmeterHomelocation + "extras/slt_autoMa_Test.jmx"));  
    HashTree testPlanTree = SaveService.loadTree(in);  
    in.close(); */ 

    HashTree testPlanTree = SaveService.loadTree(new File("D:/test-root/SLT/JmeterFiles/slt_auto_test_java_blaze_script.jmx"));

    Summariser summer = null;    
    String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");  

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

    String logFile = "D:/test-root/SLT/JmeterFiles/resultss.csv";
    ResultCollector logger = new ResultCollector(summer);  
    logger.setFilename(logFile);
    testPlanTree.add(testPlanTree.getArray()[0], logger);  

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

1 Answers1

1

Open D:/apache-jmeter-5.1.1/bin/jmeter.properties file and look for jmeter.save.saveservice.print_field_names line.

In order to have header line in the .jtl results file the the property needs to be set to true like:

jmeter.save.saveservice.print_field_names=true

Make sure that there is no # sing before the property declaration.

References:

You will need to delete the previous instance of the D:/test-root/SLT/JmeterFiles/resultss.csv as if it will be not empty - JMeter will not add the header, it will just append the new results there.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • # The commented-out values are defaults. #jmeter.save.saveservice.print_field_names=true``` They are saying in Jmeter.properties file, the # values are the default ones, So this `jmeter.save.saveservice.print_field_names=true` is also by default isn't it ?? I tried to remove the # and then run my script and the results are stored in the same way as the previous one. So what's the problem in this file ?? –  Jan 23 '20 at 05:13