简体   繁体   中英

How to use a CSV-file to fill parameters into HttpSampler in JMeter?

I'm new to Spring and JMeter and I'm trying to test the performance of my service with JMeter. A site is running on server, which accepts two parameters (username and value). I want to fill these two parameters with data from a CSV-file.

I've got the following class:

public class JMeter {

  public static Logger logger = LoggerFactory.getLogger(JMeter.class);

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

    //JMeter initialization (properties, log levels, locale, etc)
    JMeterUtils.loadJMeterProperties("target/jmeter/bin/jmeter.properties");
    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 hashTree = new HashTree();

    String csvFilePath = "src/test/jmeter/resources/userdata.csv";
    CSVDataSet csvDataSet = createAndConfigureCsvDataSet(csvFilePath);
    HTTPSampler httpSampler = createAndConfigureHttpSampler();
    LoopController loopController = createAndConfigureLoopController(httpSampler);
    ThreadGroup threadGroup = createAndConfigureThreadGroup(loopController);
    TestPlan testPlan = new TestPlan("HttpTest");

    // Construct Test Plan from previously initialized elements
    hashTree.add("testPlan", testPlan);
    hashTree.add("loopController", loopController);
    hashTree.add("threadGroup", threadGroup);
    hashTree.add("httpSampler", httpSampler);
    hashTree.add("csvDataSet", csvDataSet);

    // Run Test Plan
    standardJMeterEngine.configure(hashTree);
    standardJMeterEngine.run();
  }

  private static CSVDataSet createAndConfigureCsvDataSet(String csvFilePath) {
    // CSV Data Set
    File file = new File(csvFilePath);
    if (file.exists()) {
      logger.debug("CSV-File found: {}", file.getAbsoluteFile());
    } else {
      logger.debug("CSV-File NOT found: {}", file.getAbsoluteFile());
    }
    CSVDataSet csvDataSet = new CSVDataSet();
    csvDataSet.setFilename(csvFilePath);
    csvDataSet.setVariableNames("USERNAME,VALUE");
    csvDataSet.setDelimiter(";");
    csvDataSet.setQuotedData(false);
    csvDataSet.setRecycle(true);
    csvDataSet.setStopThread(false);
    return csvDataSet;
  }

  private static LoopController createAndConfigureLoopController(HTTPSampler httpSampler) {
    LoopController loopController = new LoopController();
    loopController.setLoops(1);
    loopController.addTestElement(httpSampler);
    loopController.setFirst(true);
    loopController.initialize();
    return loopController;
  }

  private static HTTPSampler createAndConfigureHttpSampler() {
    HTTPSampler httpSampler = new HTTPSampler();
    httpSampler.setDomain("localhost");
    httpSampler.setPort(8080);
    httpSampler.setPath("/userinfo");
    httpSampler.setMethod("GET");
    httpSampler.setContentEncoding("UTF-8");
    httpSampler.setFollowRedirects(true);
    httpSampler.setUseKeepAlive(true);
    httpSampler.addArgument("username", "USERNAME");
    httpSampler.addArgument("value", "VALUE");
    return httpSampler;
  }

  private static ThreadGroup createAndConfigureThreadGroup(LoopController loopController) {
    ThreadGroup threadGroup = new ThreadGroup();
    threadGroup.setNumThreads(20);
    threadGroup.setRampUp(1);
    threadGroup.setSamplerController(loopController);
    threadGroup.initialize();
    return threadGroup;
  }
}

But I can't figure out how to put the data from my csvDataSet into my httpSampler.

To refer the variables CSV dataset captures values into, use ${variable_name} pattern.

Thus, in your case "${USERNAME}" and "${VALUE}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM