Python Load Testing with Locust — Core Concepts

What Locust does

Locust is an open-source load testing framework written in Python. It simulates many users accessing your application simultaneously, measuring how the system performs under pressure.

Unlike tools that replay recorded HTTP traffic, Locust uses Python code to define user behavior. Each simulated user runs your Python script independently, making decisions, waiting between actions, and hitting different endpoints — just like a real person would.

Core concepts

Users: Each simulated user is a lightweight greenlet (coroutine). Locust can run thousands on a single machine because greenlets consume far less memory than threads or processes. A typical laptop can simulate 5,000-10,000 concurrent users.

Tasks: Actions that users perform. A task might be “visit the homepage,” “search for a product,” or “submit a form.” You define tasks as Python methods and assign them weights to control how often each action occurs.

Wait time: The pause between tasks. Real users don’t click instantly — they read pages, think, and move their mouse. Locust lets you configure realistic wait times (e.g., 1-5 seconds between actions) so the simulation matches real usage patterns.

Host: The target application URL. Locust sends all requests to this host, and you can switch targets without changing test code.

What Locust measures

During a test run, Locust tracks:

  • Response time (median, 95th percentile, 99th percentile) — how long requests take
  • Requests per second — throughput your system achieves
  • Failure rate — percentage of requests that return errors
  • Active users — how many simulated users are running

The web dashboard shows these metrics in real time. You watch response times climb as you add users, and you see exactly when the system hits its limit — the point where response times spike or errors start appearing.

How it differs from other tools

JMeter uses XML configuration and a Java GUI. Powerful but complex. Locust uses Python scripts — more flexible and easier to version control.

k6 uses JavaScript. Good for developers in the JavaScript ecosystem, but Python teams prefer staying in their language.

ab (Apache Bench) is great for simple URL hammering but can’t simulate complex user journeys with logins, sessions, and multi-step flows.

Locust’s Python-native approach means you can use any Python library in your tests — database queries, API clients, custom authentication flows. If Python can do it, Locust can test it.

Distributed testing

When one machine isn’t enough, Locust supports distributed mode. A master node coordinates while worker nodes generate load. Adding workers scales linearly — 4 workers generate roughly 4x the load of a single machine.

This lets teams simulate hundreds of thousands of concurrent users for large-scale applications. Cloud providers like AWS and GCP make it easy to spin up worker nodes on demand for test runs.

When to use load testing

Load testing belongs in the development cycle at specific moments:

  • Before launch: Verify the system handles expected traffic
  • Before marketing campaigns: A viral post can 10x traffic overnight
  • After architecture changes: New database, new cache layer, new deployment — validate performance didn’t regress
  • Regularly: Performance degrades gradually as features are added. Monthly load tests catch slow regressions

One thing to remember: Load testing reveals the gap between “it works” and “it works at scale.” Most applications work fine for 10 users — the question is whether they work for 10,000.

pythontestingperformance

See Also