JMeter – Counter
This article will give you a brief introduction to the crucial element of JMeter Config known as “Counter” and show you how to use it in different situations. In JMeter, the counter element allows users to create incrementing numbers that can be used anywhere in the thread pool. For example, if you need to pass a separate number for each iteration, you can use a counter to generate an incrementing number for each individual user (thread) during each iteration.
A performance tester can configure a baseline, maximum, and increment value using the counter configuration element. It repeats this process until the specified initial number reaches the maximum before starting again.
What applications do ‘Counter’ elements have?
You can use the “Counter” config element to:
- to create a special number for the test
- To determine the number of successfully submitted orders
- You may also supply $__time(ddhhmmssSSS) in Start as a loop incrementer to create a unique value in time format for each iteration.
How to add ‘Counter’ in the JMeter script?
Follow the below steps:
- Select the ‘Test Plan’ node
- Right-click on the ‘Test Plan’ node
- Hover the mouse on ‘Add’
- Hover the mouse on ‘Config Element’
- Click ‘Counter’
input fields of ‘Counter’
- Name: To provide the Counter name
- Comments: To provide comments (if any)
- Start : The starting number for the counter. The counter will equal this number during the first iteration.
- Increment: How much to increment the counter after each iteration
- Maximum: This is the maximum number till that if the counter reaches, then it is reset to the Start value.
- Format: This 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.
- Reference Name: This controls how you refer to counter values in other elements. The syntax is as in user-defined values: $(reference_name}. Example: ${testCounter}
- 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).
- 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.
JMeter’s counter() function returns a number that increases by 1 each time it is called, and it is also available in Apache JMeter.
__counter(argument 1, argument 2) is the syntax.
Argument 1: There are two potential inputs.
When each thread (user) has a separate counter, it is true.
False: The same counter is shared by all threads.
Argument 2: The name of the variable to be used to hold the outcome.
__counter(True,myCounter) is an example.
The function should not be placed under any iteration provider, such as a Loop, While, or ForEach Controller, as this will cause numerous hits and unreliable results.
It should be noted that the counter uses an integer variable to store the count, giving it a maximum value of 2,147,483,647.