3 Ways to Generate Random Variables in JMeter
Random numbers are useful in testing, like when you need a random URL. Learn how to generate random numbers in JMeter.
Join the DZone community and get the full member experience.
Join For FreeSometimes we need to generate random values in our Apache JMeter™ scripts and then work with them in our requests. For example, in cases where the URL requires an auto-generated and unique value as a parameter.
There are many ways to do this in JMeter. In this blog, we will review some of the most commons ones:
- Working with epoch — Based on epoch time.
- Working with CSV files — For cases where you want to use your own generated values.
- Working with the Random Variable Sampler — Where you can select the seed and the length of your value, among other available configurations.
Let's get started.
Epoch time, also known as Unix epoch time, is a string of numbers that represents a specific and unique time (the number of seconds that have elapsed since January 1, 1970).
Working with this value helps you to simulate the request in an easy way, when the URL you want to hit looks like this and you need to randomize numbers for it:
In these cases, when setting up your request, you just have to invoke ${__time()}. This way you can generate a random and unique value.
Let see how to look at this invoke in JMeter:
And what happens when we run it?
As you can see, this JMeter function generated requests with a random and unique value for each of my calls.
That was easy, right? OL, let's continue reviewing other options.
If you have already generated the values you want to use, you can list them in a CSV file and consume them in your requests. But, how can I achieve this? Let's see.
Imagine we have a list of random variables that are already generated, saved as "variables.csv".
Now, we want to consume this list in our script. So, we have to add a CSV dataset config as we show below.
Thread Group -> Add -> Config Element -> CSV Data Set Config
Set the CSV Data Set Config as follows:
- Filename: The name of the file, in this case. Please notice that if you locate the file in the same folder as the script, you just have to enter the name. In this case, the full path is not necessary, and you can work with the relative path of the script.
- Variable names: The name we are going to use to invoke the variable. In this example, we set it as "random".
- Leave the rest of the fields as their default settings. It is important to keep the Sharing mode as "All threads" if you don't want to repeat values.
If you want more information about this element sampler, please check it out here.
Having done this, we just have to invoke the value typing ${random}
So, if we run the test, we will see how the script consumes the values defined in the CSV file:
So, this is another option to work with random variables, in cases in which you already have the list of random variables you want to use.
A third option could be to work with Random Variable, an element available in JMeter's config elements:
Thread group -> Add -> Config Element -> Random Variable
The sampler looks like this:
Setting it as follows:
- Variable name: The name we are going to use to invoke the variable. "random" in this example.
- Output Format: The format for the variable. You can set the desired length of the number. I've set 00000 in order to work with a five-digit number. You can also use USER_000.
- Minimum and Maximum: The range we want to set for the variable.
- Seed: The seed for the random number generator. A seed is the first input that the number generation function receives to start the random generation.
- Per Thread: Is important to take this option into consideration. If you set it as True the value will be shared by the threads. This means that there will be threads with the same value. If you require the variable to be different each time, this could cause problems for us. If you want to always generate a different value, you have to set it as False.
Please, check JMeter's component reference to read more information about the Random Variable.
So, if we run the test now, we will see:
We have shown three different options we can follow to achieve a script that works with random variables each time. Choose the option that fits you best according to your needs. If you have more ideas, please share them in the comments section below!
After creating your JMeter script with its random variables, run it in BlazeMeter. You will be able to massively scale, share tests and results and analyze metrics in real-time or compare them over time.
Published at DZone with permission of Leticia Almeida, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments