Designing Effective Load Tests for Your Python App
Episode Deep Dive
Opening Stories and Analogies
- Anthony and Michael begin by sharing real-world stories where load or performance testing was overlooked.
- Examples include the UK “halftime kettle” electricity spikes, the Australian online census crash, and unexpected costs from serverless usage.
Defining Load Testing and Avoiding Common Mistakes
- Load testing isn’t just about finding a raw requests-per-second number; it’s about simulating real user behavior.
- Users click around, pause, log in, log out, and navigate in non-linear ways. Simple benchmarks often miss these nuances.
Tools and Approaches
- Locust
- Website: locust.io
- A Python-based load testing tool that lets you write user flows (tasks) in code.
- Supports weighting of tasks (e.g. “homepage” more common than “checkout”), randomizing inputs, and ramping up traffic.
- Playwright
- Website: playwright.dev
- Typically for UI testing but can integrate with Locust for full browser-based load tests.
- Automates realistic user actions (filling forms, clicking, etc.).
- Azure Load Testing
- Overview: azure.microsoft.com/products/load−testing
- A cloud service that can run large-scale distributed load tests.
- Planned support for Locust-based scripts (in addition to other tools like JMeter).
- WhiteNoise (for Django static files)
- Docs: whitenoise.evans.io/en/stable/
- A convenient way to serve static files but not ideal for production-level static hosting.
- Mentioned as a potential culprit if not properly optimized (too many requests served by Python).
- Locust
Key Concepts to Consider in Load Testing
- Spikes vs. Ramping:
- Predictable spikes (e.g., halftime or major TV events) vs. gradually increasing traffic.
- Configure “ramp-up” in load test tools to see at what point the system starts to degrade.
- Caching and CDNs:
- Evaluate caching for both static resources (CSS/JS) and dynamic data.
- Watch out for dynamic pages (e.g., logged-in user pages) that may bypass caches.
- Database Bottlenecks:
- N+1 query issues and under-indexed tables can slow everything down under load.
- Look at actual traffic and query patterns to find the real performance hotspots.
- Data Volume for Realism:
- Use fake data generators like Mimesis mimesis.name to seed databases.
- Test with production-like database sizes, not just a handful of rows.
- Spikes vs. Ramping:
Observability and Monitoring
- OpenTelemetry
- Website: opentelemetry.io
- Collects unified metrics, logs, and traces across your app.
- Helps pinpoint bottlenecks revealed by load tests (e.g., slow database queries, external API rate limits).
- OpenTelemetry
Handling Serverless and Cloud Nuances
- Serverless platforms like Azure Functions or AWS Lambda often have cold-start times.
- You can pay for “always-on” features or pre-warmed instances, but that blurs the line of “serverless.”
- Monitor cost as well as performance (avoid scenarios like the unexpected huge bill on Vercel).
Uptime Tools
Mention of Uptime Kuma github.com/louislam/uptime−kuma
- Continuously monitors website status over time.
Useful for seeing real-world performance trends and any spikes in latency or downtime.
Overall Takeaway
Effective load testing in Python requires more than just raw throughput numbers. You need to simulate realistic usage patterns, seed your database with production-like data, and instrument the app for visibility. Tools like Locust, Playwright, Mimesis, and OpenTelemetry (along with cloud or local monitoring solutions) can help reveal the true performance and reliability of your web apps or APIs under real-world conditions.
Links from the show
Anthony's PyCon Au Talk: youtube.com
locust load testing tool: locust.io
playwright: playwright.dev
mimesis: github.com
mimesis providers: mimesis.name
vscode pets: marketplace.visualstudio.com
vscode power-mode: marketplace.visualstudio.com
opentelemetry: opentelemetry.io
uptime-kuma: github.com
Talk Python uptime / status: talkpython.fm/status
when your serverless computing bill goes parabolic...: youtube.com
Watch this episode on YouTube: youtube.com
Episode transcripts: talkpython.fm
--- Stay in touch with us ---
Subscribe to Talk Python on YouTube: youtube.com
Talk Python on Bluesky: @talkpython.fm at bsky.app
Talk Python on Mastodon: talkpython
Michael on Bluesky: @mkennedy.codes at bsky.app
Michael on Mastodon: mkennedy