如何使用CSV文件将参数填充到JMeter中的HttpSampler中?

时间:2017-04-04 15:29:02

标签: java api csv jmeter performance-testing

我是Spring和JMeter的新手,我正在尝试使用JMeter测试我的服务性能。 站点正在服务器上运行,该站点接受两个参数(用户名和值)。我想用CSV文件中的数据填充这两个参数。

我有以下课程:

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

但我无法弄清楚如何将我的csvDataSet中的数据放入我的httpSampler。

1 个答案:

答案 0 :(得分:1)

要引用变量CSV数据集捕获值,请使用${variable_name}模式。

因此,在您的情况下"${USERNAME}""${VALUE}"