API Load Testing for enterprise app with Python based Locust

Debolina
affinityanswers-tech
3 min readFeb 9, 2023
Photo by STEFANIA BOKA on Unsplash

In 2021 while developing the enterprise application for Affinity Data+ we did functional testing which will ensure that the application is working as intended. Like any web application, the API in the backend can make or break the experience when the application is used simultaniously by serveral users. But then, we still didn’t know how our API will perform when the real users start using it or even worse, we did not know the performance metrics of our API under load.

Load testing of APIs is the process of evaluating an API’s performance under a specific workload. The goal of load testing is to identify the maximum number of requests the API can handle simultaneously and detect any potential performance bottlenecks before the API is deployed in production environment.

While this is absolutely necessary for any e-commerce applications where the attention span is low, in our case we were developing an enterprise application where attention span might not be an issue but long term user satisfaction will be hampered if for example loading of data takes more time than normally acceptable.

Objective of load testing was to measure -

  • Response time
  • Throughput
  • Resource utilisation
  • Maximum user load

One will find various load testing tools such as Apache JMeter, LoadRunner, PostMan and so on. But for someone like me who is a Python lover, choosing Locust to perform load test was an easy choice because the tool is written in Python and the user behaviour can be defined in Python. And, the tool is easy to install, is open-source & is light-weight.

Let’s see how it works.

Install Locust using the following command

pip3 install locust

Once it’s installed you should be able to see all Locust’s options.

locust — help

Now we are ready to create a locustfile in Python code which will call the api which you want to test at a certain load. In the following example we have an API with an endpoint ‘/api/getClients’ .

import sys

class WebsiteTestUser(HttpUser):
@tag('dashboard')
@task
def getClients(self):
with self.client.get("/api/getClients", headers={"Authorization": f"Bearer {self.token}"}, catch_response=True) as response:
try:
if response.json().get("code") != 200:
print("Didn't get 200 response")
sys.exit(1)
except JSONDecodeError:
print("Response could not be decoded as JSON")
sys.exit(1)
except KeyError:
print("Response did not contain expected key 'code'")
sys.exit(1)

Here I have added a tag called “dashboard”. By tagging tasks using the @tag decorator, you can be picky about what tasks are executed during the test using the --tags and --exclude-tags arguments.

Here we would be testing this API with 5 , 10 and 15 users respectively with a spawn rate of 1 user/second this means when you start the load test Locust will spawn 1 new user for every second until it reaches to the total number of users to simulate (which is 5,10 & 15 in this case).

How to run locust test?

locust -f locustfile.py  --host=https://ontrack-dev.affinityanswers.com -u 5 -r 1  --headless --tag dashboard -t 45s

The above command will run the API with 5 users (at 1 user/sec spawn rate) for a time span of 45 seconds. The — host option will accept the URL where the web request should go. The — headless option will run locust without the web UI. Otherwise it will open a nice UI & show the test result in real time.

The load test will show —

  • Request method
  • The no. of request made with success/failure status
  • The response time of 50 percentile, 90 percentile request
  • Average response time
  • Max/min response time

Here is an example of how it looks in the Locust UI.

You can view the result in charts as well. And if you want to run the test headless there will be option to store the result in CSV file also.

Overall, Locust is a very easy & nice tool for load testing which one can learn and implement quickly, particularly if one is comfortable with Python.

--

--