Performance Testing with Locust

Emre Dogangun
2 min readAug 21, 2024

--

In this guide, we will walk through how to perform a performance test on the Swagger Petstore API using Locust. Locust is a user-friendly, Python-based load testing tool that allows you to define user behavior and simulate thousands of concurrent users accessing your application.

Prerequisites

Before starting, ensure you have the following installed:

  • Python: Locust is a Python-based tool, so you need Python installed on your system.
  • pip: Python package installer, used to install Locust.
  • Locust: You can install it using pip.
pip install locust

Setting Up the Locust Test

Create a file named locustfile.py in your project directory. This file will define the behavior of the virtual users.

Here is an example of what your locustfile.py could look like:

from locust import HttpUser, task, between
class PetstoreUser(HttpUser):
wait_time = between(1, 5)
@task
def get_pets(self):
self.client.get("/v2/pet/findByStatus?status=available")
@task
def get_pet_by_id(self):
pet_id = 1 # Example pet ID
self.client.get(f"/v2/pet/{pet_id}")
@task
def add_pet(self):
pet_data = {
"id": 0,
"category": {"id": 0, "name": "string"},
"name": "doggie",
"photoUrls": ["string"],
"tags": [{"id": 0, "name": "string"}],
"status": "available"
}
self.client.post("/v2/pet", json=pet_data)Configuring the Test

You can also define a configuration file to manage test parameters easily. Here’s an example master.ini file:

[locust]
locustfile = locustfile.py
headless = true
master = false
expect-workers = 5
host = https://petstore.swagger.io
users = 100
spawn-rate = 10
run-time = 1m
autostart = true
autoquit = 60

Running the Test

To run the test with the configuration file, use the following command:

locust -f locustfile.py --config master.ini

If you prefer to run it without the configuration file, you can directly use:

locust -f locustfile.py --host=https://petstore.swagger.io

Once the test starts, Locust will simulate user requests to the Swagger Petstore API. If you’ve set headless = false, you can open a browser and go to http://localhost:8089 to monitor the test in real-time. The dashboard provides insights into response times, request failures, and the number of requests per second.

--

--