Skip to content

pytest-async-benchmarkModern pytest benchmarking for async Python

Built specifically for async def coroutines. True latency percentiles (p50-p99), concurrency load testing, task leak detection, and statistically sound regression tests.

pytest-async-benchmark logo

Quick Example

Benchmark async code with natural pytest syntax ​

Install pytest-async-benchmark and benchmark your coroutines with standard pytest fixtures:

python
import asyncio
import pytest


async def fetch_user_data(user_id: int):
    await asyncio.sleep(0.005)  # simulated network I/O
    return {"id": user_id, "status": "active"}


@pytest.mark.asyncio
async def test_fetch_latency(async_benchmark):
    # Benchmark the coroutine function with auto-calibrated runs
    result = await async_benchmark(fetch_user_data, 42)

    # Assert on real p95/p99 percentiles or throughput
    assert result["p95"] < 0.010
    assert result["ops"] > 100
pytest-async-benchmark terminal output

Concurrency Benchmarking

Measure throughput and event loop lag under real concurrency ​

Sequential benchmarks tell you how fast code runs in isolation; concurrency benchmarks tell you how it behaves under load.

python
@pytest.mark.asyncio
async def test_api_concurrency(async_benchmark):
    # Run 100 total requests with 20 requests in flight simultaneously
    stats = await async_benchmark.concurrent(
        fetch_user_data,
        42,
        concurrency=20,
        total=100,
        leak_check=True,
    )

    print(f"Throughput: {stats.ops:.1f} ops/sec")
    print(f"p99 Latency: {stats.p99 * 1000:.2f} ms")
    print(f"Loop Lag: {stats.loop_lag * 1000:.2f} ms")
pytest-async-benchmark concurrency results

CI Regression Guard

Catch performance regressions before they land in production ​

Save historical baselines and prevent silent slowdowns in CI. To eliminate false alarms from noisy cloud runners, pytest-async-benchmark applies a one-sided Mann-Whitney U test across individual call samples, only failing your pipeline when a regression is both substantive and statistically significant ($p < 0.05$).

bash
# Compare against baseline and fail if median latency regresses by >10%
pytest --async-bench-compare --async-bench-compare-fail=median:10%
pytest-async-benchmark CI regression detection

Zero CI Flakiness

Replaces fragile fixed cutoffs with non-parametric Mann-Whitney U testing, ignoring transient VM neighbor spikes.

Automated Baselines

--async-bench-autosave records numbered runs in .async_benchmarks/ with Git commit SHA and host hardware metadata.

JSON Export

Export all raw percentiles and per-call sample arrays via --async-bench-json for dashboard ingestion and PR comments.

From the Author

Deep dives into async architecture and performance ​

Articles written by Mihai Farcas on building performant Python architectures, AI agents, and type systems:

Written by , author and maintainer of pytest-async-benchmark. Follow along on GitHub.

Latest Updates

What's new in pytest-async-benchmark ​

v0.4.1November 12, 2026