Performance testing of APIs using Apache Jmeter
When we develop APIs, we tend to forget about certain scenarios where there could be a huge load on our interfaces, leading to unavailability of the service and some downtime or might ultimately crash the deployment. Therefore, it is always a good idea to subject our APIs to the expected load and test it before deploying them for public use. The idea of using Postman or for that matter, any client application to mimic that kind of situation does not make sense and could prove to be very tedious.
ENTER JMETER
Before diving deep into JMeter, let's try to understand the different types of testing.
- Load Testing: This tests the workload that is produced in normal conditions and measures the application’s response time. It also helps in identifying any bottlenecks.
- Stress Testing: Extreme workload is applied to find out the load point at which the app will crash.
- Spike Testing: This involves changing the workload rapidly and monitoring the application’s response.
- Endurance Testing: This involves exposing the application to the expected workload for a prolonged period of time to check how well it can handle it.
- Scalability Testing: This test involves a gradual increase in workload to find out how efficiently the software can scale up.
- Volume Testing: Volume testing involves the performance testing services feeding the software’s database with large volumes of data to check its data processing capabilities and efficiency.
There are many tools that one can use for performing performance testing but Apache JMeter is open-source and is being used for a long time and has a GUI as well as CLI. It can be used to analyze and measure the performance of a wide range of software, covering services including networks and servers. It is mostly used as a website load testing tool for different types of web service applications.
Installing and Running JMeter
Step 1:
Because JMeter is a pure Java desktop application, it requires a fully compliant JVM 6 or higher. You can download and install the latest version of the Java SE Development Kit here.
Step 2:
Download Jmeter binary from here. You can choose to download zip or tgz format. Once downloaded, extract the zip/tgz file to any favorable location.
Step 3:
Once extracted, the structure of the file looks something like this:
- /bin: Contains JMeter script file for starting JMeter
- /docs: JMeter documentation files
- /extras: ant related extra files
- /lib/: Contains the required Java library for JMeter
- /lib/ext: contains the core jar files for JMeter and the protocols
- /lib/JUnit: Junit library used for JMeter
Running JMeter
If you are using Windows, just run the file /bin/jmeter.bat to start JMeter in GUI mode. Once run successfully you should see something like this below:
Voila, It's that easy!
Writing your first test in Jmeter
Step 1: Run JMeter
Step 2: Rename the Test Plan to anything you like. I am renaming it to “My First Test” here for reference. Save the test in /bin folder as shown below. This is done to ensure that whenever we use CLI it is easier to locate files and run the test plans.
Step 3: Add Thread Group
Now we add our first element in the window. We add one Thread Group, which is a placeholder for all other elements like Samplers, Controllers, and Listeners. We need one so we can configure the number of users to simulate.
- Right-click on the My First Test (our Test Plan) → Add → Threads (Users) → Thread Group. Thus, the Thread Group gets added under the Test Plan (My First Test) node.
- Rename the thread group as Users (or anything you like). For us, this element means users visiting the Medium home page.
The controls for a thread group allow you to:
- Set the number of threads
- Set the ramp-up period
- Set the number of times to execute the test
Each thread will execute the test plan in its entirety and completely independently of other test threads. Multiple threads are used to simulate concurrent connections to your server application.
The ramp-up period tells JMeter how long to take to “ramp-up” to the full number of threads chosen. If 10 threads are used, and the ramp-up period is 100 seconds, then JMeter will take 100 seconds to get all 10 threads up and running. Each thread will start 10 (100/10) seconds after the previous thread was begun. If there are 30 threads and a ramp-up period of 120 seconds, then each successive thread will be delayed by 4 seconds.
For our scenario, I have configured it as follows:
Step 4: Add Sampler
A sampler is a request or the operation that the threads will be hitting to simulate or mimic the load on the server. Here, we will be making requests to https://www.medium.com and find out how it handles the load.
- To add a sampler, we will open the context menu of the Thread Group (Users) node by right-clicking and we will add HTTP Request Sampler by choosing Add → Sampler → HTTP request option.
Let us configure the HTTP Request sampler:
- Name − We will change the name to reflect the action that we want to achieve. We will name it as Visit Medium Home Page
- Server Name or IP − Here, we have to type the web server name. In our case it is www.medium.com. (http:// part is not written this is only the name of the server or its IP)
- Protocol − We will keep this blank, which means we want HTTP as the protocol.
- Path − We will type path as / (slash). It means we want the root page of the server.
- Method — We can also configure the method as GET/ PUT/ POST/ PATCH based on our requirements and even add the request body and other parameters to the request. Here, to keep things simple, we will just perform a GET on the medium homepage URL.
Step 5: Add Listener
Once the tests are run successfully, there should be a way to monitor and analyze the test results. This is where Listeners come in. Let us add View Results Tree Listener under the Thread Group (User) node. It will ensure that the results of the Sampler will be available to view in this Listener node element.
To add a listener −
- Open the context menu
- Right-click the Thread Group (Users)
- Choose Add → Listener → View Results Tree option
There are a number of listeners that can be added and even various plugins are available to use in case built-in listeners are not enough.
Step 6: Run the test plan
Save all the changes and to execute the test plan, Select Run from the menu and select the Start option.
Step 7: View Results
We had set the thread count to 25 and loop as 4, so ideally 100 requests should have been made.
Details of the above result are −
- The green color against the name Visit Medium Home Page indicates success.
- JMeter has stored all the headers and the responses sent by the web server and ready to show us the result in many ways.
- The first tab is Sampler Results. It shows JMeter data as well as data returned by the web server.
- The second tab is Request, which shows all the data sent to the web server as part of the request.
- The last tab is Response data. In this tab, the listener shows the data received from the server in text format.
Looks good!
This was just an introductory test plan to familiarize beginners with the Apache JMeter terminologies and structure of the test plan. To summarize, the anatomy of a JMeter test looks something like this:
I will try to highlight different use cases, timers, assertions, and CLI usage of JMeter. For now, let’s end it here. Reach out to me on Twitter if you want to give feedback or have any queries. Peace 👋