Apache JMeter is a popular open-source performance testing tool. It is a 100% pure Java application designed to load test functional behaviour and measure performance. It was originally designed for testing Web Applications but has since expanded to other test functions.
This tutorial tells how random variablecan 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.
The random variable config element is used to generate random numeric values within a range of specified minimum and maximum values.
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.
Implementation Steps
Create a Test Plan in JMeter
Create a new test plan in JMeter by selecting File > New Test Plan.
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: 1 Duration: 5 sec
Adding HTTP Request
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
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
Adding Listeners to the 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 shows the results of the user request in basic HTML format
To add: Right click Test Plan, Add -> Listener -> View Result Tree
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
Run the Test Plan
Click on Green Triangle as shown at the top to run the test
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!!