BreadcrumbHomeResourcesBlog How To Use a JMeter Counter For Complex Tests November 8, 2022 How to Use a JMeter Counter For Complex TestsOpen Source AutomationBy Dmitri TikhanskiThe JMeter counter is an important element. But when should you use it? And more importantly, how do you use a counter in JMeter? In this blog, we'll break down how to use a counter and a loop controller in JMeter in order to efficiently run these complex tests. Table of ContentsWhat is a JMeter Counter?When to Use the JMeter CounterHow to Use the JMeter Counter Config ElementOther JMeter Counter OptionsTable of Contents1 - What is a JMeter Counter?2 - When to Use the JMeter Counter3 - How to Use the JMeter Counter Config Element4 - Other JMeter Counter OptionsBack to topWhat is a JMeter Counter?A JMeter counter is an element that allows you to generate an incremental value. This allows you to reference the value from anywhere within a thread group. Back to topWhen to Use the JMeter CounterWhen 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.🚀Try BlazeMeter today for JMeter testing at scale >>These scenarios can include test plans:That are dependent on external data sources like a CSV Data Set, JDBC Result Set or previous response.In which 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. Back to topHow to Use the JMeter Counter Config ElementLet’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.The better way is to use a Loop Controller and a Counter. Now let’s implement the same scenario using a single HTTP Request run via parameterized iterations.1. Add a Loop ControllerAdd a Loop Controller and set the “Loop Count” to 52. Define the JMeter CounterDefine a Counter inside the Loop Controller and configure it as follows.a. “Start”This is initial counter value, let’s make it 1. (If you leave it blank, the Counter will start from zero.)b. “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.c. “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.d. “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.e. “Reference Name”This is 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. Back to topOther JMeter Counter OptionsIf 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 ElementDefine a Counter inside the Thread Group and configure it as follows:Start: 1Increment: 1Reference name: any variable name i.e. iterationTick “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() FunctionJMeter 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 instanceAll threads share the same counter instanceIn our scenario, the relevant function will be:${__counter(TRUE,)} “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 ScriptingIf the above options are not applicable, you can always fall back to scripting. JMeter supports several scripting languages, the most popular being:JavaScript (via Rhino or Nashorn) - is used internally in JMeter, for example in the “If” ControllerBeanshell (via BSH interpreter) - provides easier access to JMeter and underlying Java APIsAny 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:vars.getIteration() 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. Demo: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.This blog was originally published on May 9, 2016 and has since been updated for accuracy and relevance.START TESTING NOW Related Resources4 Endurance Testing Tips You Need to Know Back to top
Dmitri Tikhanski Contributing Writer Dmitri Tikhanski is a Contributing Writer to the BlazeMeter blog.