JMeter – Counter

JMeter - Counter

This article will brief you about one of the important JMeter Config Element called ‘Counter’ and how to use it in many scenarios. Counter element in JMeter allows a user to generate an incremental number that can be referenced anywhere in the Thread Group. For example, if you have a scenario where you need to pass a unique number in each iteration then you can use a counter which will generate an incremental number in each iteration for each individual user (thread).

The counter config element lets a performance tester to configure a starting point, a maximum, and the increment number. It starts from the given starting number and reaches to the max, and then start over with the start, continuing on like that until the test is ended.

The counter uses long data type to store the value, so the range is from -2^63 to 2^63-1.

JMeter - Counter
Figure 01: JMeter ‘Counter’ Element

What are the uses of ‘Counter’ element?

‘Counter’ config element can be used:

  • To generate unique number during the test
  • To count the number of order submission (successful request)
  • As a loop incrementer
  • You can also pass ${__time(ddhhmmssSSS)} in Start so that in each iteration a unique value can be generated in time format

How to add ‘Counter’ in JMeter script?

Follow the below steps:

  1. Select the ‘Test Plan’ node
  2. Right-click on the ‘Test Plan’ node
  3. Hover the mouse on ‘Add’
  4. Hover the mouse on ‘Config Element’
  5. Click ‘Counter’
JMeter - Counter - Adding
Figure 02: Adding a Counter element

What are the input fields of ‘Counter’?

  1. Name: To provide Counter name
  2. Comments: To provide comments (if any)
  3. Start: The starting number for the counter. The counter will equal this number during the first iteration.
  4. Increment: How much to increment the counter after each iteration
  5. Maximum: This is the maximum number till that if the counter reaches, then it is reset to the Start value.
  6. FormatThis is an optional field. It denotes the format of counter e.g. 000 will format as 001, 002, etc. This is passed to decimal format so any valid formats can be used. If there is a problem interpreting the format, then it is ignored.
  7. Reference NameThis controls how you refer to counter value in other elements. Syntax is as in user-defined values : $(reference_name}. Example: ${testCounter}
  8. Track Counter Independently for each User: If you checked this option (when a counter is global) then each thread (user) will have independent counter starting from Start value. If it is unchecked then the unique value will be assigned to each user (i.e., user #1 will get value ” 1 “, and user #2 will get value ” 2 ” on the first iteration).
  9. Reset counter on each Thread Group Iteration: This option is only available when the counter is tracked per User, if checked, the counter will be reset to Start value on each Thread Group iteration. This can be useful when Counter is inside a Loop Controller.

‘Counter’ as a Function in JMeter:

Apache JMeter also provides counter() function, which returns a number starting from 1 and increasing by 1 each time. 

Syntex: __counter(argument 1,argument 2)

argument 1: It has 2 possible inputs

  • True: When Each thread (user) to have its own counter
  • False: All threads share the same counter

argument 2: Name of the variable in which to store the result (optional)

Example: __counter(True,myCounter)

Don’t put the function under any iteration provider i.e. Loop, While or ForEach Controller else it will hit multiple times and resulting value won’t be reliable.

Note: The counter uses an integer variable to hold the count, which therefore has a maximum of 2,147,483,647.


Leave a Reply

Your email address will not be published. Required fields are marked *