Learn Python with Talk Python's 270 hours of courses

Designing Effective Load Tests for Your Python App

Episode #479, published Sun, Oct 6, 2024, recorded Thu, Aug 8, 2024

You're about to launch your new app or API, or even just a big refactor of your current project. Will it stand up and deliver when you put it into production or when that big promotion goes live? Or will it wither and collapse? How would you know? Well you would test that of course. We have Anthony Shaw back on the podcast to dive into a wide range of tools and techniques for performance and loading testing of web apps.

Watch this episode on YouTube
Play on YouTube
Watch the live stream version

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

    1. 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.
    2. 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.).
    3. Azure Load Testing
    4. 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).
  • 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.
  • 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).
  • 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

  • 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 on Twitter: @anthonypjshaw
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

Talk Python's Mastodon Michael Kennedy's Mastodon