REST API Load performance testing using Apache JMeter

Thameem Ansari
Javarevisited
Published in
7 min readMar 5, 2022
REST API Load performance testing with Apache JMeter

Introduction

In this article, we are going to do a basic load testing with Apache JMeter which is an open source software and It can be used to simulate loads of various scenarios and output performance data in several ways, including CSV and XML files and graphs. Since it is an 100% pure Java Application, It is platform-independent (able to run on every OS ).

According to the documentation, this tool comes with a bunch of features. Such as;

  • Ability to load and performance test many different applications/server/protocol types.
  • Easy correlation through the ability to extract data from most popular response formats, HTML, JSON, XML or any textual format
  • Complete portability and 100% Java purity.
  • The full multithreading framework allows concurrent sampling by many threads and simultaneous sampling of different functions by separate thread groups.
  • Caching and offline analysis/replaying of test results.

Installation

First, you need to download the JMeter from the Apache JMeter website. Once after you download the zip file then extract it to a location where you want to work with it. Then move to the /bin folder inside that extracted folder and hit the following command- jmeter.sh to open the Apache JMeter with GUI, please note it is advised not to load the GUI when running load tests, only advised to use GUI for Test creation and Test debugging purposes.

If the installation was successful you should see below screen.

Test Plan

Before writing a test plan let’s see what it is, A Test Plan can be viewed as a container for running tests. It defines what to test and how to go about it. A proper test plan will have some of the elements such as;

  • Thread groups
  • Logic controllers
  • Timers
  • Pre and Post Processors
  • Configuration Elements etc.

Please note that, In a test plan, it is a must to have at least one thread group.

You can simply add or remove above elements to a Test Plan by right-clicking on the name of the Test Plan (i.e in my case it is the Test Plan as shown below) and then select add from the menu and, chose whatever the element you want to add.

Configuring the Test Plan

As we learned we need to have at least one Thread Group for the test plan in order to run the load test. So let’s first add a thread group to the Test Plan.

To add a Thread Group right click on the Test Plan and then add -> Threads -> Thread Groups

Adding a Thread Group

If you are successful then you will see Thread Group has been added to your Test Plan as follows.

Thread Group has been added to the Test Plan.

Then what you need to do is add another element under Thread Group for HTTP Request, as we did before we can add a new HTTP Request element to Thread Group by right-clicking on that and select the menu items as shown in the below picture.

Adding a HTTP Request element to the Thread Group.

If you are successful then you will see your HTTP Request element listed under the Thread Group element as follows.

Configured HTTP Request element

Let’s configure our HTTP Request element with relevant values for Protocol, Server Name, Port Number, Http Request Method and Path as follows.

HTTP Get Request config

Now we have successfully configured our HTTP Request element. Then we need to configure a Listener to see the result of the HTTP Request.

If you want to Configure a Test Plan for a POST API endpoint

If you want to load test a POST endpoint with a payload then you need to configure your HTTP Request to have a different configuration than the above, You will have to pass a payload under the Body Data section and the relevant port number and the Path value.

Configured HTTP Request for POST API endpoint.

Adding a Listener

In JMeter, listeners are used for outputting the results of a load test. There are several types of listeners available in JMeter but for this test we will use View Results in Tree listener, which is a more simple one.

To add a View Results in Tree element to the Thread Group element right-click on the Thread Group element and select the menu items as shown in the picture as we did before.

Adding View Results Tree to the Thread Group element.

Saving the File before Running the Listener

We will have to save the file in order to view the outputting results of the load test.

Saving the file

The file will get saved in .jmx format as shown above.

If you have added the element correctly to the Thread Group element, then our setup would look similar to the below Screenshot

Configured View Results in Tree listener.

Running the Test Plan

Now we have configured the test plan correctly. Then let’s run the test and see results.

Select on View Results Tree element in the left pane, then click Run from the main menu then click Start (or just click the green Start arrow below the main menu) as shown in the below screenshot.

If you have followed the steps correctly then you should see the output as follows.

HTTP Request Test response

We have got a 200 success response with response data since it is a Get request.

Load Testing

Okay, cool now we have everything set up correctly. Now it’s time to run the load test. In order to do so, we need to reconfigure our Thread Group element in our Test Plan to have several Thread related properties. To do that click on the Thread Group and add the following properties to it. Because to be a load test we should provide some heavy load to the API’s endpoint. By changing the following parameters in Thread Group it allows JMeters to perform a proper load test with a number of users specified.

Updated Thread Group

Those changed values are the three particularly important properties which influence a load test:

  • Number of Threads (users): The number of users that JMeter will attempt to simulate.
  • Ramp-Up Period (in seconds): The duration of time that JMeter will distribute the start of the threads over.
  • Loop Count: The number of times to execute the test.

Then again save the Test Plan and then run the test again by following the above mentioned steps and you would see something similar to below.

Load Test

Understanding the Result

We can see that the Status of all the requests in the above screenshot is “Success” (indicated by a green shield with a tick in it). After that, the Sample Time (ms)and Latency columns are probably the most interests columns in the result set.

  • Latency: The number of milliseconds that elapsed between when JMeter sent the request and when an initial response was received
  • Sample Time: The number of milliseconds that the server took to fully serve the request (response + latency)

According to the View Results in Tree output, the load Time is 144. This is fairly a high value for a simple Rest API, but in my case, this endpoint does few heavy tasks and that is acceptable for me. You might be having different results than me based on what your REST API endpoint does and based on other factors, such as; geographical distance (which generally increases latency), the size of the requested item (which increases transfer time) etc.. So don’t worry about the result, if you have different values than mine.

So, basically what we did was we simulated 50 users accessing one API endpoint over 10 seconds (5 every second).

As you can see it is pretty clear that its very easy to test the API with the number of virtual users with just a simple change in Thread Group configuration.

With JMeter user can do testing other than load testing for example Stress Test, Functional Test, Distributed Test.

Happy coding..

--

--

Thameem Ansari
Javarevisited

Technology Expert| Coder| Sharing Experience| Spring | Java | Docker | K8s| DevOps| https://reachansari.com