Load Testing using Locust: Easiest way to performance tests

Japneet Sachdeva
3 min readJun 3, 2024

--

Locust is open source python programming based load testing tool, which can help you to performance test your APIs and then report back complete details of errors, number of request sent, 90% error, average, min & max response time and alot more details.

Now let’s dive into setup of Locust for your machines. Make sure before setup of Locust, you have Python and PIP installed in your machines. Otherwise use below config and commands to complete the same.

  1. Download Python Installer: Go to the official Python website.

2. Click on the “Download Python” button. Make sure to download the latest stable release.

3. Run the installer (make sure to install PIP too) and once completed then verify whether python successfully installed in your machine or not using:

python3 --version

Now let’s proceed towards installing locust:

  1. Install Locust using pip: Python’s package installer, pip, is included with Python installations. Use it to install Locust:
pip3 install locust

2. Verifying the installation:

locust --version

3. Now as a python SDE or SDET, I expect you already have installed Pycharm, if not please download here: Link (install community edition)

Note: All the steps mentioned above can also be executed within pycharm using Terminal or Shell.

Now as pre-requisite please create a empty project in pycharm and create a new python file.

Once this is created then we can start with the setup of our API requests for load testing as below:

from locust import HttpUser, task, between

class APITestUser(HttpUser):
wait_time = between(1, 2) # Wait time between tasks

@task
def get_users(self):
self.client.get("/users")

@task
def create_user(self):
self.client.post("/users", json={"name": "testUser", "email": "test@example.com"})

@task
def update_user(self):
self.client.put("/users/1", json={"name": "updatedUser", "email": "updated@example.com"})

@task
def delete_user(self):
self.client.delete("/users/1")

Let’s first have a higher level understanding:

  1. HttpUser: A class in Locust that represents a user that can perform HTTP requests
  2. task: A decorator used to mark methods as tasks that Locust users will execute during load testing
  3. between: A function used to specify a range of wait times between tasks
  4. wait_time: An attribute that defines the time a user waits between tasks, specified using the between function. Now (1,2) means locust will pick a random number between 1 and 2 seconds for each iteration or request
  5. client: An instance of HttpSession used to make HTTP requests within tasks
  6. Get, Post, Put & Delete are HTTP methods which are used to make requests for load testing of APIs

Now, we have a setup of API requests, let’s start with load testing:

  1. Start Locust by running the command in the terminal:
locust
  1. Open your web browser and navigate to http://localhost:8089

2. Configure the number of users, spawn rate, and host URL in the web interface

3. Click “Start swarming” to begin the test

  • Number of Users: Total concurrent users to simulate
  • Spawn Rate: Rate at which users are added to the test
  • Host URL: Base URL of the application or API being tested

Once this test is started and completed we will get certain reports like below which can be used to judge the performance of APIs if any improvement can be discussed with DEV team or Product Owner.

-x-x-

If you are preparing for Test Automation Manger/Architect/Director roles then refer my updated Q&A interview bank here: Link

My latest course for creating a test automation framework from scratch and using top tools like docker, github actions, jenkins, parallel exeuction within different env. and browsers: Link

#japneetsachdeva

--

--