In order to make the performance test more realistic, it is better not to use 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 propose ideas on how to effectively store user data that will be used during the test run.
For this reason, JMeter has a very useful configuration element called CSV dataset configuration. The CSV dataset configuration allows you to use a CSV file as an external data source where you can keep unique user data, such as name, e-mail, and password. With this configuration element, JMeter can read the CSV file line by line, and then use the split parameter to assign different values to different threads.
At first glance, all CSV dataset configuration parameters seem self explanatory. But one of them may not be as simple as you think. This parameter is shared mode. This blog post will explain all the information you need to know about this option.
Understanding the JMeter CSV Data Config Element
Create a user group with five threads:
Create an HTTP request for user login:
You will see that all five users in the thread group have logged in using the same email and password. This happens because these values are hard coded in the request sampler and they are related to all five users in the thread group:
Multiple Users With Multiple Requests
But what if you want to log in under different users? To simulate a real web application load, you might want to use different users with different credentials. To this end, JMeter has a CSV dataset configuration element designed to help resolve such situations.
The main idea of this element is that it reads the CSV file line by line and provides its value to the threads of the current thread group. There are many useful articles and practice sessions that explain this JMeter element with different examples.
But first, let’s create a simple CSV file that contains a list of users (each user is on a separate line), and the e-mail and password are separated by commas between each line:
Contents of CSV file:
‘All threads’ Sharing Mode
Let’s add the CSV dataset configuration element to our performance script (right click test plan – > Add – > configuration element – > CSV dataset configuration). In this element, you need to specify file names that contain user details and variable names that will be used as containers for these values obtained from the file.
Remember, there are two options: you need to specify the full path to the CSV file, or you can use only the file name. In this case, however, the file should be in the same folder as the JMX performance script. This time you will use the default shared mode: all threads.
After rerunning our script, you can see that all users have obtained e-mail and password respectively according to order and line number:
- The first user uses the first line of email and password
- The second uses the second line of email and password
“All threads” sharing mode means that the file will be shared among all threads, andEach request will be orderedRead a line in the CSV file. This is the default method for CSV dataset configuration. It looks like this:
‘Current thread’ Sharing Mode
When we want each thread to open and read CSV files separately, we should use the “current thread” sharing mode. under these circumstances,
Each user reads the CSV file from beginning to end。 Let’s change the sharing mode to the current thread value and verify the behavior of the performance script this time.
As you can see, this time all threads try to run login requests under the same user. The reason is simple. Since each thread reads the file from beginning to end, all five threads in our thread group use the value in the first line to execute the login request.
Currently, you have only 1 loop configured in the thread group. If you want to ensure that each thread will read the second row in the next loop, let’s update our thread group with the “2” value in the “loop count”.
Since you have 5 threads and 2 loops, there should be a total of 10 requests. The first five requests should be logged in under the first user (the first line of the CSV file), while the next five requests should be logged in under the second user (the second line of the CSV file).
Let’s run the script and confirm. But before that, it might be useful to add a “constant timer” to the current thread group. Since you want to show that two loops use different lines from the CSV file, you need to avoid the situation where the first thread executes the first loop and starts the second loop while the second thread just starts the first loop. In this case, the requests in the view results tree listener will be mixed. Let’s add a 2-second timeout for this (right click test group – > Add – > Timer – > constant timer).
As you can see, the first five requests use the first line of the CSV file, and the last five requests use the second line. It looks like this:
‘Current thread group’ Sharing Mode
In the’current thread group’sharing mode, each file is opened separately by each thread group (not to be confused with the “current thread” in which each thread reads the file separately. This basically means that each thread in each thread group reads the CSV file from beginning to end without affecting each other.
To actually demonstrate this sharing pattern, you need to create another thread group. You can copy and paste the existing and rename the thread group and login sampler as appropriate.
Change the sharing mode to: current thread group
After running the script, you can find that each thread group uses a CSV file:
The workflow diagram of this case looks more complex, but the main idea is as follows:
‘Edit’ Sharing Mode
This sharing mode usually brings a lot of confusion to many JMeter users. This field is converted to input when you select the edit option under the shared mode configuration element. You can then provide your own string in this field instead of using a predefined string.
The main idea of this sharing mode is that you can use the same CSV file in multiple thread groups, but only the specified thread groups will share the file with each other. Seems a little complicated? Let us give an example to illustrate this point.
First, you need to add an additional thread group (you can copy and paste in the same way as we did in the previous paragraph). This time, let’s copy and paste the “CSV dataset configuration” element under each thread group:
Now you need to configure each CSV dataset configuration in this way:
Thread group 1: “shared mode” = “sharedwiththreadgroup1and3”
Thread group 2: “shared mode” = “all threads”
Thread group 3: “shared mode” = “sharedwiththreadgroup1and3”
“Thread group 1” configuration example:
In this case, the values of the first and third thread groups should match each other. Such a configuration tells JMeter to share a CSV file between the two, and the second thread group will use its own CSV copy. Let’s run the script again and verify the request in progress again:
First thread group:
Second thread group:
Third thread group:
As you can see, the first two thread groups read the CSV file from scratch and use the first line, while the third thread group does not. Since the third thread group has the same shared mode value as the first thread group, it continues to read the file from the line where the first thread group ends (the first line in the example provided).
By providing this sharing mode CSV dataset configuration element, you have great flexibility in controlling CSV file sharing in any way you want. This current mode can be visualized as follows:
As you can see, CSV dataset configuration is very configurable and provides great flexibility, so you can always control the use of CSV files as needed.
More Jerry’s original articles can be found in: “Wang Zixi”: