How well does your API perform?

An overview of API load testing using JMeter.

Paul Chege
4 min readJun 14, 2022

What is load testing?

So you’ve spent some time building an amazing API, tested it locally and perhaps even tested on a staging/sandbox environment. But you’re not too sure how it might perform in production where there may be numerous concurrent requests being made. Here comes load testing. Load testing allows you to simulate how your API will behave when being accessed by multiple concurrent users. This helps you identify and improve any performance bottlenecks.

There are a number of tools you can use for load testing including Apache JMeter, LoadRunner and Boomq.io. This article will focus on JMeter.

Setup

Confirm Java installation

On the command line enter:

java -version

If you don’t have java installed, download and install the latest version from https://www.oracle.com/java/technologies/downloads/

Download and run JMeter

  • Unzip the contents and store in your preferred location.
  • On your terminal move into the /bin directory of the JMeter folder and enter this command:
sh jmeter.sh

This will open the JMeter UI:

Example with REST API

JMeter is organised using test plans. A test plan is the series of steps that JMeter will run when executing the load test. We will use the default ‘Test Plan’.

In this example we will be load testing a simple GET request that uses Basic authentication.

  • Create Thread Group

This will specify the threads/users that will be calling your API during the load test. You can do this by right clicking on the ‘Test Plan’ and select Add → Threads (Users) → Thread Group. Then specify the following:

Number of Threads (Number of concurrent users/threads) : 5

Ramp up period (Time it will take for all threads to be created): 1

Loop Count (Number of times each user will make the request): 1

  • Create HTTP Request

Right click on ‘Thread Group’ and select Add → Sampler → HTTP Request. Specify the details of the request, here is our example:

  • Create HTTP Authentication Manager

Since our example implements Basic Auth we can use the authentication manager to setup the credentials. Right click on ‘Thread Group’ and select Add → Config Element → HTTP Authentication Manager. Specify the authentication details, here is our example:

  • Create Listeners

This will help us debug our load test after we run it.We will create 2 listeners ‘Results Tree’ and ‘Summary Report’. Right click on ‘Test Plan’ and select Add → Listener → View Results Tree / Summary Report.

  • Test/Debug

Click the green play button at the top. You should now see the results on the listeners. Here is a successful example:

  • Run Load Test

For running the load test, JMeter recommends not using the GUI interface but instead running it on the command line.

Save the JMeter project then go to the jmeter folder and run:

bin/jmeter -n -t ~/TestPlan.jmx -l ~/test_plan.jtl -e -o ~/test_plan_results

This generates a.jtl file with a summary of the results as well as a web dashboard report.

JTL file:

Web report:

Open index.html file generated

You can explore the various metrics that are generated depending on your use case and what you intend to optimise. One of the useful metrics under ‘Charts’ is Response Time Distribution:

We see here that most of the 5 requests took between 1800 and 1900ms.

In conclusion, load testing is an important but sometimes overlooked step in software development and with JMeter you can easily setup your load testing workflow and make your APIs more performant and fully production ready!

--

--