Load testing is nonfunctional testing that evaluates whether an API stays responsive under simulated users or requests. It helps reveal performance bottlenecks and estimate system capacity before production planning. section 4.8
A practical Locust workflow is:
- Install Locust with pip install locust and create a locustfile.py. section 4.8
- Define an HttpUser with a host, a wait interval, and @task methods that call API endpoints through self.client. section 4.8
- Run locust, open the local dashboard, and provide the user count, spawn rate, host, and test duration. section 4.8
- Expand coverage across endpoints, replace hardcoded identifiers with random values, and inspect successive run metrics. section 4.8
- Add status-code assertions so failed responses become visible during the run. section 4.8
Testing a file API
A FileUser can request /files, retrieve individual files and records, and post file data. Random file_id and record_id values exercise different paths; checking for status code 200 exposes unexpected responses. Server output can also reveal problems such as 405 Method Not Allowed or 307 Temporary Redirect. section 4.8
Listing 4.18 Locust test
from locust import HttpUser, task, between
class FileUser(HttpUser): #1
wait_time = between(1, 2.5) #2
host = "http://localhost:8000" #3
@task #4
def get_files(self): #5
self.client.get("/files") #5
@task
def get_file(self):
self.client.get("/files/1")
@task
def post_file(self):
self.client.post("/files", json={"file_id": 5})
@task
def get_records(self):
self.client.get("/files/1/records")
@task
def get_record(self):
self.client.get("/files/1/records/1")
Use the complete Locust listing as a compact reference for defining several API tasks in one simulated user. section 4.8




