Performance testing in Python means measuring speed at two levels: load testing a running service with a tool such as Locust (written in Python) or Apache JMeter, and benchmarking or profiling code with timeit, cProfile and pytest-benchmark. Pick the tool by what you want to know: how the system behaves with many users, or why a function is slow.
Different tests answer different questions, so decide which one you need before choosing a tool.
| Test type | Question it answers | Typical tool |
|---|---|---|
| Load test | Does the system meet targets at the expected traffic? | Locust, JMeter |
| Stress test | Where does it break, and how does it fail? | Locust, JMeter |
| Spike test | How does it handle a sudden jump in users? | Locust (custom load shape) |
| Soak test | Does it leak memory or degrade over hours? | Locust, JMeter |
| Micro-benchmark | Which implementation of a function is faster? | timeit, pytest-benchmark |
| Profiling | Where is the time spent inside the code? | cProfile, py-spy |
Run load tests against a staging environment that resembles production, never against a system you do not own.
Locust describes user behaviour as ordinary Python code. Install it in a virtual environment:
Create locustfile.py. Each @task is something a simulated user does, and the number is its relative weight.
Here the home page is requested three times as often as the item list, and a slow or failing response is recorded as a failure.
Open http://localhost:8089, enter the number of users and the spawn rate (users started per second), and start the test. The interface shows requests per second, response time percentiles, failures and a live chart.
For automated runs, skip the interface and write results to CSV:
-u is the peak number of users, -r the spawn rate and -t the run time. Locust can distribute load across several machines with --master and --worker when one machine cannot generate enough traffic.
JMeter is a Java application configured through a test plan, so it needs a supported Java runtime. Download it from the official Apache JMeter site and unpack the archive.
jmeter only to build the plan.plan.jmx.Run the real test in non-GUI mode. The GUI consumes resources and distorts the results.
This writes raw samples to results.jtl and generates an HTML dashboard in report/ with percentiles, throughput and error rates.
| Aspect | Locust | JMeter |
|---|---|---|
| Test definition | Python code | GUI-built XML plan |
| Version control | Clean diffs | Hard to review XML |
| Protocol coverage | HTTP by default, other clients via code | Many built in (HTTP, JDBC, JMS, FTP) |
| Learning curve | Easy for Python developers | Steeper |
| Reporting | Web UI, CSV | HTML dashboard |
If your team writes Python, Locust fits naturally into the repository and CI. JMeter is a good choice when you need many protocols or a team already knows it.
When a load test shows a slow endpoint, measure the code itself.
timeit disables garbage collection during timing and repeats the statement, which gives more reliable numbers than manual time.time() calls. To find hotspots, run the profiler:
For a running production process, py-spy can sample a live Python process without changing the code. In tests, pytest-benchmark provides a benchmark fixture that runs a function many times and reports statistics, and it can fail a build when performance regresses.
Do not rely on averages. Look at percentiles (p50, p95, p99), because a few very slow requests hurt users while the average hides them. Watch these metrics together:
Run a baseline first, change one thing at a time, and repeat each test to rule out noise.
Neither is better in every case. Locust is simpler to maintain if you write Python, while JMeter offers more built-in protocols and a graphical plan builder.
Start from real traffic: expected peak concurrent users, then test at that level and at one and a half to two times that to find the margin. Ramp up gradually instead of starting all users at once.
Yes. Run Locust with --headless and a failure threshold, or JMeter in non-GUI mode, against a dedicated environment. Keep the runs short and compare results to a stored baseline.
Load testing measures the whole system from the outside under traffic. Profiling looks inside one process to show which functions use the time.
Use Locust or JMeter to learn how a service behaves under load, and run them headless or in non-GUI mode for real measurements. Use timeit, cProfile, py-spy and pytest-benchmark to find and fix slow code, judge results by percentiles and error rates, and always compare against a baseline.