Skip to content

Faster backend unit tests: cut repeated work and real waits, run in parallel - #136

Merged
mlkonopelski merged 1 commit into
mainfrom
fix/faster-backend-unit-tests
Dec 2, 2026
Merged

mlkonopelski merged 1 commit into
mainfrom
fix/faster-backend-unit-tests

Conversation

@mlkonopelski

Copy link
Copy Markdown
Contributor

Why

The CI job Unit (python services) took 20-28 min of pytest in recent runs; every other job takes 8 min or less. An audit of all 532 backend test files (14,512 cases) showed that the time does not come from a few slow tests: deleting every low-value test would save under a minute. It comes from work repeated in thousands of tests, a few real waits, and the serial run.

What changes

  • Repeated parsing: the merged ontology, plan rules, gap rules and the shop fixture are parsed once per process in the test support modules instead of on every call (thousands of YAML parses at about 60 ms each). Ontology, PlanRules, GapRules and the shop models are frozen, so sharing is safe. The ontology loader is based on yaml.CSafeLoader (libyaml, about 10x faster); duplicate keys are still rejected with their line. This is the only src/ change.
  • Real waits: the stub runtime skips the 0.5 + 1 + 2 s clock-read retry backoff (35 tests: 31 s to 0.7 s); offline Temporal replayers get a build_id (the first replay hashed every loaded module, about 4.7 s); the JWKS, readyz and logger-overflow tests use short timeouts and still assert the production values; the Drive perf test computes the 300 ms-per-call bound instead of sleeping it.
  • Parallel safety: a random uuid4() in the bad-cursor parametrize ids made each xdist worker collect different tests, which aborts the run; it is now a fixed UUID. The worker-runner tests no longer start the health server on the fixed port 8001 (the health test that needs it restores the real server).
  • Parallel run: pytest-xdist in the dev group; make unit-test runs pytest -n auto --dist loadfile (each file stays in one process, so module fixtures load once). A plain uv run pytest <file> is unchanged.

How it was checked

Local, macOS (the CI runner is slower, so the CI time of this PR is the real number):

Run Before After
Serial 13 m 38 s 6 m 32 s
Total test time 760 s 371 s
4 xdist workers - 2 m 14 s

13,683 passed in every run, the same count as before. The only failures are the 3 known macOS-only process-group tests in test_mcp_qa_lib.py (os.pidfd_open), which pass on Linux. make check and the Python validation tracks (validate-python-lint, -scripts, -arch, validate-licences) pass.

Follow-ups

  • Delete or merge about 60 low-value tests (constant snapshots, tests without asserts, duplicate matrices) and move source scans that ruff, semgrep or import-linter already cover; plus the remaining speed-ups (shell- and interpreter-spawning tests, repeated Docling parses).
  • Split the job into CI shards only if it is still slower than the E2E shards after this change.

…rallel

The backend unit job ran 20-28 min in CI. Most of the time went to work repeated in
thousands of tests and to a serial run, not to slow individual tests.

- Parse the merged ontology, plan rules, gap rules and the shop fixture once per
  process in the test support modules (Ontology, PlanRules, GapRules and the shop
  models are frozen). Base the ontology YAML loader on libyaml (CSafeLoader).
- Skip the 3.5 s clock-read retry backoff in the stub runtime, give offline Temporal
  replayers a build_id, and use short timeouts in the JWKS, readyz and logger
  overflow tests (production values are still asserted). No real latency in the
  Drive perf test.
- Worker-runner tests no longer start the health server on the fixed port 8001.
- Fixed UUID in the bad-cursor parametrize ids: a random one made every xdist worker
  collect different tests.
- Add pytest-xdist; make unit-test runs -n auto --dist loadfile.

Local (macOS): serial 13m38s -> 6m32s, 4 workers 2m14s; 13,683 passed, same as before.
@mlkonopelski
mlkonopelski merged commit 1636e3c into main Dec 2, 2026
18 checks passed
@mlkonopelski
mlkonopelski deleted the fix/faster-backend-unit-tests branch December 2, 2026 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants