How to use BeanShell Sampler to Load Test with Unique Users
This guest post is courtesy of Mustafa Mahir Kaplancı of Etiya.
A potential problem with automated load testing is when the system requires unique users to log in.
Let’s imagine you would like to run a 5k virtual user test. And you’ve determined that you should use 10 AWS instance. In addition to the JMeter script you will also need to have one CSV file which contains all the user credential info. We will use the BeanShell Sampler to load test with unique users.
Username CSV File
First we will create a CSV file which keeps all of the different usernames. Then we will save it to the same location as our JMeter script. In this example, I am using different usernames, but all users’ passwords are the same “12345”. Our CSV file name is “allusers.csv”.
Now let’s configure the JMeter scripts. I have prepared a sample JMeter script. On the “Login” page we will enter the appropriate user credentials.
See that the username field is filled statically. In order for the JMeter script to use our CSV file, which has all of the username data, we should add “CSV Data Set Config” config sampler. To perform this: Right Click on Thread Group->Add->Config Element and select the CSV Data Set Config. After adding CSV config file we should add the right path for our CSV file.
Now we can step back to our Login Sampler and change the username field to a dynamic value. JMeter variable syntax looks like ${variablename}. Our variable name is “username” because the CSV file header for the column is “username”.
We then run the test with 1 user.
As you see the test script uses a variable from the CSV file dynamically. If we run the same test again, it would use second user from the CSV file. The third test would use the third user and so on.
Doing it Random
So far everything is simple. However, we need to select users randomly and not in sequential order. Unfortunately JMeter CSV Data Config sample does not support a “Random” option such as Gatling and LoadRunner. But JMeter has deep support for using Java code in test plans. To overcome this situation, BeanShell Sampler helps us write sample Java code to select random users from our user list so that we can load test with unique users
Right click Thread Group -> Add->Sampler-> select the BeanShell Sampler and paste the java code below which first reads the CSV file and puts all the values into the Arraylist. It will randomly select the index of a user and assign this variable to a JMeter global variable named username. The critical part is the “vars.put“ line, which assigns the “username” variable for later use in the Login Sampler.
We already put username variable inside Login request, so we don’t need to change anything in the Login settings. This username points to the username variable assigned by the BeanShell script.
Now let’s run our script again with 1 user and see which user will be selected.
Success! We have done a load test with unique users. You can see that our JMeter script selected a random user (row 38) for the first Thread.
After checking that your script worked well, you are ready to run this script, as you would with any JMeter script including CSV files. In this example we keep our different usernames on AWS (Amazon Web Services) to perform load test using Redline13. Apache JMeter is one of the most popular tools for load testing and scaling out your JMeter test plan on the cloud in RedLine13 is easy. This guide and video walks you through running your first JMeter test on Redline13.
That’s it.
Reference Java Code
import java.text.*;
import java.io.*;
import java.util.*;
String csvTest = “allusers.csv”;
ArrayList strList = new ArrayList();
try {
File file = new File(System.getProperty(“user.dir”) + File.separator + csvTest);
if (!file.exists()) {
throw new Exception (“ERROR: file ” + csvTest + ” not found in ” + csvDir + ” directory.”);
}
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;
while((line = bufRdr.readLine()) != null) {
strList.add(line);
}
bufRdr.close();
Random rnd = new java.util.Random();
vars.put(“username”,strList.get(rnd.nextInt(strList.size())));
}
catch (Exception ex) {
IsSuccess = false;
log.error(ex.getMessage());
System.err.println(ex.getMessage());
}
catch (Throwable thex) {
System.err.println(thex.getMessage());
}