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. The 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 helps a performance tester for configuring a starting point, a maximum, and an increment number. It starts from the given starting number and reaches the max, and then starts over with the start, continuing on like that until the test is ended.

The counter uses a 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 the ‘Counter’ element?

‘Counter’ config element can be used:

  • To generate a unique number during the test
  • To count the number of order submissions (successful requests)
  • 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 the 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 the 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 the 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 values in other elements. The 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 an independent counter starting from the Start value. If it is unchecked then the unique value will be assigned to each user (i.e., user #1 will get the value ” 1 “, and user #2 will get the 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 the Start value on each Thread Group iteration. This can be useful when the Counter is inside a Loop Controller.

‘Counter’ as a Function in JMeter:

Apache JMeter also provides a 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) has 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 the 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.


You may be interested:

Leave a Comment