Wait-bound / IO suites¶
A playbook for suites whose time goes to waiting (sleeps, network, timeouts) rather than computing, where rstest gains the most.
Who this is for¶
You maintain a backend suite that spends most of its time waiting (on
sockets, HTTP calls, database round-trips, sleep()s, and timeouts), not
computing. This is rstest's biggest-win case: waits overlap, so more tests
can be in flight at once than you have cores.
One-line self-check: run rstest --doctor. If it prints a
WAIT-BOUND line, this page is for you.
1. Confirm you're wait-bound¶
rstest --doctor runs your suite once and reports where the
time actually goes, from timing the runner already owns (per-test wall
time, per-test CPU time, per-fixture setup). The two sections that matter
here:
- WAIT-BOUND: compares each test's wall time against its CPU time. A
test whose wall time vastly exceeds its CPU time isn't computing; it's
sleeping or waiting on IO/a timeout. Both cover the whole test,
fixture setup and teardown included, and CPU time includes child
processes the test waited for (on Windows, every child it started), so a
test that runs a CPU-heavy CLI through
subprocess.runcounts as computing, not waiting (oversubscribing-nwill not help it). Doctor prints the share of test time spent waiting and names the worst offenders. In one real suite (aiohttp) this was 95% of test time (176.5s) waiting, almost all of it on 10-second proxy timeouts. - PARALLEL FLOOR: no worker count can finish faster than the longest single test. If your longest test exceeds the ideal per-worker share (at least 1 second) by more than 10%, doctor names the gate tests; splitting or shrinking them is the only way to lower that floor (adding workers won't).
If WAIT-BOUND dominates, the fastest lever is often to fix the waits (mock the clock, shrink the timeout, use event-driven waits). Doctor calls this out as usually the single biggest speedup in a suite. But even without touching a test, you can overlap the existing waits harder by tuning the worker count (next section).
2. Tune the worker count¶
Here is the key move for a wait-bound suite, and it is counter-intuitive:
-n autoonly ever caps downward. It never exceeds your logical core count, and on a small or few-file suite it settles below it (how it sizes the pool).- An explicit
-n Nis a fixed count, regardless. Pin-n <k>and you get exactlykworkers; the auto cap does not apply. So a wait-bound suite can set-nabove the logical core count to keep more waits in flight at once, since a waiting worker isn't using a core.
Doctor's PARALLEL EFFICIENCY section shows this as a realized speedup
above the logical core count (for example 21.0x realized of 32x possible
on a 14-core machine): overlapping sleeps and IO run more tests at once than
there are cores. The efficiency percentage is relative to the worker count,
so it stays at or below 100%. (The section is -n ≥ 2 only.)
The scheduler helps here too: it dispatches
slow tests first (a cached duration of 1s or more), longest first, so a
54-second waiter starts at t=0 instead of stacking behind other work.
The tuning loop. Raise -n past your core count and watch two doctor
numbers until they flatten:
$ rstest -n 16 --doctor # start above cores for an IO-heavy suite
$ rstest -n 24 --doctor # keep raising while wall time keeps dropping
$ rstest -n 32 --doctor # stop when wall time flattens / stops improving
Each run, read: wall time (the header line, wall Xs) and the
WAIT-BOUND %. Keep raising -n while wall time keeps falling; stop
when it flattens. Two things put a floor under it, both named by doctor:
the PARALLEL FLOOR long pole (a single test no -n can beat), and the
point where you've run out of overlappable waiting. Once tuned, you can
guard the outcome in CI against regressions with a doctor metric that
tracks how well the run parallelized, e.g. --doctor-fail-on
'parallel_efficiency<N' or a wall_seconds ceiling
(--doctor-fail-on). (Don't
gate on wait_pct for this; it measures how wait-bound the suite is,
not whether the worker count is well-tuned.)
Timing-sensitive tests degrade under high -n
This oversubscription trick is safe because the work is waiting, not
computing. Tests that assert on rate-limit windows, token expiries, or
tight elapsed-time bounds can degrade under high -n; that's load, not
ordering. Contain them with @pytest.mark.serial, a clock mock, or a
capped -n; see Parallel safety.
Arm a hang backstop. A wait-bound suite is exactly the one that hits a
real hang: a socket that never returns, a timeout that never fires. Set
--worker-timeout: a test that
exceeds it is reported failed with a timeout message, its worker is killed
and replaced, and that worker's remaining tests redistribute so the run still
finishes. Concurrency exposes these; without a backstop one hung test can
stall a whole worker for the length of the run. A per-test cap,
@pytest.mark.timeout, is the finer-grained tool (see
Markers).
An explicit --worker-timeout replaces the per-test hang watchdogs that
--timeout and @pytest.mark.timeout arm (3× the test's timeout + 10s) with
one fixed limit for every test, so set it above your longest
mark.timeout, or that test is killed before its own timeout fires. See
Hung tests.
3. Per-worker isolation (DB / ports)¶
More workers means more concurrent copies of every shared resource: a session-scoped fixture runs once per worker, so N workers means N databases, N servers, N bound ports (Session-scoped fixtures duplicate). Each worker's resources must be safe to duplicate:
- Ports: bind port
0(let the OS assign) instead of a fixed port. - Databases / directories: derive a per-worker name or path from
RSTEST_WORKER_ID(gw0,gw1, ...) or xdist'sworkerinput; see Worker identity.
pytest-django suffixes the test database per worker automatically. As you
raise -n, rstest --doctor flags the expensive session fixtures that ran
once per worker, so you can spot the ones that aren't per-worker-safe yet.
4. Measure the real win¶
Before committing to anything, get your numbers with
rstest try:
It runs your suite once under plain pytest and once under
rstest -n auto, then reports whether outcomes are identical and how
much faster rstest is. The baseline needs pytest installed in the
project environment (python -m pytest must work), not just rstest.
Because try uses -n auto, a wait-bound suite's real ceiling is
higher, so treat the try speedup as a floor and then run the
-n-above-cores tuning loop from section 2 to find the actual best wall
time.
Go deeper¶
- Suite diagnostics: every doctor section (WAIT-BOUND, PARALLEL FLOOR, PARALLEL EFFICIENCY, fixture hotspots, slowest files, coverage waste, resource leaks, network access, timeouts, duration drift) and the JSON/markdown/CI-gate outputs.
- Scheduling: slow-tests-first dispatch and why it beats collection-order schedulers on wait-heavy suites.
- Parallel safety: per-worker isolation, the
@pytest.mark.serialescape hatch, and time-sensitive tests at high concurrency. - CLI reference:
-n,--dist,--timeout,--worker-timeout, and the doctor flags. - Migrating from pytest: the one-line switch
and the
-n 0single-worker escape hatch. - Environment variables: the full
RSTEST_WORKER_ID/workerinputcontract.