Leticia Almeida is a computer engineer and a performance engineer. She has been working at Abstracta for more than 4 years, during which she has gained vast experience in analyzing application performance across multiple technologies. Leticia has leading testing teams and managed performance projects for clients from places all over the world including the USA, Latin America, and Japan.

Learn JMeter in 5 Hours

Start Learning
Slack

Run massively scalable performance tests on web, mobile, and APIs

Aug 14 2018

3 Ways to Generate Random Variables in JMeter

Sometimes 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.

 

Generating Random Values from epoch

 

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:

 

jmeter, random variables

 

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:

 

load testing with random variables

 

And what happens when we run it?

 

jmeter variables

 

As you can see, this JMeter function generated requests with a random and unique value for each of my calls.

 

That was easy, right? Ok, let’s continue reviewing other options.

 

Reading the Values from a CSV file

 

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”.

 

how to get random variables from jmeter

 

Now, we want to consume this list in our script. So, we have to add a CSV Data set config as we show below.

 

Thread Group -> Add -> Config Element -> CSV Data Set Config

 

variables, jmeter, csv file, csv data set config

 

Set the CSV Data Set Config as follows:

 

jmeter, csv data set config configuration

 

  • 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}.

 

invoking variables in jmeter

 

So, if we run the test, we will see how the script consumes the values defined in the CSV file:

 

load testing with jmeter tutorial

 

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.

 

Working with the Random Variable Element

 

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

 

jmeter, random variable element

 

The sampler looks like this:

 

jmeter , load testing, random variable sampler

 

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:

 

jmeter performance testing

 

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!

 

Running Your Test in BlazeMeter

 

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.

 

To try out BlazeMeter, put your URL in the box below and your test will start in minutes. Or, get a live demo from one of our performance engineers.

Interested in writing for our Blog?Send us a pitch!

Your email is required to complete the test. If you proceed, your test will be aborted.