Skip to content

Speed up the Python tests in debug and sanitizer builds - #8860

Merged
danlapid merged 10 commits into
mainfrom
dlapid/faster-pyodide-tests
Nov 25, 2026
Merged

danlapid merged 10 commits into
mainfrom
dlapid/faster-pyodide-tests

Conversation

@danlapid

@danlapid danlapid commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

The Python tests are among the slowest in the debug and sanitizer builds. Each commit fixes one cause:

  • asgi: the test slept 20 seconds twice to let errors get logged, so it took over 40 seconds in every build, release included. It now waits for the ERROR record itself.
  • durable-object-websocket: the Durable Object never replied to the tester's Close frame, so each run waited 10 seconds for the idle Durable Object to be evicted. The Durable Object now closes its end, and the tester waits for the close event.
  • Liftoff only: V8 tiers Pyodide's hot Wasm functions up to TurboFan on background threads. Most of these tests start from a snapshot and run little Python, so tiering up rarely pays off, while each test is scheduled as one CPU. With --liftoff-only, a workerd run in debug and TSan builds uses 1.3 to 2.5 times less CPU, and its wall time on an idle machine stays about the same. The JSPI and stack-switching tests keep V8's default tiering.
  • worker-loader-python-test: each of its four cases starts Pyodide from scratch in a dynamic Worker. Each case is now a target of its own, selected with workerd test's filter, so they run in parallel, and the test reads the Pyodide bundles from the disk cache that py_wd_test uses rather than downloading them, so it no longer needs network access.
  • 0.26.0a2 variants: 0.26.0a2 can't stack a snapshot on its baseline snapshot, so each 0.26.0a2 variant created its snapshot by starting Pyodide from scratch, over 10 CPU-seconds in debug builds. They now just load the baseline snapshot.
  • Baseline snapshot creation: no test created a baseline snapshot in the newer versions, whose variants stack a dedicated snapshot on the released baseline one. The new top-level-entropy-create-baseline target (create_baseline_snapshot in py_wd_test) starts Pyodide from scratch, creates a baseline snapshot and restores it, in every version.

The table compares typical (median) times before this change with the average and worst of 5 runs after it. The "after" runs covered every Python test and variant at once, about 800 runs sharing the executors:

TSan before TSan after (avg / worst) debug before debug after (avg / worst)
asgi (each version) 54–60 s 8–15 s / 23 s 51–56 s 8–21 s / 37 s
worker-loader-python-test 33–36 s 12–20 s / 43 s per case 33–34 s 14–20 s / 58 s per case
durable-object-websocket 20–23 s 8–11 s / 16 s 20–21 s 13–17 s / 29 s
slowest Python test 22 s / 33 s 28 s / 52 s

Loading the baseline snapshot halved the 0.26.0a2 variants, measured in a separate 5-run pass: debug average 19.9 s → 10.0 s, worst run 26.9 s → 15.9 s; TSan average 14.7 s → 9.2 s. top-level-entropy-create-baseline averages 20–26 s per version in debug builds and 15–27 s in TSan over 5 runs; the worker-loader-python-test figures come from the same, more heavily loaded pass, in which even hello_0.28.2 (about 7 s on an idle executor) had runs of over 30 s.

🤖 Generated with Claude Code

@codecov-commenter

codecov-commenter commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.87%. Comparing base (1110a5a) to head (de3bf5e).
⚠️ Report is 125 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8860      +/-   ##
==========================================
+ Coverage   67.53%   69.87%   +2.33%     
==========================================
  Files         871      881      +10     
  Lines      220178   231888   +11710     
  Branches    32916    33652     +736     
==========================================
+ Hits       148700   162026   +13326     
+ Misses      52041    49899    -2142     
- Partials    19437    19963     +526     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@danlapid
danlapid force-pushed the dlapid/faster-pyodide-tests branch 2 times, most recently from af8e477 to 458323d Compare November 24, 2026 19:02
danlapid and others added 6 commits November 24, 2026 22:17
The asgi Python test slept 20 seconds twice to let the event loop log
an error, so every run took over 40 seconds in every build. The log
handler now sets an event when it receives an ERROR record, and the
test waits on that. An error after the response is logged before
asgi.fetch() returns, and a failed background task is logged by its
done callback a few event loop turns later.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tester closed its WebSocket, but the Durable Object never replied
to the Close frame, so the test request stayed open until the idle
Durable Object was evicted 10 seconds later. The Durable Object now
closes its end in webSocketClose(), and the tester waits for the
close event.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
V8 tiers hot Wasm functions up to TurboFan on background threads.
Most Python tests start Pyodide from a memory snapshot and then run
little Python, so that compilation rarely pays for itself, while the
tests are scheduled as using one CPU. In debug and TSan builds, where
V8's compilers are slow, a workerd run of these tests used 1.3 to 2.5
times the CPU time it uses with --liftoff-only, and its wall time on
an idle machine was about the same. On a busy machine the extra
threads only compete with other tests. Runs that start Pyodide
without a snapshot run more Python and take somewhat longer on an
idle machine, but still use less CPU.

The JSPI and stack-switching tests keep V8's default tiering, so that
stack switching stays covered with TurboFan code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When Bazel runs a test in shards (shard_count), it tells each shard
its index and the shard count in TEST_SHARD_INDEX and
TEST_TOTAL_SHARDS, and expects the runner to touch
TEST_SHARD_STATUS_FILE to show that it shards. workerd test now does
that and runs only every n-th test case, so that a wd_test whose
cases are slow can spread them over several processes. A shard left
without cases, when there are fewer cases than shards, passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each of its four cases starts Pyodide from scratch in a dynamic
Worker, which takes several seconds in debug and sanitizer builds, so
the test took 25-40 seconds there. It now runs each case in a shard of
its own, and, like the other Python tests, runs Wasm with Liftoff
only, so a shard needs one CPU rather than two.

It also reads the Pyodide bundles from the disk cache that
py_wd_test provides, through the new PYODIDE_BUNDLE_ARGS and
PYODIDE_BUNDLE_DATA, rather than downloading them, so it no longer
needs network access.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
0.26.0a2 can't stack a snapshot on its baseline snapshot, so every
0.26.0a2 variant of a py_wd_test created its snapshot by starting
Pyodide from scratch, which takes over 10 CPU-seconds in debug builds
and made these variants the slowest Python tests. They now only load
the baseline snapshot, in a single workerd run.

The hello test keeps the from-scratch path for 0.26.0a2, through the
new create_0_26_snapshot_from_scratch parameter, so that creating a
0.26.0a2 snapshot and restoring it stay covered. It is the smallest
worker whose top level runs checks while the snapshot is collected
and whose handler runs after the restore.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danlapid
danlapid force-pushed the dlapid/faster-pyodide-tests branch from 458323d to 3ce1722 Compare November 25, 2026 03:20
@danlapid
danlapid marked this pull request as ready for review November 25, 2026 20:08
@danlapid
danlapid requested review from a team as code owners November 25, 2026 20:08
@danlapid
danlapid enabled auto-merge November 25, 2026 20:10

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 008e0571.

This review, of 3ce1722e

💬 Approved with comments — The changes to the test infrastructure and the new Bazel sharding support look correct. The only note is an optional suggestion to unit-test the shard environment-variable parsing.

Findings: 1 suggestion (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 suggestion

The changes look correct. Optional: add a unit test for the shard environment-variable parsing.

Recommendation: 💬 Optional Nits

  • [SUGGESTION] bazel_test_shard() has no unit tests; only shard_cases() does. (src/workerd/server/server/run.rs:739)
View full review
  • run-test.rs tests shard_cases but not bazel_test_shard(). That function holds the protocol handling: both variables must be present, the status file is touched, the values are parsed and index >= total is rejected. Moving the string-to-(index, total) parsing into a pure helper would make it testable without changing process environment variables.

These parts were checked and look correct: the shard assignment is deterministic, a shard with no cases passes while an unmatched pattern still fails, the 0.26.0a2 variants load a real baseline snapshot, --liftoff-only is applied consistently, and the asgi and WebSocket test changes are sound.


✅ All other checks passed

🔒 Security: ✅ LGTM

The change only touches test infrastructure. The shard values come from environment variables set by the test runner, and they are validated before use. The status file is only created or touched.


📖 AGENTS.md: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 3ce1722e

Comment thread src/workerd/server/server/run.rs Resolved
Move the parsing of TEST_SHARD_INDEX and TEST_TOTAL_SHARDS out of
bazel_test_shard() into parse_shard(), which takes the values rather
than reading the environment, and test that it parses valid values
and refuses non-numbers and an index that isn't below the total.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/pyodide/tests/BUILD.bazel Resolved

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of de3bf5ee.

This review, of 008e0571

✅ Approved — No findings: the one earlier suggestion, unit tests for the shard-variable parsing, is fixed by parse_shard() and its new tests.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

This pull request only changes test infrastructure. Bazel's test runner sets the shard variables, and they are checked before use. The shard status file is only created or touched.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The earlier suggestion is fixed: parse_shard() holds the parsing and the index < total check, and run-test.rs tests it.

I checked these parts and found nothing wrong:

  • Sharding: a shard with no cases passes, and a pattern that matches nothing still fails.
  • Bundle loading: PYODIDE_BUNDLE_ARGS and PYODIDE_BUNDLE_DATA are reused correctly in src/workerd/api/tests/BUILD.bazel.
  • asgi and WebSocket tests: the asgi wait-for-error change and the WebSocket close handshake work as described.
  • 0.26.0a2 variants: they load the baseline snapshot, and hello keeps the path that creates its snapshot from scratch.

⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 008e0571

@hoodmane hoodmane left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM aside from the question about test coverage for baseline snapshot creation.

danlapid and others added 3 commits November 25, 2026 15:35
Instead of sharding the test, define a target per case that selects
it with `workerd test`'s filter, so the cases still run in parallel
without `workerd test` needing to know about Bazel's sharding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
workerd test read TEST_SHARD_INDEX and TEST_TOTAL_SHARDS from its own
environment, so it also sharded the cases of tests that shard
themselves: a sharded C++ test that runs `workerd test` in its own
process lost cases, and a wd_test whose harness divides its work
between shards (wd_test's supports_sharding) ran only part of its
cases in each shard. Its one user now runs each case as a separate
target instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each py_wd_test variant of a newer Pyodide version stacks a dedicated
snapshot on the released baseline one, so no test created a baseline
snapshot in those versions, and the 0.26.0a2 variants now load the
released one too. py_wd_test's create_baseline_snapshot makes every
variant start Pyodide from scratch, create a baseline snapshot and
restore it, and top-level-entropy-create-baseline uses it: its top
level runs after the restore, as it does in
top-level-entropy-baseline. hello no longer needs the 0.26.0a2-only
option this replaces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No findings. The changes since the last review (create_baseline_snapshot, a target per worker-loader case, sharding removed from workerd test) look correct, and the earlier suggestion stays fixed.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The pull request only changes test macros, .wd-test configs and test workers. It adds no secrets and changes no access control. The worker-loader test now reads the Pyodide bundles from the local disk cache, so it no longer needs network access.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The changes since the last review in src/pyodide/tests/BUILD.bazel, src/pyodide/tests/py_wd_test.bzl and src/workerd/api/tests/BUILD.bazel look correct.

  • create_baseline_snapshot: it sets use_snapshot = None and keeps make_snapshot, so the snapshot test still runs. In that case python_dedicated_snapshot is not added, which is correct.
  • 0.26.0a2 variants: they load the baseline snapshot with make_snapshot = False.
  • Per-case worker-loader targets: the four case names match the exports in worker-loader-python-test.js. The service:entrypoint filter is a format workerd test accepts. Only one target lints the shared source file.
  • Bundle arguments: PYODIDE_BUNDLE_ARGS and PYODIDE_BUNDLE_DATA use absolute labels, so they resolve from src/workerd/api/tests.
  • asgi: the error-wait timeout is caught, so the existing assertions still report a failure.

On the question in the discussion about coverage for creating a baseline snapshot: the new top-level-entropy-create-baseline target covers it in every Pyodide version.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: de3bf5ee

@danlapid
danlapid disabled auto-merge November 25, 2026 20:59
@danlapid
danlapid merged commit 06a49ec into main Nov 25, 2026
24 of 25 checks passed
@danlapid
danlapid deleted the dlapid/faster-pyodide-tests branch November 25, 2026 20:59
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.

3 participants