How to set CSV file in java code by running the Jmeter test using a program (Java Code)?

强颜欢笑 提交于 2020-03-03 03:54:08

问题


I have previously ask the question that how to log the results after running the jmeter test using a java program and then I have got this by myself so I am going to share the link of that question with answer for future references. So here is the link... How can I save a result set after running the Jmeter Test using a program (JAVA CODE)?

But now I have another question for this, How can I set a CSV file here with a multiple logins, I have tried some code but it not happened as I want. Can anyone please help me out from this ? And please see the following code that I have tried.

package com.solitera.automation.controller;

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();  

    HashTree testPlanTree = SaveService.loadTree(new File("D:/apache-jmeter-5.1.1/extras/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);  
    }  


    CSVDataSet csvDataSet = new CSVDataSet();
    csvDataSet.setName("CSV Data Set Config");
    csvDataSet.setProperty("delimiter",",password,submitLogin,userName");
    csvDataSet.setProperty("filename", "D:/apache-jmeter-5.1.1/extras/CSVData.csv");
    csvDataSet.setProperty("ignoreFirstLine", false);
    csvDataSet.setProperty("quotedData", false);
    csvDataSet.setProperty("recycle", true);
    csvDataSet.setProperty("shareMode", "shareMode.all");
    csvDataSet.setProperty("stopThread", false);
    csvDataSet.setProperty("variableNames", "foo");
    csvDataSet.setProperty(TestElement.TEST_CLASS, csvDataSet.getClass().getName());
    csvDataSet.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());

    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);  

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

Below is the images in which I have recorded a script using Blazemeter and add it to Jmeter GUI and please refer for the same for more information how my TestPlan actually looks like.

CSVData.csv file :

NOTE : This whole script I am trying to run through the Java code that I have shared above, If I run without CSV file with only one user login and set the No. of threads = 3 then the script runs fine.


回答1:


Just Remove everything from the "Variable Names" section of the CSV Data Set Config:

and your setup should start working as expected. Given you have "Ignore first line" set to False and the first line of your CSV file is the header, not the data you don't need to set any variable names there, JMeter will do it automatically.

You also don't need this CSVDataSet declaration/configuration in the code because:

  1. It is not configured properly
  2. It doesn't add any value as you're not adding it to the Test Plan

More information: Using CSV DATA SET CONFIG



来源:https://stackoverflow.com/questions/59841310/how-to-set-csv-file-in-java-code-by-running-the-jmeter-test-using-a-program-jav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!