How to do Performance and Load Testing on your REST API in simple way

Lavisha Yadav
6 min readFeb 23, 2022

--

Every app/software runs on a client-server environment.Client and server are connected through API ,which need to be tested.To test the API,you will check “ How many transactions can the system handle for a given time period ”or “How stable is your API when lots of users have been hitting it for day”or “How many users can your API support before it crashes?”or “How long can users work with the API before the restart is needed?”

Load testing is a great way to minimize performance risks, because it ensures an API can handle an expected load.Concurrently loadAPI tests works by sending many requests to one endpoint in order to check how system behaves under normal or peak load conditions.

Here we will check what happen when we test our API Get method with 5000 request?How it behaves? How it response? etc.Here I am going to use Apache JMeter tool ,most reputed load testing tool in the industry , it’s an open source tool with considerable community.

Show Time !

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 .

Apache JMeter with GUI

Downloads/apache-jmeter-5.4.3/bin/jmeter

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

Thread Groups is the backbone of a test plan , it helps to perform concurrent user’s action in different time periods and schedules.Here i am testing API with 5000 number of users.

Adding a HTTP Request element to the Thread Group.

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.

Next you need to add an HTTP Request Sampler inside a Thread group (Test Plan -> Thread Group -> Sampler->HTTP Request)

According below picture in Http request

  • Set protocol according your server https or http.
  • In “Server Name or IP” enter URL of API
  • In “Path” enter path of API.
  • In “Port Number”enter port number used by API
Configured HTTP Request for GET API endpoint.

Right click on HTTP Request>>Add>>Config Element >> HTTP Authorization Manager.

Press add button and then add Base URL,username and the password required.

Configured HTTP Authorization Manager

Add Listener for see result/response in different format.

Right click on HTTP Request>>Add>>Listener>>View Result Tree/Summary Report/Graph Results add according your requirement, See below picture.

Running the Test Plan

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.

We should see the output as follows.Table reports are more informative in JMeter, as “Summary Report”.

Summary Report
  • Samples: This indicates the number of users(5000) per request.
  • Average: It is the average time(41449ms) taken by all the samples to execute specific request.
  • Min: The shortest time(26ms ) taken by a sample for specific request.
  • Max: The longest time(77337ms) taken by a sample for specific request.
  • Error%: Percentage of Failed requests per request.
  • Throughput: Basically, “Throughput” is the amount of proceedings produced over time during a test.It’s also represent as the amount of volume/transactions that application can handle Throughput = Total Requests / sec which is 46.3/sec here .Higher throughput is better.

“Graph result” displays overall time of response from server to the sent requests.

Graph Results

You can find general information about longest responsives, average speed of responses, deviation(22838) from average trend, throughput(2,779.657/minute) and median of server responding to different requests and their quantity.

Deviation — Shows how much the response time varies. Deviation value should always be as low as possible. Higher values generally means system is under stress.

From above two reports you can get a good idea of the load test we carried out. By increasing Thread count / Loop count we can get a clear understanding of the volume that our system can handle.

Apache JMeter with Non-GUI

The GUI mode of JMeter is perfect for adding and editing new configuration elements, thread groups, and samplers as a result of which you can view a number of different listeners helping for debugging. JMeter in GUI mode consumes much computer memory.It is generally used for creating the test script.If we are running multiple listeners in a script, it affects the JMeter performance. To overcome such a situation, the script should be run in the non-GUI mode.JMeter consumes less memory in Non-GUI mode.

Why to use JMeter in Non GUI/command line mode:

  • Avoid Crash: Increasing threads due to which JMeter crashes in the GUI mode.
  • Complex Scenarios: JMeter consumes memory and CPU and it may affect your test results.
  • Increase JMeter capabilities: To get more requests per second.

Go to $JMETER_HOME/bin folder and run the command like below

Approach 1: create report from a standalone csv file

Windows:

jmeter -n -t <jmx file name with path> -l <log file name with path>

Unix (MacOS, Linux, etc.):

sh jmeter -n -t <jmx file name with path> -l <log file name with path>
CSV Result File

Approach 2: to create report at the end of the test

Windows:

jmeter -n -t <jmx file name with path> -l <log file name with path>
-e -o <folder name with path>

Unix (MacOS, Linux, etc.):

sh jmeter -n -t <jmx file name with path> -l <log file name with path> -e -o <folder name with path>
  • -n: Is for use when we executing the file in non-GUI mode.
  • -t: Name of Jmeter .jmx file that contains the Test Plan.
  • -l : Here we give the path where we want to save the executed results into .csv format.
  • -e : This command is used to convert the .csv results into HTML format at the end of the test.
  • -o : This command saves the html results into given output folder(Non-existing).

Open the index.html file in your browser and you will find Dashboard and Charts. From dashboard you will get the APDEX, Result Summary, Statistics, Errors and Top 5 Errors by sampler.

Dashboard of HTML Report

From charts you will get different types of charts according to Over Time, Throughput and Response Times.

Charts of Response Times

Conclusion

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 here. If you have any questions feel free to ask on the comment section.

Cheers!
Happy Coding

--

--