Rest API Load testing with Apache JMeter
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 an100% pure Java Application, it is available on every OS that supports Java 6 or later.
Tool: Apache JMeter
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.
- Full-featured Test IDE that allows fast Test Plan recording (from Browsers or native applications), building and debugging.
- Command-line mode (Non-GUI/ headless mode) to load test from any Java compatible OS (Linux, Windows, Mac OSX, …)
- A complete and ready to present dynamic HTML report
- 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.
- 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.
Install Apache JMeter.
Prerequisites: Java 8 or Java 9 for Apache JMeter 5.o
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 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 that was successful you should see above console log and GUI should be visible as follows.
Then what we need to do is, we need to create a Test Plan in order to run the load test.
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.
Now you know basics to start with creating a Test Plan to load test.
Configuring the Test Plan
API which I've chosen to load test with the Apache JMeter has many endpoints, but I’ve chosen pretty small endpoint when it comes to processing time.
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
If you are successful then you will see Thread Group has been added to your Test Plan as follows.
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 picture.
If you are successful then you will see your HTTP Request element listed under the Thread Group element as follows.
For my case will I uses an API with simple Http Header based username and password authentication mechanism. To add attributes to the header we need to add a Header Manager element under the HTTP Request element. Before do that let’s configure our HTTP Request element with relevant values for Protocol, Server Name, Port Number, Http Request Method
and Path
as follows.
Now let’s add a Header Manager element under the HTTP Request element. To do that right click on the HTTP Request element and select the menu items as shown in the picture as we did before.
If you were successful then you will see your HTTP Header Manager element listed under the HTTP Request element as follows. And press add
button and then add whatever the headers you want to add there. In my case, it will be only the username and the password.
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, as well as you might need to configure HTTP Header Manager element to have Content-Type based on your API. Following I’ve added sample configurations for both two elements for your reference if you want to load test a POST endpoint with a payload. Here I’ve assumed that API accepts Content-Type as application/json.
Adding a Listener
In JMeter, listeners are used to outputting the results of a load test. There are several types of listeners are available in the JMeter but for this test will use View Results in Table listener, which is a more simple one.
To add a View Results in Table 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.
If you have added the element correctly to the Thread Group element, then our setup would look similar to the below and you can give a Filename to output the results to a CSV file.
Running the Test Plan
Now we have configured the test plan correctly. Then let’s run the test and see results. To do that first save the test plan by clicking on File then Save, then specify your desired file name.
Then select on View Results in Table 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).
If you have done everything correctly then you should see the output as follows.
Load Testing
Okay, cool now we have everything set up correctly. Now it’s high 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 load.
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.
As you can see above we got an output like about, but how we can interpret those values or the results to have a proper idea about the test we did.
Understanding the Result
We can see that the Status of all the requests 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 Table output, the range of Sample Time was 1212–2998 ms. 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, will also be taken to the account when doing this kind of load testing. 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) in the first test. Let’s see what happens when we increase the number of threads. Because since this is a load test we need to see how the Rest API behaves based on different loads.
Retest with Increased Load
In order to increase the load then again we need to configure the Thread Group to have different values for the above files, for the time being, will only change the number of threads to have 80 over 10 seconds. In that case, you would probably see a spike on the Sample Time, In my case, it was increased by another second.
Likewise, we can test any API to see how its endpoints behave with the various loads and then you can identify issues on some endpoints when processing heavy loads. Then after you can think what to do in order to handle such loads by removing bottlenecks, load balancing, API caching, Database query optimizing etc.
This is a very powerful tool, and there are a lot more you can do with this tool such as Distributed testing, Test Recording, etc. Here we did only a fairly simple Rest API load test, you can play around with this tool and read more on the documentation to build more powerful load tests. Hope I was able to help someone out. If you have any questions feel free to ask anything on the comment section. Cheers!!
Resources: