Basic Load Testing with Locust

Dennis Walangadi
3 min readMay 9, 2022

Yay! The service you’ve been working hard on has finally finished! Now you’re ready to release your service to the public space. However, you don’t know how many users it can handle and how it will react when many users access it simultaneously.

Intro to Load Testing

Load testing is a non-functional software testing approach in which the output of a software application is checked under a specific load. It defines how the software application behaves when multiple users access it simultaneously. Load testing aims to improve app bottlenecks and ensure reliability and smooth running before deployment. Load testing may occur when the software has already been deployed to the production environment and is attempting to prepare for an important event.

Intro to Locust

Locust is an open-source load testing tool based on Python. With Locust, we can define which endpoints we want to test and how big the load will be.

You need to have Python installed on your computer to install Locust. You can type `pip install locust` on the shell to install your Locust on your local machine. Once you are done, you can test it by running:

locust --help

you should see the following output:

Getting Started with Locust

Below is an example API server in Node.JS. We will try to test this app with Locust.

As you can see, it has two endpoints: the/helloendpoint, which returns hello world, and the /slow endpoint, which is intentionally supposed to take two seconds to complete.

Now we will set up the Locust script for the app above. Create a new file called locust.py and add the following code:

As you can see, the script is very straightforward: it’ll test both endpoints by sending an HTTP GET request. On line 6, we call the between(1,5)to tell Locust to wait between 1 and 5 seconds until it sends another request. It’s optional, but it’s good to emulate actual user requests.

Line 8 and 12 contain functions that will run to test each endpoint. Notice the @task decorator. It marks which function is the task function. The task function describes how Locust should call your services, and you can add a number after the task decorator to give them weight, which allows you to manage which tasks are hit more than others.

Running the Locust Script

If you haven’t already installed Python, you might need to do so. Then you can run locust -f locust.py to run your locust script. You can then use the UI at localhost:8089 to tell it how many users to test and at what rate to spawn them:

After that, you watch the results roll in:

Congrats, you just load-tested your project!

If your project have authentication and payloads, Locust can test them out too! Checkout the official Locust documentation to learn more!

--

--