In this article, we will learn how to do basic load testing of Rest API using Apache JMeter. The Apache JMeter is an open-source tool based on Java used to perform load testing and performance testing. As JMeter is solely Java-based, it can be used on any platform that has a JVM - Windows, Linux, or macOS.
What is load testing?
Load testing is a type of performance testing that simulates a real-world load on any application. The goal of Load Testing is to improve performance and to ensure stability.
What is an API?
API or Application Programming Interface is an intermediate software that makes the interaction between two applications possible. API acts as a messenger between two systems.
JMeter simulates a group of users sending requests to a server, and returns result that shows the performance of the target application via tables, graphs, logs etc.
Demo of JMeter API testing
In the below sections, we will see how to install JMeter and perform API testing in JMeter.
Install Apache JMeter:
There is just one prerequisite for downloading and installing JMeter-to have Java installed in your system. JMeter is compatible with Java 8 or higher.
Check if Java is installed
Open the command prompt and put the command java -version
If you have Java installed in your system, the command will show the version of Java installed. Else, you must install Java before going forward with the installation of JMeter.
2. Download JMeter
To download JMeter go to the Apache JMeter website
On the website go to the binaries section and download the zip file.
3. Install JMeter
Once the zip folder is downloaded, go to the folder location, and then extract the zip folder.
Once the folder is extracted go inside that folder and then go inside the bin folder.
4. Launch JMeter
You can start JMeter in three modes:
Command Line Mode
Start JMeter in GUI mode:
In the bin folder open the jmeter.bat file. It will take a while to open.
Note: GUI mode is not good for heavy load testing, since it consumes a lot of memory and resources. GUI mode of JMeter is designed for test recording, development and debugging purpose.
This is how you can download and install JMeter onto your system.
Steps to JMeter API load testing:
Add a Thread Group
Start JMeter and select the Test Plan. Test Plan is where you can add elements required for your JMeter Test script.
Right click on the Test Plan and select Add ->Threads (Users) -> Thread Group.
Once you click on Thread Group, there are three things under Thread Properties that are important concerning the load test:
The number of threads (users): It reflects the number of threads or users JMeter will simulate. e.g. 150
Ramp-Up Period (in seconds): The duration of time that JMeter will take before starting the thread over. e.g. 10
Loop Count: It is the number of times the test will be executed. Let’s leave it to be 1, so JMeter will run your test only once.
2. Add an HTTP Request Sampler
Add an HTTP Request Sampler inside a Thread group (Test Plan -> Thread Group -> Add -> Sampler-> HTTP Request)
3. Next search for REST APIs that are freely available on the internet. We will take the example of Reqres API.
4. Now you have the API, find out the server name, path, and parameters from the link. Copy those values in the HTTP Request of JMeter Test Plan.
5. To view the results of your Test, add a Listener. Right click on HTTP Request, select Add ->Listeners -> View Results Tree.
6. Save the test (.jmx) and run it. Simply click on the green button or short key Ctrl+R to run the test.
Test Result above shows the Request and Response for the HTTP Request server.
7. You can increase the number of Threads in order to increase the Load on your API.
8. Now run your test again.
Similarly we can test any API to see how its endpoints behave with the various loads. We can identify issues on some endpoints when processing heavy loads.
How to view JMeter results?
Results of the tests executed in JMeter can be viewed using Listeners. JMeter listeners allow viewing the results in various formats like Table, Trees, Graphs, Logs, etc.
For our demo, we have added View Results Tree listener. The response can be viewed in different formats that can be selected from the drop-down on the left as shown in the below screenshot. We can see that the status of all the requests is Success as indicated by a green shield with a tick in it.
The Sampler result tab contains the response code, response message and information about time, latency, response size in bytes — separately for the headers, the body and the error count.
In this demo we did a fairly simple Rest API load test. You can play around with JMeter and read more in the documentation in order to build more powerful load tests.
Happy Testing !