liveBook · Manning

Load testing

As explained in Build Financial Software with Generative AI (From Scratch)

Load testing is nonfunctional testing that evaluates an application's behavior and performance under simulated user or request loads.

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

How Build Financial Software with Generative AI (From Scratch) covers it

How deep it goes
Walks through Locust setup, endpoint scripting, dashboard configuration, randomized inputs, and assertions, but leaves richer API data for later.
Examples
Exercises a FastAPI file API through Locust tasks, randomized path parameters, response assertions, dashboard runs, and server logs.
What it assumes
Assumes basic Python and API familiarity while building Locust usage from installation through endpoint requests and assertions.
What you can do after
Operate Locust to script API workloads, run simulated users, inspect metrics, and diagnose failing status codes or routing behavior.
How it connects
Connects nonfunctional testing with API testing, performance bottlenecks, automated testing, and continuous integration and delivery.

Also explained in

Compare how 5 books explain load testing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Financial Software with Generative AI (From Scratch). The section links point to the parts of the book it draws on.