CSV Data Set Config in Sharing Mode - Made Easy
This post was updated for accuracy in May 2021.
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 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.
Understanding the JMeter CSV Data 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. This article can give you an idea how to automate the user registration 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.
2. Add an HTTP Request to perform the login action.
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:
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.
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.
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
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:
‘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.
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’.
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).
Now we can run the test and verify how the CSV file is used this time.
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:
‘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.
Then, all you need is to change ‘Sharing mode’ in ‘CSV Data Set Config’ to ‘Current thread group’:
After you run the script, you can find that each thread group uses the CSV file separately:
The workflow graph for this case seems more complicated, but the main ideas looks like this:
‘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:
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:
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:
Second Thread Group:
Third Thread Group:
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:
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. Let us know if you have a use case that can not be resolved with the provided examples, and we will get back to you!
To learn more JMeter, check out our free BlazeMeter University.
To start testing with BlazeMeter, sign up for free.