Performance Testing Using JMeter

Rajeev Kalal
Version 1
Published in
6 min readAug 10, 2022

Overview

The Apache JMeter application is a 100% pure java application designed to test for functional behaviour and measure performance. It can perform a lot of other functions and was originally designed for web application testing. It has typically 2 modes of operation GUI and non-GUI. GUI mode is used to create a script and debug it and the non-GUI is used for execution.

GUI of JMeter

JMeter UI can be opened by clicking the jmeter.bat file present in the bin folder. It looks something like this.

JMeter GUI has menu buttons on the top and the Test Plan pane on the left side. GUI has different options for testing. Right-click on the Test Plan item and then hovering over the sub-items opens a wide range of options that you can add to the test plan. Thread group from the submenu item Threads(users) is one of the options that you can add to your test plan.

Thread Group

The thread group element controls the number of threads JMeter will use to execute your test. The controls for a thread group allow you to.

  1. Set the number of threads (Concurrent users).
  2. Set the ramp-up period (Ramp-up time).
  3. Set the number of times to execute the test (Iterations).

Each thread will execute the test plan in its entirety and completely independent of other test threads.

Right-click on the thread group and then hovering over the sub-items opens a wide range of options that you can add in the thread group.

HTTP Request

Add the HTTP Request from the submenu item Sampler. Different samplers are corresponding to different protocols. Based on the protocols used the corresponding sampler can be added to the thread group. A sampler is a request to the server.

As you can see in the right pane, we have different fields such as the name of the request, comments, protocol, Server Name, Port number, Method, path and content-encoding.

Suppose we are getting a resource from an API “https://www.test.com/resource” then.

1. The Protocol section will be “https”.

2. The Server Name will be “www.test.com”.

3. Path section will be “/resources”.

4. The Method will be GET.

You can add parameters using Add button at the bottom. We have added the scope parameter in the request.

HTTP Header Manager
The HTTP request has request headers we can add these headers using this Header manager option. Right-click on HTTP Request and add the HTTP Header Manager from the submenu item Config element.

HTTP header manager has 4 sections:

1. Header manager name.

2. Comments.

3. Header name.

4. Header value.

Add the header using the Add button at the bottom. We have added the Origin header in the header manager.

JSON Extractor

Suppose the HTTP request produces a JSON response. To extract a particular response value from the JSON we can use the JSON Extractor. Right-click on HTTP Request and add the JSON Extractor from the submenu item Postprocessors.

JSON Extractor has the following sections:

1. Name of the JSON Extractor.

2. Comments if any.

3. Options to apply to four different samples.

4. Variable name in which the extracted value is saved.

5. JSON path expression to identify the key uniquely in the JSON response.

6. Match Number: -1 for all, 0 for a random one, n for the nth on.

7. Compute concatenation var’. In case many results are found, this extractor will concatenate them by using the ‘,’ separator and storing it in a var named _ALL.

8. Default Values — Sometimes our JSON can bring an empty value, so we can use a default value for that.

Assuming this server responds with JSON.

{     "store": {         "book": [           { "category": "reference",             "author": "Nigel Rees",             "title": "Sayings of the Century",             "price": 8.95           },           { "category": "fiction",             "author": "Evelyn Waugh",             "title": "Sword of Honour",             "price": 12.99           },           { "category": "fiction",             "author": "Herman Melville",             "title": "Moby Dick",             "isbn": "0-553-21311-3",             "price": 8.99           },           { "category": "fiction",             "author": "J. R. R. Tolkien",             "title": "The Lord of the Rings",             "isbn": "0-395-19395-8",             "price": 22.99           }          ],          "bicycle": {          "color": "red",          "price": 19.95      }   }}

The table below provides a great example of different ways to extract data from a specified JSON.

User Defined Variables

User defined variables config element is used to declare and define variables which are used in the JMeter scripts. If you want to define the global variables, then add this element under the test plan. Right-click on HTTP Request and add the User defined variables from the submenu item Config element.

User defined variables have the following sections:

  1. Variables Name.
  2. Variables Value.

Assertions

There are different types of assertions in the sub-menu Assertions. To name a few we have Response, Size, Duration and compare assertion. Right-click on the HTTP Request and add the response Assertion from the submenu Assertions.

Response Assertion has the following sections.

1. Name.

2. Options to apply to different samples.

3. Field to test.

4. Pattern Matching Rules.

5. Patterns to test.

6. Custom failure message.

We are verifying the response code 200 in the response assertion.

Listeners

There are different types of listeners in the sub-menu Listener. To name a few we have View result tree, Summary report, Aggregate report and Aggregate graph. Right-click on the HTTP Request and add the View result tree from the submenu listener.

View result tree has the following sections:

1. Name.

2. File name to write results to file/read from file.

3. Option to log only errors to the file.

4. Option to log only successes to the file.

5. Configure button to save the configuration in different formats such as XML or CSV.

Clicking the play button at the top will run the test plan, and the result of the request will be output to the View Results Tree listener. This contains metadata such as response code and response data.

Test Plan execution

The test plan can be executed using GUI as well as the command line. To run using GUI click on the Play button on the top.

To run the test plan using the command line we use the below command line to execute.

jmeter -n -t $path_to_jmx_file -l result.jtl

The results will be saved in the “result.jtl” file.

To execute the test plan and generate the report we use the below command line to execute.

jmeter -n -t $path_to_jmx_file -l result.jtl -e -o $path_to_report_folder

Conclusion

These are the steps which need to be followed to do the performance testing using JMeter. You now have a basic performance test plan using an HTTP endpoint. JMeter allows you to do a lot more but this would be a good starting point.

About the Author:
Rajeev Kalal is a Test Automation Consultant here at Version 1.

--

--