Using CSV parameter files in JMeter

Updated at:

In native stress testing with Performance Testing Service (PTS), you can use parameterization to build dynamic business requests. When using JMeter for stress testing, you can address this need by adding a CSV Data Set Config Config Element to your JMeter script to read parameters from a CSV file. PTS also supports splitting CSV files and distributing the data to each stress testing engine.

Background

CSV Data Set Config is a JMeter Config Element that reads data from a CSV file and splits the data into variables. It is ideal for scenarios with a large number of variables.

If you upload a CSV file without a corresponding CSV Data Set Config in the JMeter script, PTS splits the file by default as if it lacks a header row. To simplify operations and prevent incorrect data splitting, we recommend always configuring a CSV Data Set Config for your CSV parameter files.

Configure a CSV parameter file

To build dynamic stress test requests, you need to configure a CSV file in your JMeter script.

For example, to test the performance of a website's login API with 100 concurrent users, you need to create 100 different sets of user login data. Follow these steps:

  1. Create a local CSV file that contains data for 100 users. In this example, the first row of the CSV file is a header specifying the variable names, so the file has 101 rows. The following example shows the basic format:
    id,name
    1,ali
    2,pts
    3,jmeter                     
    Note If the CSV file does not have a header, you must configure the variable names in the CSV Data Set Config.
  2. Start the JMeter GUI, right-click the Test Plan, and select Add > Threads (Users) > Thread Group to add a thread group.
    This action is performed in the Test Plan tree on the left side of JMeter. The Thread Group then appears as a child node of the Test Plan.
  3. Right-click the new Thread Group and select Add > Config Element > CSV Data Set Config.
    In addition to CSV Data Set Config, the Config Element submenu includes other elements, such as HTTP Header Manager.
  4. In the CSV Data Set Config panel, enter the name of the CSV file and leave the other parameters at their default settings.

    JMeter automatically reads the parameter names from the CSV file header and uses them as variable names.

    Note If the CSV file does not have a header, enter id,name in the Variable Names (comma-delimited) field of the CSV Data Set Config.
  5. Right-click the Thread Group and select Add > Sampler > HTTP Request. Configure the sampler based on your specific business API. The following is an example:
    • Name: Login API example.
    • Web Server: Enter the protocol, server name or IP address, and port number for your business API.
    • HTTP Request: Select the request method and enter the Path and Content encoding.
    • On the Body Data tab, use ${id} and ${name} to reference the variables from the CSV file.
    In this example, the Protocol is https, the Port Number is 443, the request method is POST, and the Content encoding is UTF-8. On the Body Data tab, the CSV parameters are referenced in JSON format as follows: {"userid":${id}, "name":${name}}.
  6. Save the JMeter Test Plan script.
  7. Upload the test script and the CSV file to PTS. For more information, see Create a JMeter scenario.
    Select the Split file checkbox for the CSV file to distribute its data across the stress testing engines. For more details, see the next section, CSV file splitting. On the Scene Configuration page, the file list shows information such as the filename, upload status, and file type. For CSV files, a checkbox is available in the Split file column. You can also configure parameters such as Use Dependency and JMeter Version.

CSV file splitting

PTS automatically allocates JMeter stress testing engines based on the number of concurrent users that you configure. When multiple engines are used, PTS can split a CSV file and distribute its data evenly across the stress testing engines.

If the number of data rows cannot be divided evenly by the number of engines, some engines may receive one more data row than others.

Splitting a CSV file with a header

When the original CSV file has a header, each split file also contains the header, but the data rows are not duplicated.

For example, if the sample file is split across two engines:

  • File 1 contains:

    id,name
    1,ali
    2,pts                    
  • File 2 contains:

    id,name
    3,jmeter                   

Splitting a CSV file without a header

If a CSV file lacks a header and its first row contains data, a file named "b.csv", for example, contains the following:

1,ali
2,pts
3,jmeter          

You must configure the variable names in JMeter.

In the CSV Data Set Config panel, set the Filename to the path of the headerless CSV file, such as b.csv, and manually enter the variable names, such as id,name, in the Variable Names (comma-delimited) field.

In this case, if you choose to split the CSV file, the resulting split files do not contain a header. For example, if the file is split across two engines:

  • File 1 contains:
    1,ali
    2,pts
                        
  • File 2 contains:
    3,jmeter
                        

More information

For more information about how to configure and use CSV files, see the JMeter official documentation.