How to Use Counter in JMeter

From sending an email to load testing database, JMeter allows us to create some really sophisticated load testing scenarios. It provides range of different elements to accomplish our performance testing goals. Counter is one such JMeter element which can prove immensely useful for testers. In this article, we will see how we can use JMeter Counter in our performance test script.

For easier understanding here, we will take an example of test data generation using JMeter. In that, we will register multiple users by incrementing a username (Eg. user+1, user+2, user+3 and so on). This is a very simple example of test data creation which testers often have to carry out. And it can be automated using JMeter Counter. So, without wasting any more time, let’s see how we can do this.

Using JMeter Counter:

  • The screenshot below shows parameters for an HTTP request. Since we will be sending our request in JSON format, we have selected Body Data option here.

    HTTP Request

    HTTP Request

  • We will change ‘username’ value in above request to ‘user+1’, ‘user+2’ etc for multiple users creation. This is where JMeter Counter comes in to the picture. It allows us to increment a value by some number. To add JMeter Counter in script, right click Thread Group and select Add–>Config Element–>Counter option.
  • Below screenshot shows options present in Counter element. Since we are starting from value 1 and incrementing it by 1, we have set Start and Increment values by 1 respectively. Reference Name value is set to ‘userCount’ which we will use in our JSON request.

    JMeter Counter Options

    JMeter Counter Options

  • Since we have our counter setup now, we just need to update our JSON request with a counter reference name (userCount in our case). Below screenshot shows how we can do it.

    Updated HTTP Request

    Updated HTTP Request

  • Now that we have our setup ready, we can run our script for multiple users and get our users created with sequential numbers.

While this example of JMeter Counter shows test data creation, it can come in handy for other testing activities also. We encourage you to try it out in your project and share your feedback in comments.

Comments
  1. sathya
  2. evangeline

Leave a Reply

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