Updated February 21, 2023
Introduction to JMeter Random Variable
The following article provides an outline for JMeter Random Variable. Today, most programming languages have an API that produces random numbers. The generator algorithm produces a typical sequence of numbers without any structure, order, or format. This algorithm is based on seed value which drives the sequence generation. In JMeter, the random variables can be generated and passed in a load test. In a load test, there is a need to pass any random values to a constrained parameter in the request. It can be achieved by passing different values from the .csv file, which has distinct values to use in the request. It is also possible to generate random variables at every request execution.
What is JMeter random variable?
JMeter enables the user to produce random numbers and use them as a variable. It can be achieved with the configuration element of a random variable. It enables the user to configure all the mandatory parameters like format string, variable name, per-thread values, user counts, random speed, and minimum and maximum values. The random values can be generated in three methods using JMeter. They are epoch methods using CSV files and a random variable sampler.
How to use the JMeter random variable?
To generate random variables in JMeter, all the configuration fields must be set, and the user should determine which method he will use to generate random variables.
It can be generated using the epoch time; then, it can be generated using CSV files to produce its own set of generated values. Then in a random variable sampler, the user can choose the seed values, length of values, and other available configurations.
In the variable name field, give the appropriate name used in the test plan, and it can be saved in this variable.
In the format string, give the format of the number that wants to be generated. It can be used as a prefix or suffix with a string. It is also possible to generate alphanumeric values.
In minimum and maximum fields, the user has to define the numbers’ range that needs to be generated. For example, the minimum value can be 10, and the maximum can be 50.
In the per-thread field, the user has to give whether the random generator should be shared by all the users or every thread has its instance random values. It can be set using the true or false option.
Random speed is used to choose the seed value of the generator. If the same seed is used for all threads, it generates the same number for every thread.
Create JMeter random variable
In the below section, the test plan in JMeter is created.
Select the test plan from the tree, then add the thread group. To include the thread group, right-click the test plan and include the new thread from the thread group.
Add the elements of JMeter used in the HTTP request sampler. The control panel of HTTP requests indicates the URL request that needs to be transferred. Right-click on the thread group and choose sampler, then HTTP request. A few of the values in the HTTP request to execute the test are the name of the request, server name or IP or localhost, port, method name, and the path of the request.
Then add a random variable by selecting configuration elements on the thread group and select a random variable from there. The variables of the cache key have parametric values, and the reqcachekey will generate random values passed to the parameter cachekey present in the request’s body.
Seed generates random values; if left empty, the test can be executed multiple times. However, the seed is not left blank if it has the same set of repeated values.
Then listeners can be added to the test plan, and the result can be viewed in different formats like table, graph, tree, or log file. In the view result tree of the user, request choose view result tree from listener then include in the test plan.
Save the test plan, give the appropriate name to the test plan, and it will be saved in JMX format.
Execute the test plan by clicking on the green triangle placed on the top pane.
The execution status can be viewed in the view result tree to see the status, and the successful request is marked in green.
JMeter random variable examples
The random values can be generated in the configuration element. Nowadays, generating or picking the test case of a random variable is a tough task. So the user can have a test case where the test has a random number and checks if it is an acceptable range or if the number format is valid or invalid. The other test case can be defined as the user giving a few random numbers as a URL section like order id, or page numbers can be mentioned. It is used to execute the load testing on these URL pages. The configured variable in the HTTP request can be used as a sampler on this part of the URL request. It will test the Java section pages on JCG websites.
The random variable option can be used if the user wants to execute a load test on several URL pages with parameter values that can be substituted dynamically at the execution time. It also supports load testing on both web models, client-server and host with static and dynamic resources. In addition, it works on a wide range of protocols like HTTP, JMS, HTTPS, FTP, and JDBC.
The user must sometimes generate JMeter scripts and then work on the request. So, the URL needs an auto-generated and constrained value as a suitable parameter in this case. JMetIn addition there uses multiple methods to generate random variables, which are briefly discussed here. So it is up to the user to select the suitable method for his generation of random variables.
This is a guide to JMeter Random Variable. Here we discuss How to use the JMeter random variable and the examples. You may also look at the following articles to learn more –