CSV Data Set Config in Sharing Mode - Made Easy
May 18, 2021

CSV Data Set Config in Sharing Mode - Made Easy

Open Source Automation
Test Data Management

To make performance testing more realistic, it's best practice not to employ the same user in each thread group. Therefore, you need a way to configure Apache JMeter™ to use different users with unique user data, and to come up with an idea about how you can effectively store users’ data that will be used during the test run.

For this purpose, JMeter has a very useful configuration element called the JMeter CSV Data Set Config. The “CSV Data Set Config” enables using CSV files as an external data source, where you can keep unique user data like names, emails and passwords. With the help of this config element, JMeter is able to read the CSV file line by line, and then use split parameters to allocate different values for different threads.

At first glance, all of the  “CSV Data Set Config” parameters seem to be self-explanatory. But one of these might not be as straightforward as you might think. This parameter is “Sharing Mode”. This blog post will explain everything you need to know about this option.

Back to top

Understanding the JMeter CSV Dataset Config Element

But first things first. To understand the meaning of the “Sharing Mode” parameter, we need to first of all go through the “CSV Data Set Config” element, and write a test script that we can use as an example. As we are going to try all the “Sharing Mode” parameter options, we have to write performance scripts that contain several thread groups. For the test, you are going to use the http://blazedemo.com/ web app, which has user registration and login functionality. 

Initially, let’s create at least 10 users in http://blazedemo.com/ which you are going to use further. If you are too busy to create 10 users manually, you can create a script to automate the process.

Multiple Users With One Request

Now it’s time to create a performance script that shows different sharing options in action.

1. Create a simple script with one ‘Thread Group’ for 5 users, default ramp-up period and a loop count.

learn jmeter csv data set config sharing mode

2. Add an HTTP Request to perform the login action.

jmeter csv data set config guide

3. To ensure that our script is working as expected, let’s add the ‘View Results Tree’ listener (Right click on ‘Thread Group’ -> Add -> Listener -> View Results Tree) and run the script.

You will see that all 5 users from the ‘Thread Group’ have executed a login with the same email and password. This happened because these values are specified in the request sampler and they are relevant for all 5 users from that thread group:

sharing mode thread group csv data set config jmeter

Multiple Users With Multiple Requests

But what if you want to login under different users? To simulate a real web application load, you probably want to use different users with different credentials. For this purpose, JMeter has the ‘CSV Data Set Config’ element, which is designed to help with such situations.

The main idea of this element is that it reads a CSV file line by line and provides its values to threads of the current Thread group. There are many useful articles and hands-on sessions which explain this JMeter element through different examples. For example, read this article to understand the basic principles of the component. But for us, it is important to show how easily we can use this config element to perform logins under different users. Then, we will be able to move on and explain the meaning of different sharing mode values.

But first of all, let’s create a simple CSV file that contains a list of users (each user on a separate line), with an email and password separated by a comma on each line:

➜  less users.csv


Now that the CSV file with our users is created, you are ready to move on.

Back to top

CSV Data Set Config Sharing Mode

‘All threads’ Sharing Mode

Let’s add the ‘CSV Data Set Config’ element to our performance script (Right click on ‘Test Plan’ -> Add -> Config Element -> CSV Data Set Config). In this element, you will need to specify the filename that contains user details and variable names, which will be used as containers for these values fetched from the file.

Keep in mind that there are two options: you need to specify the full path to the CSV file or you can just use a filename. But in this case, the file should be located in the same folder as the JMX performance script. This time you are going to use the default sharing mode: All threads.

jmeter multiple threads csv data set config

After rerunning our script, you can see that all users took emails and passwords respectively based on the order and line number:

  • The first user used the email and password from the first line
  • Second used the email and password from the second line
  • …..

csv data set config jmeter sharing mode

jmeter sharing mode

The ‘All threads’ sharing mode means that the file will be shared between all threads and each request will read one line in the CSV file, in sequential order. This way is default for ‘CSV Data Set Config’. It will look like this:

jmeter csv data set config sharing mode thread groups all threads

‘Current thread’ Sharing Mode

The ‘Current thread’ sharing mode should be used when we want each thread to open and read the CSV file separately. In this case, each user will read the CSV file from beginning to end. Let’s change the sharing mode to ‘Current thread’ value and verify how the performance script behaves this time.

current thread sharing mode jmeter

current thread sharing mode csv data set config jmeter

jmeter sharing mode thread groups csv data set config

As you can see, this time all threads are trying to run the login request under the same user. The reason is simple. As each thread reads the file separately from the beginning until the end, all 5 threads of our thread group performed a login request with the values of the first line.

Currently, you had only 1 loop configured in the thread group. If you want to ensure that for the next loop each thread will read the second line, let’s update our thread group with the ‘2’ value in ‘Loop Count’.

jmeter csv data set config

Since you have 5 threads and 2 loops, there should be 10 requests in total. The first 5 requests should perform a login action under the first user (the first line of the CSV file), while the next 5 requests should execute a login action under the second user (the second line of the CSV file).

Let’s run the script and confirm that. But before, it might be useful to add a ‘Constant Timer’ in the current thread group. Since you want to show that both loops use different lines from the csv file, you need to avoid a situation where the first thread executes the first loop and starts the second while the second thread just started the first loop execution. In such a case, the requests in the ‘View Results Tree’ listener would be mixed. Let’s add a 2 seconds timeout for that (Right click on ‘Test Group’ -> Add -> Timer -> Constant Timer).

jmeter thread groups sharing mode

Now we can run the test and verify how the CSV file is used this time.

csv data set config sharing mode on jmeter

jmeter csv data set config sharing mode

thread group sharing csv data set config

jmeter data set config

As you can see, the first 5 requests use the first line of the CSV file, while the second 5 requests use the second line. It looks like this:

jmeter ‘Current thread’ Sharing Mode

‘Current thread group’ Sharing Mode

With the ‘Current thread group’ sharing mode, each file is opened by each thread group separately (not to be confused with the “Current thread” where each thread reads the file separately. This basically means that each of the threads in each thread group reads the CSV file from beginning to end without any impact on each other.

To show this sharing mode in action, you need to create one more thread group. You can just copy paste the existing one and rename the thread group and login sampler appropriately.

jmeter current thread group sharing mode

Then, all you need is to change ‘Sharing mode’ in ‘CSV Data Set Config’ to ‘Current thread group’:

cyrrent thread group sharing mode csv data set config

After you run the script, you can find that each thread group uses the CSV file separately:

‘Current thread group’ Sharing Mode, jmeter

csv data set config jmeter

The workflow graph for this case seems more complicated, but the main ideas looks like this:

csv data set config sharing mode workflow

‘Edit’ Sharing Mode

This sharing mode usually brings a lot of confusion for many JMeter users. When you choose the ‘Edit’ option under the ‘Sharing mode’ config element, this field transforms to input. Then, you can provide your own string into this field instead of using a predefined one.

The main idea of this sharing mode is that you can use the same CSV file in multiple thread groups, but only specified thread groups will share that file between each other. Seems a bit complicated, right? Let’s show an example to show this in action.

First of all, you need to add one additional thread group (you can just copy paste in the same way we did in the previous paragraph) and this time let’s copy paste the ‘CSV Data Set Config’ element under each of the thread groups:

jmeter, edit sharing mode

Now you need to configure each ‘CSV Data Set Config’ in this way:

  • Thread Group 1: ‘Sharing mode’ = ‘SharedWithThreadGroup1and3’
  • Thread Group 2: ‘Sharing mode’ = ‘All threads’
  • Thread Group 3: ‘Sharing mode’ = ‘SharedWithThreadGroup1and3’

An example of ‘Thread Group 1’ configuration:

jmeter csv data set config edit sharing mode

In this case, the value for the first and the third thread groups should match each other. Such configurations tell JMeter to share one CSV file between the two, while the second thread group will use its own copy of that CSV. Let’s run the script again and verify the ongoing requests one more time:

First Thread Group:

edit sharing mode thread groups

Second Thread Group:

jmeter csv data set config edit sharing mode

Third Thread Group:

three thread groups jmeter sharing mode

As you can see, the first two thread groups start reading the CSV file from the beginning and use the first line, while the third thread group doesn’t. As the third thread group has the same value of ‘Sharing mode’ with the first thread group, it continues to read the file from the line which the first thread group ended (the first line in the provided example).

By providing this sharing mode ‘CSV Data Set Config’ element provides you great flexibility to control CSV file sharing in any way you want. This current mode can be visualized like this:

multiple thread groups csv data set config sharing mode jmeter

As you can see, the ‘CSV Data Set Config’ is very configurable and provides great flexibility, so you can always control CSV files usage based on your needs. In this article we tried to show you different ways to apply this configuration, by using different sharing mode options.

Back to top

Saving Data to CSV Files With Java Through JMeter

Creating CSV files with Java through Apache JMeter™ is a convenient and easy way to form and to update your CSV files. Instead of creating the CSV file separately, you can complete your whole work process in one place - in JMeter.

Let’s get started.

1. First add a Thread Group

Right click on Test Plan -> Threads -> Thread Group

2. Add an element that enables you to write a code in Java, i.e a BeanShell element. This can be a Sampler, PreProcessor or PostProcessor, according to your script needs. In this example we will use a sampler.

Right click on Thread Group -> Sampler -> BeanShell Sampler

This is where you add the Java code with all the relevant parameters for the CSV file. The code executes writing to a CSV file with the variables in it.

update CSV files in JMeter with java

This is the code I used. This code writes the parameters that you set, to a CSV file. Every time the sampler is executed, one more line is added to the CSV file.

importjava.io.FileWriter;importjava.util.Arrays;importjava.io.Writer;importjava.util.List;//Default separatorcharSEPARATOR=',';//function write line in csvpublicvoidwriteLine(FileWriterwriter,String[]params,charseparator){booleanfirstParam=true;StringBuilderstringBuilder=newStringBuilder();Stringparam="";for(inti=0;i<params.length;i++){//get paramparam=params[i];log.info(param);//if the first param in the line, separator is not neededif(!firstParam){stringBuilder.append(separator);}//Add param to linestringBuilder.append(param);firstParam=false;}//prepare file to next linestringBuilder.append("\n");//add to file the linelog.info(stringBuilder.toString());writer.append(stringBuilder.toString());}//get path of csv file (creates new one if its not exists)StringcsvFile="";// for example '/User/Downloads/blabla.csv'String[]params={${param1},${param2},${param3}};FileWriterfileWriter=newFileWriter(csvFile,true);writeLine(fileWriter,params,SEPARATOR);//proper close to filefileWriter.flush();fileWriter.close();


If you want to use it, you can adjust the following lines:

  • Change the parameters here, and add more if you need to:
  • If you run this script a few times, “true” will append new lines to the file. To override the existing lines, change to “false”. This is an optional part of the code.


4. Add a BeanShell element that will read the entire CSV file and set each parameter to a variable so they can be used later on in the script.

Here is an optional script for reading the entire file, you can create your own.



Let's explain some of the lines:

  • The following line goes through each line in the CSV file and reads it.


  • The following defines the array of the CSV line's params.


  • The following part sets a new JMeter variable for each parameter in the line. You can define the parameters according to your needs and decide what to do with them in JMeter.


That’s it! Now you can incorporate this configuration into your entire script, and integrate the CSV file reading and writing into your complete test flow.

That’s it! You now know how to read and write data to CSV files through JMeter in Java.

To learn more JMeter, check out our free BlazeMeter University.

This post was updated for accuracy and relevance in May 2021.


Back to top