Get Knowledge from Video instead of Content:

Monday, 30 January 2017

JMeter - Config Element - "Counter"

Today, we will discuss one of the important Config Element called “Counter” and how can you use it to fulfil your requirement in many scenarios. The Counter 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. The counter will loop from the start to the max, and then start over with the start, continuing on like that until the test is ended.

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

How to add "Counter" element?
You can follow below steps:
1. Select "Test Plan" node
2. Right click on "Test Plan" node
3. Mouse hover "Add"
4. Mouse hover "Config Element"
5. Click "Counter"
Apache Jmeter Counter


How to remove "Counter" element?
You can follow below steps:
1. Select "Counter" node
2. Right click on "Counter" node
3. Click "Remove" (Mouse) or "Delete" button from keyboard
4. Click "Yes"
Apache Jmeter Counter


What are the uses of "Counter" element?
"Counter" config element can be used:
1. To generate unique number during the test
2. To count the number of order submission (successful request)
3. As a loop incrementer
4. You can also pass ${__time(ddhhmmssSSS)} in Start so that in each test a unique value can be generated 

What are the attributes of "Counter" element?
"Counter" has below attributes:
a. Name: To provide Counter name
b. Comments: To provide comments (if any)
c.  Start: The starting number for the counter. The counter will equal this number during the first iteration.
d. Increment: How much to increment the counter after each iteration
e. Maximum: This is the maximum number till that if the counter reaches, then it is reset to the Start value.
f.  Format: This 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.
g. Reference NameThis controls how you refer to counter value in other elements. Syntax is as in user-defined values : $(reference_name}. Example: ${testCounter}
h.   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 unchecked then 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). 
i.   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:
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 values
          True: For 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.
Apache Jmeter Counter

Next Topic:
Apache JMeter - "CSV Data Set Config"

Previous Topic:
Apache JMeter - "Config Element"

Related Topics:

5 comments :