WebAPI Performance Testing Using Apache JMeter

Introduction 

 
Apache JMeter is open-source software that is popular for performance testing and it has many capabilities beyond just API testing. The scope of this article is to demonstrate how to perform load testing of RESTful APIs using Apache JMeter. We will simulate a load on a server to test its strength or to analyze the overall performance of the APIs like response time, throughput, CPU/Memory usage, and how many concurrent users the server can handle it, etc.
 
Here, I have created a sample WebAPI project in ASP.NET Core and added a couple of endpoints to perform GET/POST operation and deployed to Azure. I will directly access to my endpoints URI from JMeter.
 

Installing JMeter

 
Download Zip format binaries of JMeter from http://jmeter.apache.org/download_jmeter.cgi. Extract the zip then go to bin folder => Run ApacheJMeter.jar from the bin folder which is the executable for JMeter. Once it’s launched, you will see the below window:
 
WebAPI Performance Testing Using Apache JMeter
 

Configuring Test Plan

 
We need to create a Test Plan in order to run the load test. A Test Plan must have at least one thread group. To create a new test plan, Click on File => New. Now we need to add Thread Group into Test Plan.
 
To add a Thread Group right click on the Test Plan => Add => Threads => Thread Groups.
 
WebAPI Performance Testing Using Apache JMeter
  
Thread groups will help to manage and configure loads of your APIs.
  • The number of threads means how many concurrent users accessing APIs.
  • Ramp-up period means how a thread should be started. For example, if you set No of Threads = 100 and Ramp-up in seconds = 50, that means in every second 2 threads will be started.
  • Loop count means how many times the thread will be looped.
WebAPI Performance Testing Using Apache JMeter
 
Now, we are going to add our Http endpoints under Thread Group. Right-click on it => Add => Sampler => HTTP Request element. Under this control, you will configure the API. Here, I named it “Get Students Request” and added my API URL into the path and method.
 
WebAPI Performance Testing Using Apache JMeter
 
In case you have any specific headers that should be part of this get HTTP request, then right-click on the HTTP Request that you added just now => Add => Config Element => HTTP Header Manager => Add your header and value which is specific to this request on clicking on Add.
 
WebAPI Performance Testing Using Apache JMeter
 
I have other endpoints for the POST request. Similarly, I added new Http Request on clicking on Thread Group and named it as “Create Student Request” and added JSON request body, on click on Body Data.
 
WebAPI Performance Testing Using Apache JMeter
 
Here, I am generating a random string for the email address field to make it unique. But this completely depends on your requirement.
 
Until now, we have successfully configured our 2 HTTP Requests – GET and POST. In the same way, you can add your other requests like PUT and DELETE.
 
Now at this point, you may have a question on how we are authorizing those HTTP Requests. You may have common herders like “Ocp-Apim-Subscription-Key” or “Content-Type” that need to send across all endpoints. We can easily achieve this by configuring a common HTTP Header Manager under the thread group.
 
Right click on Thread Group => Add => Config Element => Select HTTP Header Manager. Add those headers one by one.
 
WebAPI Performance Testing Using Apache JMeter
 
For authorization herder, there are multiple ways we can configure JWT token value in JMeter. For the simplest approach, I have increased the lifetime of my JWT token so that it won’t have an impact during load testing and generated JWT token using Postman and pasted here.
 

Adding Listeners

 
To view the results and statistics of performance testing, we need to add Listeners. This will help to monitor and view the results in a nice format. There are several types of listeners are available there but for this test, I have added View results tree, View Results in Table, Summary Report.
 
Right-click on the Thread Group => Add => Listener and select and add them one by one.
 
WebAPI Performance Testing Using Apache JMeter
 

Running Load Test

 
Once you saved the project, a JMX file will be created. Check Thread group settings before running. It depends on your requirement of how much load you can put into. Here is my sample setup of Thread Group properties.
  • No of Threads/Users: 100
  • Ramp-up period (sec): 0
  • Loop Count: 5
Now we can start the test by clicking the Green Play button on top (red box in screenshots). This will start all the thread groups as didn’t mention the Ramp-up period and the results will be capture on Summary Report as shown below. If you set a ramp-up period, then the thread will be started periodically, not at the same time.
 
If you have done the test plan set up correctly, then you should see the output as below. In case you want to re-run the test again, then clean up the previous result by clicking on the Clear All button on top (green box in screenshots).
 
Summary Report
 
WebAPI Performance Testing Using Apache JMeter
 
In the Summary report, we can easily get a few performance matrices of each request, like the No. of Samples processed, Avg. response time, Throughput, % of Error, etc.
 
View Result Tree
 
WebAPI Performance Testing Using Apache JMeter
 
In the view result tree, you see all the details related to the request. In case any request failed, we will get to know what went wrong in the request from this listener. This is useful for troubleshooting in case the request failed.
 
At the point, you may question why I am not talking about graphical statistics. To generate graphical reports, you may need to install additional plugins into JMeter. But here, I did it in a different way and generated an HTML report from cmd using a JMX file that I created for this project. Note, when you saved the project, a JMX file will be generated.
 

Generating an HTML Report

 
To generate an HTML report, create an empty folder called “HtmlReport” where generated HTML report files will be saved. Create a folder called “CSVReport” where CSV data of the report will be saved.
 
Open a command prompt and change the directory to the bin folder of ApacheJmeter where the executable jar file is present. In my case, C:\MyProject\apache-jmeter-5.2.1\bin
 
Now modify below command based on your file/folder path of Html report and CSV report and paste it on cmd.
 
jmeter -n -t C:\MyProject\apache-jmeter-5.2.1\LoadTestingDemo.jmx -l C:\MyProject\apache-jmeter-5.2.1\CSVReport\LoadTestReport.csv -e -o C:\MyProject\apache-jmeter-5.2.1\HtmlReport
 
WebAPI Performance Testing Using Apache JMeter
 
WebAPI Performance Testing Using Apache JMeter
 
The report will be generated and saved in a specified location. Now go to the HTML report folder and run index.html file. You will see a similar screen as shown below.
 
WebAPI Performance Testing Using Apache JMeter
 
In the dashboard, you will see many statistics like APDEX (Application Performance Index), Errors, Top 5 Errors by the sampler, etc.
 
In charts, you will see multiple charts like Response Times Over Time, Active Threads Over Time, Hits Per Second, Response Time Percentiles, and many more. Below are a few charts as an example.
 
WebAPI Performance Testing Using Apache JMeter
 
A sample HTML report is attached to this article for your reference.
 
Excellent! We are done with performance testing on WebAPI using JMeter and observed a couple of performance matrices like average response time, throughput, error %, etc. and generated graphical statistics on various key performance matrices. In case your application deployed to Azure you can get various metrics and analytics details in Azure API Management and Azure Application Insights, as shown below.
 
WebAPI Performance Testing Using Apache JMeter
 
I hope this article will be useful for you!