How to Use a Counter in a JMeter Test
When it comes to building various types of advanced JMeter test plans, which include not only replaying a recorded test scenario with an increased number of users, but something more complex, you will likely need some form of a Counter.
These scenarios can include test plans that:
- are dependent on external data sources like a CSV Data Set, JDBC Result Set or previous response
- the target is just to run a test (or a part of a test) several times and each time have a dynamic parameter which represents a current iteration number
This post will detail how to use a counter and a loop controller in JMeter in order to efficiently run these complex tests.
Using the Counter Config Element
Let’s imagine a scenario in which you need to create five entities in a loop using the HTTP Request sampler and each entity name has to be unique.
The bad way to go about this is to copy and paste the relevant request five times so that the Test Plan appears as in the image below:
It works and it works fine, but what if you need to do it 100 times? 200 times? What if you need one more parameter to the HTTP Request? Code duplication isn’t a very good idea by itself - it’s better to design reusable components in the form of pluggable modules, use loops and other logic controllers in order to keep your test as short as possible.
Now let’s implement the same scenario using a single HTTP Request run via parameterized iterations. To do so:
- Add a Loop Controller and set the “Loop Count” to 5
- Define a Counter inside the Loop Controller and configure it as follows:
- “Start” - this is initial counter value, let’s make it 1 (If you leave it blank, the Counter will start from zero.)
- “Increment” - this value will be added to the current Counter value once the Counter is hit. Let’s make it 1 as well as we need to replicate the above behavior
- “Maximum” - this is pretty much self-explanatory. If it is left blank, the Counter value will increment infinitely. When it is set and the current counter value exceeds “maximum” value, counter starts over. So it is fine either to leave it blank or to set it to 5 or above
- “Number Format” - you can change the output number format using this parameter. If it is left blank, the values will be “normal” like 1, 2, 3, etc. If you put “00” into the field, the values will be prefixed by double zeros like 001, 002, 003, etc. For this exercise let’s leave it as is
- “Reference Name” - the most important setting. Here you can define a JMeter Variable name that will be holding the current Counter value. It’s better to make it something self-explanatory, so that others can understand what it is (and when you revisit your test at a later date, you would easily be able to identify and understand it). So let’s call it counter_value
The Counter configuration should therefore look like:
Now let’s amend the HTTP Request sampler “name” parameter so it would take the current Counter value and send it to the server. You have the option to modify the sampler label in order to differentiate between different samplers in Listeners and identify a problematic request in case of a failure.
Change your HTTP Request as follows:
When you look at the View Results Tree listener output, you should see that the results are identical as if they were implemented by copy-and-paste.
If for some reason the Counter Config Element doesn’t play for you, there are other approaches to get the current iteration number.
1. Using the Counter Config Element
Define a Counter inside the Thread Group and configure it as follows:
- Start: 1
- Increment: 1
- Reference name: any variable name i.e. iteration
- Tick “Track counter independently for each user” box (this is required for to avoid double counting when you have more than one virtual user)
2. Using the __counter() Function
JMeter provides the __counter() function, which returns an incrementing number starting from 1 and increasing by 1 each time. It has 2 modes:
- Each thread (virtual user) has its own counter instance
- All threads share the same counter instance
In our scenario, the relevant function will be:
“TRUE” stands for individual counter for each virtual user.
Don’t put the function under any iteration provider i.e. Loop, While or ForEach Controller elsewise it will be hit multiple times and resulting value won’t be reliable.
The __counter() function use cases are described in details in our Using JMeter Functions - Part II article - be sure to check it out.
3. Using Scripting
If the above options are not applicable, you can always fall back to scripting. JMeter supports several scripting languages, the most popular being:
- Beanshell (via BSH interpreter) - provides easier access to JMeter and underlying Java APIs
- Any language supported by JSR-223 JCP (Groovy is recommended to use with JSR-223 test elements in JMeter)
Each of these languages have access to the vars pre-defined variable, which stands for the JMeterVariables class instance, which provides read-write access to all JMeter Variables in scope (the JMeter Variables scope is limited to the current Thread Group only).
Getting the current Thread Group iteration is as simple as:
Remember that this way you will get the Thread Group iteration number only. The other iteration providers (like Loop, While Controller, etc.) will not be considered and won’t cause the iteration to increment.
Here is an example of getting the current Thread Group iteration using the aforementioned 3 approaches:
Usually the iteration number is combined with the current virtual user number. The current virtual user ID can be retrieved using the __threadNum function.
Hopefully, this article clarifies everything regarding using different Counter approaches in JMeter tests. If your scenario isn’t listed and you have problems with it - don’t hesitate to ask questions in the comments section below.
Want to Learn More Practical Advice on Using JMeter?
If you are new to JMeter, and you’d like to learn more, please sign up for our free online JMeter training course.
For more experienced JMeter users, you'll want to view the on-demand webcast, How to Create Advanced Load Testing Scenarios with JMeter.