How to generate Random Variables in JMeter

HOME

This tutorial tells how random variable can generate and pass as a part of a request in JMeter. Suppose, in the load test, there is a requirement to pass random or different values to a specific parameter in the requests, how this can be achieved. One of the ways is to pass different values from a .csv file that contain different values and use them in the requests. The second way is to add Random Variable in JMeter, which generates random values at every run for requests. 

 

 

Implementation

Variable name: The name we are going to use to invoke the variable. “ReqCacheKey” in this example.

Output Format: The format for the variable. You can set the desired length of the number. I have set 0000000000 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. Here, it is ${__time()}. This will randomly generate the value like 1256078934, 9863457201

Per Thread: Is important to consider this option. If you set it as True, the threads will share the value. 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.

Create a Test Plan in JMeter by following below-mentioned steps

Step 1 –  Add Thread Group

Select Test Plan on the tree

Add Thread Group                     

To add Thread Group: Right click on the “Test Plan” and add a new thread group: Add -> Threads (Users) -> Thread Group

In the Thread Group control panel, enter Thread Properties as follows: 

Number of Threads: 1 – Number of users connects to the target website
Loop Count: Infinite  – Number of times to execute testing
Ramp-Up Period:
Duration: 5 sec

Step 2 –  Adding JMeter elements  

The JMeter element used here is HTTP Request Sampler. In HTTP Request Control Panel, the Path field indicates which URL request you want to send

Add HTTP Request Sampler

To add: Right-click on Thread Group and select: Add -> Sampler -> HTTP Request

Below-mentioned are the values used in HTTP Request to perform the test

Name – HTTP Request 
Server Name or IP – localhost
Port – 8010
Method – POST
Path – /demo/helloworld

Step 3 –  Add a Random Variable

 To add: Right-click on Thread Group and select: Add -> Config Element-> Random Variable

The sample Request is shown below. cacheKey variable is parameterized. ReqCacheKey in Random Variable will generate the random values, which will be passed to the parameter cacheKey present in the request body

Seed is ${__time()}. This generates the value randomly, like 1256078934, 9863457201. If I keep Seed blank, then when the tests run multiple times, it has the same set of random values (repetitive values), we don’t want the same repetitive values, so seed is not blank

Step 4 – Adding Listeners to Test Plan

Listeners

They show the results of the test execution. They can show results in a different format such as a tree, table, graph or log file

We are adding the View Result Tree listener

View Result Tree – View Result Tree show the results of the user request in basic HTML format

To add: Right click Test Plan, Add -> Listener -> View Result Tree

Step 5 – Save the Test Plan

To Save: Click File Select -> Save Test Plan as ->Give the name of the Test Plan. It will be saved as .jmx format

Step 6  – Run the Test Plan

Click on Green Triangle as shown at the top to run the test

Step 7 – View the Execution Status

Click on View Result Tree to see the status of Run. A successful request will be of a Green colour in the Text Section

In the below image, we can see that the cacheKey value is 1917449705 which is generated by Random Variable

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

Additional Tutorials

 How to send POST requests in JMeter
JMeter Authorization with access token
 Install Apache JMeter in Ubuntu
Constant Throughput Timer in JMeter
How to generate Random Variables in JMeter
Advertisement

2 thoughts on “How to generate Random Variables in JMeter

  1. What I love about your blogs that you provide very detailed description with images which make it very helpful to understand…great work

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s