Search code examples
csvjmeterautomated-testsperformance-testing

Programmatic JMeter configuration with CSV test data


I'm running JMeter programmatically from Java code and I'd like to generate a bunch of POST requests with a varying body. Here is a sampler I use to generate one request:

HTTPSamplerProxy sampler = new HTTPSamplerProxy();
sampler.setDomain("localhost");
sampler.setPort(8081);
sampler.setPath("/service");
sampler.setMethod("POST");
sampler.addEncodedArgument("body", "{\"key\": \"data\"}");
sampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());

How can I replace data in the body for each request with a value from a CSV file?

I know there is CSV Data Set Config plugin in GUI version but I haven't found a way to use it from Java code.


Solution

  • Given you're able to create the HTTP Request sampler using JMeter API you can take the similar steps to create the CSV Data Set Config. Just look into .jmx file and convert what you see there into Java code.

    Just in case if you are not able:

    CSVDataSet csvDataSet = new CSVDataSet();
    csvDataSet.setName("CSV Data Set Config");
    csvDataSet.setProperty("delimiter", ",");
    csvDataSet.setProperty("filename", "test.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());
    

    See Five Ways To Launch a JMeter Test without Using the JMeter GUI article for more information on various ways of kicking off a JMeter test including creating script from scratch in Java.


    If your test plan generation has to be programmatic but doesn't have to be Java you may find Taurus tool much easier to use as you will be able to create a test using declarative YAML syntax.