JMeter ForEach Controller
Among the many elements JMeter offers for creating test scenarios, the ForEach loop stands out as an efficient way of browsing and processing data.
The ForEach loop in JMeter is a controller that allows you to cycle through the elements of a list or variable, executing the test samples included in the loop for each element iteratively. This is particularly useful when you need to perform tests with dynamic data, or when you want to repeat a sequence of samples for different values.
- Save time and effort:
-
The ForEach loop simplifies the management of test scenarios involving dynamic data, reducing complexity and development time.
-
Improved maintainability:
-
By using the ForEach loop, test scenarios remain easily understandable and modifiable, facilitating long-term maintenance.
-
Dynamic test scenarios:
- Allows tests to be adapted to real-life situations by iterating over variable data sets.
ForEach in more details¶
As explained in the official documentation a ForEach controller loops through the values of a set of related variables. When you add samplers (or controllers) to a ForEach controller, each sample (or controller) is executed one or more times, with the variable taking on a new value with each loop.
For example, you need to request an API endpoint that returns information such as stock, location and price for a set of available products retrieved from an upstream API call.
There are several parameters to take in count :
Input variable name:¶
This is the name of the variable or list containing the elements you wish to use in the loop.
Start index loop (exclusive)¶
If you wish to use a value from which elements will be used from the list of retrieved elements.
End index loop (inclusive)¶
If you wish to use an end value in the list of elements to be retrieved.
Example :
You can easily filter the elements to be browsed / used using the indexes.
Results :
Output variable name¶
This is the variable name that will be used within the loop, with a distinction between each element using "_" if checked on the controller.
Remarks¶
If you wish to use a variable (${...}) containing a numerical value as a starting value, it will not be interpreted as such, and will start at 0.
We'll give you an example of how to use a ForEach loop through a JSR223 script to take into account a starting index, and thus filter the list of values to be used.
If you leave the input/output variables blank, there will be no error, but no execution either.
Difference with While loop?¶
The While loop Controller is used when you want to repeat a set of instructions as long as a given condition is true, so the instructions may not be executed, either once or several times. More flexible, as the condition can be based on any evaluable expression, enabling a wider variety of test scenarios.
Quick example :
Let's imagine we want to execute all the children of the while loop 4 times we can proceed that way by using a Loop Controller, the index start at 0.
Once the condition is set to 'False' while loop will stop :
The ForEach loop is used when you have a fixed list of elements to go through, such as predefined values or specific test data, as explained above.
Practical example¶
If you want to follow along you can download the sample JMX here.
In this example, the first call is made to the /example
endpoint :
From which we'll extract the responses contained in the "responseVal" key using a JSON extractor :
The JSON extractor allows you to extract a particular response from the results obtained, either by specifying the value of the result you wish to use next, or retrieve it randomly, or retrieve all the values that match the expression.
We'll enter -1 in Match No. to store all matching results in the JSONValues
variable.
Now we can use the ForEach loop in JMeter, just follow these simple steps:
Add a ForEach Loop Controller¶
- Right-click on your Test Plan.
- Go to "Add" -> "Logic Controller" -> "ForEach Controller".
Configure the Loop Parameters¶
- In the controller properties, specify the variable to be used and the list of elements to be browsed. The variable will be updated at each iteration with the current value, in our case we'll browse
JSONValues
and use at each iteration theJSONValue
within the loop.
Place your Test Samples inside the Loop¶
All test samples you wish to repeat must be placed inside the ForEach Loop Controller. The ForEach loop will iterate through each value contained in the JSONValue variable, executing the associated test samples for each iteration.
Tree View example
HTTP Request example
View Result Tree overview
In our example 10 requests have been sent, which correspond to the number of values contained in the variable JSONValues
- Here an extract of the Debug Sampler :
The Debug Sampler generates a sample containing the values of all JMeter variables and/or properties during the iterations.
The values can be seen in the View Results Tree Listener.
The variable named JSONValues_matchNr
indicate the number of values stored in JSONValues
Advanced example using a JSR223 sampler¶
In some cases, you may need to specify a start and end index using numerical values, in order to reduce the number of samples to be sent. And if, for example, you wish to dynamically vary this adjustment, you could proceed with a JSSR223 script, as shown below:
As we said previously if you wish to use a variable containing a numerical value as a starting value, it will not be interpreted as such, and will start at 0.
Using this type of example, you can define a starting index with a numerical value (random here) that will allow you to run a different number of samples at each iteration, taking into account the number of elements extracted at each iteration in the JSONValues_matchNr
variable.
Example of JSR223 Sampler :
Random rnd = new Random();
def start_index = rnd.nextInt(Integer.parseInt(vars.get("JSONValues_matchNr"))+1); // Random values to define start index, we need to parse it as an Integer to use it properly on the for loop, randomization takes into account the number of elements contained in: JSONValues_matchNr.
def end_index = Integer.parseInt(vars.get("JSONValues_matchNr")); // Max number of values used for the end index, we need to parse it as an Integer to use it properly on the for loop.
log.info("START INDEX : " + start_index + ", " + "END INDEX : " + end_index);
for(i=start_index;i<=end_index;i++){
log.info("OUTPUT : " + vars.get("JSONValues_"+i));
// Add some code to launch http request
}
In this way, results will vary from one iteration to the next dynamically.
Conclusion¶
Using the ForEach loop in JMeter offers an elegant solution for automating and iterating efficiently through test scenarios, improving the quality and robustness of your performance tests.