November 8, 2022

How to Use a JMeter Counter For Complex Tests

Open Source Automation
The 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. 
 
Back to top

What 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 top

When to Use the JMeter Counter

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.

🚀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 top

How to Use the JMeter 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:

 

A screenshot of the JMeter HTTP Request Sampler Loop 5 Entities.

 

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.

 

An image of a winding staircase.

 

Now let’s implement the same scenario using a single HTTP Request run via parameterized iterations.

1. Add a Loop Controller

Add a Loop Controller and set the “Loop Count” to 5

2. Define the JMeter Counter

Define 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:

A screenshot of JMeter counter configuration.

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:

 

A screenshot of the HTTP Request Counter Value in JMeter.

 

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.

 

A screenshot of how to view results tree of identical results in the JMeter Counter.

 

Back to top

Other JMeter Counter Options

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)


A screenshot of the Counter Config Element in JMeter.

 

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:

  1. Each thread (virtual user) has its own counter instance
  2. All threads share the same counter instance

In 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 Scripting

If the above options are not applicable, you can always fall back to scripting. JMeter supports several scripting languages, the most popular being:

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:

 

A screenshot of the Thread Group iteration in the View Results Tree in JMeter

 

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 Resources

 

Back to top