我是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中。

最佳答案

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

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

09-28 14:56