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 a random or different values to a specific parameter in the requests, how this can be achieve. One of the way is to pass different values from .csv file which contain different values and use them in the requests. Second way is to add Random Variable in JMeter, which generates random value at every run for requests. 

 

 Setting used in Random Variable are as follows:

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 time to execute testing
Ramp-Up Period: 1 
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 Random Variable

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

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

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

Step 4 – Adding Listeners to Test Plan

Listeners – They shows 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  View Result Tree listener

View Result Tree – View Result Tree show 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 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. Successful request will be of Green color in the Text Section

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

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s