Api Load Test

Dharmendra S Negi
4 min readJan 28, 2024

As a developer working with cloud services, you are always concerned about the performance of the API you are designing.

Today in this blog, we’ll look at how quickly you can put up your own load test on the APIs that users consume.

There are many tools available for performing load tests, including Apache JMeter and Gatling. Both are excellent tools, but I needed something that would merge seamlessly into your CI/CD pipeline. And Locust appears to be a pretty useful tool. It didn’t take long for me to get started with it.

I found Locust to be an excellent tool for determining how many requests per second your system can handle and how response time varies for different percentiles of requests.

What is Locust?

Locust is an open source performance/load testing tool for HTTP and other protocols. Its developer-friendly approach lets you define your tests in regular Python code.

Locust tests can be run from command line or using its web-based UI. Throughput, response times and errors can be viewed in real time and/or exported for later analysis.

Prerequisites

  • Basic on Aws lambda and api gateway.
  • Locust
  • Github action
  • Serverless Framework

Skipping the lambda creation and github action set up.

Aws lambda configuration.

Serverless framework lambda config

Next, create a test.py file. This Python file defines how to load test your application.

from locust import HttpUser, task
class HelloWorldUser(HttpUser):
@task
def hello_world(self):
self.client.get("/hello")

The code shown above will make HTTP Get requests to get the values from the api.

Now, include a deploy and perf-test step in your Github CI/CD flow.

deploy:
runs-on: ubuntu-latest
env:
AWS_ACCESS_KEY_ID: ${{secrets.AWS_ACCESS_KEY_ID}}
AWS_SECRET_ACCESS_KEY: ${{secrets.AWS_SECRET_ACCESS_KEY}}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- name: Install serverless
run: npm install -g serverless
- name: Install npm
run: npm install
- name: Deploy
if: github.ref == 'refs/heads/main'
run: serverless deploy --verbose

Above configuration is deploying your stack to our aws account and creating a lambda with rest api endpoint. Cool you already know, what is AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

perf-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install locust
run: pip3 install locust
- name: Locust version
run: locust -V
- name: Perf test
if: github.ref == 'refs/heads/main'
run: locust -f test.py --headless -u 100 -r 10 -t 1m -H https://dslsi9j2pb.execute-api.ap-south-1.amazonaws.com/dev/ --print-stats

Next, in above perf-test step, install locust and pass api endpoint as host. You can get the Api endpoint from the output of your stack and use it.

locust -f test.py — headless -u 100 -r 10 -t 1m -H https://dslsi9j2pb.execute-api.ap-south-1.amazonaws.com/dev/ — print-stats

The command above will spawn users at a rate of 10 per second until the count hits 100. The test will last for one minute. The term “headless” indicates that no web user interface is desired and that stats should be printed to the command line instead. You may also use locust ui to view the test results.

Test Result

After about a minute, you may see how your server responds. You can examine the amount of requests per second it can service and how response time varies over time.

The result above displays the RPS and response time for various percentiles.
The total number of requests sent in one minute was around 23k, with an average rate of 400 requests per second. More

The same you can see at API gateway metrics below.

Api Gateway metrics

Summary

Locust is an incredible tool. It is simple to use, supports testing REST APIs with HttpClient and allows you to easily express what you want to accomplish in your tests.

Find the complete srouce code implementation here.

Don’t worry, api endpoint used above is not active.

Hello, I’m Dharmendra. Thank you for reading, I hope this blog will help you get the understanding of laod testing on your api. If you have any questions, feel free to DM me and I’ll be happy to assist.

Twitter, LinkedIn

References

https://docs.locust.io/en/stable/what-is-locust.html

--

--