Skip to content

Make the slowest stream, snapshot and bench tests faster - #8863

Merged
danlapid merged 2 commits into
mainfrom
dlapid/faster-slow-tests
Nov 25, 2026
Merged

danlapid merged 2 commits into
mainfrom
dlapid/faster-slow-tests

Conversation

@danlapid

@danlapid danlapid commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Several tests spent most of their time on work that added no coverage. Under TSan, or in debug builds, they took tens of seconds and sometimes timed out. This PR makes them fast without dropping any assertion.

Test Config Before After
digest-cpp, digest-ts, digest-cpp-pedantic TSan 51–76s 0.7–3.4s
digest-gc-pressure-{cpp,ts} (new) TSan — 1.9–14.3s
streams/termination-injection-test TSan 25–26s 7 shards: max 19s, avg 6s; @all-autogates avg 7–9s, max 26–42s on busy executors
js-readable-stream-test@all-autogates TSan 18–20s 4 shards: avg 9s, max 25–29s
startup-snapshot-globals-test@all-autogates TSan 24–25s 7–17s per config, avg 9–10s
body-cpp@smoke, body-ts@smoke TSan 15–25s avg 5.6–8.5s, max 24s on busy executors
body-cpp@all-autogates@smoke debug 35s avg 4.7s, max 7.9s
transform-cpp@smoke (all variants) TSan up to 9.6s, throughput alone 7s avg 2.0–3.9s, max 10.4s
piping-cpp@smoke TSan 20s, pipeThrough alone 12s avg 3.8s, max 12s
Stream @smoke tests changed here (36 targets) debug up to 35s max 14.6s, avg ≤6s
bucket-test debug 4 of 60 runs timed out at 60s; others took 45–55s max 4.4s over 90 runs
bench-json debug up to 33s 0.1s

Timings are from remote test executors. "After" ranges cover 5 runs per variant, and 20 runs per variant for the @smoke tests, whose "Before" is one run of each; bucket-test covers 30 runs each of @, @all-compat-flags and @all-autogates. Every changed target also passed 5 runs under debug and ASan, where the slowest took 13.7s.

Changes

  • digest: gcDuringCloseOfDroppedStream made 30,000 streams and allocated 30 GB in 1 MiB buffers. That was 99.5% of each digest suite's time, and it never reproduced the use-after-free it was written for: 0 of 50 runs without the fix. The case now runs alone in digest-gc-pressure-{cpp,ts}.wd-test under --stress-incremental-marking. Keeping marking in progress lets cppgc's allocation-limit check finish a GC inside the close sink, which destroys the stream under it. Without the fix, the new config crashed in every ASan run (30 of 30) and in 19 of 30 debug runs, after about 10,000 streams.
  • termination-injection-test: it was a single test case looping over independent scenarios. The scenarios are now split between 7 cases, which run in 7 shards (kj_test's shard_count).
  • js-readable-stream-test: its 117 cases, each with a fixture of its own, run in 4 shards.
  • startup-snapshot-globals-test: its 29 cases now run in three configs: transferable-v8, transferable-runtime and fallback. Their Workers, each built from a zygote and then from its snapshot, start in parallel. The case services live in startup-snapshot-globals-services.capnp. The test no longer needs size = "large".
  • bucket-test: pattern() computed 57 MiB one byte at a time in JavaScript. In the slow debug runs every data-generating step got 40–100x slower and stayed slow. That matches V8 leaving the loop unoptimized: about 1.3 µs per byte. Streams are now copied from one precomputed run. As a side benefit, the 8 MiB parts of a multipart write now differ from one another, so a completion that reordered them would fail the digest check; before, parts 1 and 2 were identical. The test no longer needs size = "large".
  • workerd bench --quick (wd_bench @smoke) now takes 2 samples instead of 5 and does no warmup. It checks that each case runs, not how fast it runs.
  • Stream benchmarks under --quick: cases that moved 1 MiB bodies or hundreds of chunks per call took 0.3–1.4s per call in TSan and debug builds. Under --quick they now move a sixteenth as many bytes or chunks, and their names show the amounts a run used. This covers transform throughput; piping pipeThrough and pipeTo; body read, pipe, clone, serviceBinding and htmlRewriter; identity throughput and pipe; readable from, asyncIteration and tee; and strategies queueAccounting. src/bench/streams/AGENTS.md asks new cases like these to do the same.
  • bench-json: each benchmark iteration used to time 10,000 or 100,000 inner repetitions, so one iteration took seconds in a debug build. Each iteration is now a single encode or decode.

🤖 Generated with Claude Code

@danlapid
danlapid force-pushed the dlapid/faster-slow-tests branch from bb84aa9 to cbb7222 Compare November 24, 2026 05:37
@danlapid
danlapid force-pushed the dlapid/faster-slow-tests branch from cbb7222 to 3a8b88a Compare November 25, 2026 03:18
@danlapid
danlapid marked this pull request as ready for review November 25, 2026 18:12
@danlapid
danlapid requested review from a team as code owners November 25, 2026 18:12

@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 df76d652.

This review, of 9acbaeab

✅ Approved — No problems found in the test, bench and docs changes. The one known caveat, the capnp-cpp sharding dependency, is already stated in the description.

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

No security concerns in these test and bench changes.


📖 AGENTS.md: ✅ LGTM

The src/tests/streams/digest/AGENTS.md update matches the new GC-pressure test layout.


📋 Code Quality: ✅ LGTM

Snapshot-case coverage, the termination-injection split, the bucket-test pattern() rewrite, the bench-json change and the --quick warmup change all check out. The sharded targets need the capnp-cpp update named in the description to get faster.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The docs/benchmarking.md and docs/startup-snapshot.md updates match the new behaviour.


Reviewed commit: 9acbaeab

@danlapid
danlapid force-pushed the dlapid/faster-slow-tests branch from 9acbaea to df76d65 Compare November 25, 2026 18:20

@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 6066ebca.

This review, of df76d652

✅ Approved — No issues found in this pull request's test, bench and docs changes, including the files changed since the last review.

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

These changes do not touch any production-facing security boundary.


📖 AGENTS.md: ✅ LGTM

The src/tests/streams/digest/AGENTS.md update matches the new GC-pressure test layout.


📋 Code Quality: ✅ LGTM

  • Quick-mode warmup (bench-api/lib.rs, bench-core/lib.rs): with QUICK_WARMUP_NS = 0, each case skips warmup and goes straight to calibration, or to sampling when a batch size is set. Calibration still runs each case at least once before sampling. startup-test.rs now checks against the QUICK_SAMPLES constant instead of a literal, so it follows the new value.
  • Termination-injection split: the loop for (index = group; index < scenarioCount; index += GROUPS) runs every scenario exactly once across the 7 groups. A TIJ_FILTER that matches a scenario in another group leaves this group with nothing to run, which is fine for a debugging aid.

⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The docs/benchmarking.md and docs/startup-snapshot.md updates match the new behaviour.


Reviewed commit: df76d652

@danlapid
danlapid force-pushed the dlapid/faster-slow-tests branch from df76d65 to 6066ebc Compare November 25, 2026 18:35

@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 5765b2d4.

This review, of 6066ebca

✅ Approved — No issues found in the test, bench, build-macro and docs changes, including those made since the last review.

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 changes do not touch any production-facing security boundary. In build/deps/gen/deps.MODULE.bazel, the capnp-cpp pin updates the sha256, the strip_prefix and the URL together.


📖 AGENTS.md: ✅ LGTM

The src/tests/streams/digest/AGENTS.md update matches the new GC-pressure test layout.


📋 Code Quality: ✅ LGTM

  • bench-core / bench-api:
    • encode_group now returns Vec<u8>, and its callers in runner.rs and lib-test.rs match.
    • words_to_bytes is used correctly by server/bench.rs.
    • Startup tests compare against QUICK_SAMPLES rather than a literal.
  • termination-injection-test: the stride loop runs every scenario exactly once across the 7 cases, and shard_count = 7 matches.
  • startup-snapshot configs: the three configs split the 29 cases between them. An unknown GROUP fails an assertion. The shared services capnp file is listed in each target's data.
  • bucket-test: every slice of the precomputed run stays in bounds. The expected digest comes from pattern(), so it is deterministic.
  • digest gc-pressure: gc-pressure.js is left out of the suite glob, so it runs only in its dedicated configs with the V8 flag set.
  • bench-json: each iteration is now a single encode or decode, and decode uses a fresh builder each time.

⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The docs/benchmarking.md and docs/startup-snapshot.md updates match the new behaviour.


Reviewed commit: 6066ebca

@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 fdda15c9.

This review, of 5765b2d4

✅ Approved — No issues found. The only change since the last review is an opt-in shard_count argument in build/kj_test.bzl, passed to both test variants, which leaves tests that don't set it as they were.

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 kj_test change only affects test sharding and does not touch any security boundary.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

build/kj_test.bzl now forwards shard_count = None to both _kj_test instances.

  • _kj_test is a test rule, so Bazel gives it the standard shard_count attribute.
  • When the argument is None, the attribute keeps its default, so tests that don't set it are unchanged.
  • test_attrs() does not also return shard_count, so it is never passed twice.

⚡ Performance: ✅ LGTM

Sharding is opt-in through shard_count, so tests that don't set it run as before.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 5765b2d4

@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 ffa9e66f.

This review, of fdda15c9

✅ Approved — No issues found. The new --quick changes in src/bench/streams/** scale the byte and chunk counts consistently, every expected total is computed from the scaled values, and case names stay unique.

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 changes affect tests, benchmarks and test or build configuration. None of them touches a security boundary that an untrusted Worker or caller can reach.


📖 AGENTS.md: ✅ LGTM

No issues found in the new or updated AGENTS.md files under src/bench/streams/.


📋 Code Quality: ✅ LGTM

No problems in the --quick scaling under src/bench/streams/**:

  • Scaled values. Dividing by 16 gives whole numbers everywhere. scaled() in body/helpers.js guards with Math.max(1, ...).
  • Case names. Case names still differ under --quick, so none collide.
  • Totals and URLs. Every expected total in the expectEqual checks, and every service-binding URL, is computed from the scaled count or size.
  • HTML rewriter. documentOf() builds the scaled document without ever producing an empty one.

⚡ Performance: ✅ LGTM

The bench-api/runner.rs change is a speedup:

  • When the case filter selects every case it is now None, so the byte-by-byte name matching is skipped.
  • Group encoding builds its bytes directly.

📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: fdda15c9

danlapid and others added 2 commits November 25, 2026 14:48
Several tests spent most of their time on work that bought no
coverage, and took tens of seconds under TSan or in debug builds.

* digest: gcDuringCloseOfDroppedStream made 30,000 streams and
  30 GB of 1 MiB buffers, 99.5% of each digest suite's time, and
  still never hit the use-after-free it was written for. It now runs
  alone in digest-gc-pressure-{cpp,ts}.wd-test under
  --stress-incremental-marking, which keeps marking in progress so
  that cppgc's allocation-limit check finishes a GC inside the close
  sink. Without the fix that crashes every ASan run and most debug
  runs, within about 10,000 streams.

* termination-injection-test: its scenarios are independent, so
  they are split between 7 test cases, which Bazel runs in 7 shards.

* js-readable-stream-test runs its cases in 4 shards.

* startup-snapshot-globals-test: the 29 cases run in three configs,
  transferable-v8, transferable-runtime and fallback, so that their
  Workers start in parallel. Each case still has its own service.

* bucket-test: pattern() computed 57 MiB a byte at a time in
  JavaScript. When V8 left that loop unoptimized, a debug run took
  45 to 60 seconds and sometimes timed out. Streams are now copied
  from one precomputed run, which also makes the parts of a
  multipart write differ from each other, so a completion that
  reordered them would fail the digest comparison.

* bench --quick (wd_bench @smoke) takes 2 samples instead of 5 and
  no warmup: it checks that each case runs, not how fast.

* bench-json timed each iteration's 10,000 or 100,000 inner
  repetitions, so a single benchmark iteration took seconds in debug
  builds. Each iteration is now one encode or decode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The @smoke tests run every case under --quick, which only checks that
the case runs. Cases that moved 1 MiB bodies or hundreds of chunks a
call took 0.3 to 1.4 seconds a call in TSan and debug builds, so a
single group took up to 12 seconds and a whole smoke test up to 35,
past the 15 second limit of a medium test in a debug build.

Under --quick, these cases now move a sixteenth as many bytes or
chunks, and their names show the amounts a run used:

* transform: throughput
* piping: pipeThrough and pipeTo
* body: read, pipe, clone, serviceBinding and htmlRewriter
* identity: throughput and pipe
* readable: from, asyncIteration and tee
* strategies: queueAccounting

src/bench/streams/AGENTS.md now asks new cases of this kind to do the
same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danlapid
danlapid force-pushed the dlapid/faster-slow-tests branch from fdda15c to ffa9e66 Compare November 25, 2026 19:52

@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 issues found. The new --quick settings (no warmup, 2 samples) work with the runner's handling of a zero warmup, and the startup tests now check QUICK_SAMPLES.

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 changes only lower the --quick limits for benchmarks. They cross no security boundary.


📖 AGENTS.md: ✅ LGTM

No issues found in the AGENTS.md files.


📋 Code Quality: ✅ LGTM

QUICK_WARMUP_NS = 0 works:

  • The runner handles a zero warmup by going straight to calibration, or to sampling when a case has a fixed batch.
  • The calibration call still runs each case once before it is sampled.
  • The overhead cases use the same constant, and a zero warmup works there too.
  • budget.warmup_ns = 0 gives the same result as the old min() would with a cap of 0.

QUICK_SAMPLES = 2 is used the same way across the bench runner and startup groups. The startup-test.rs assertions now check against it.


⚡ Performance: ✅ LGTM

Both changes make --quick runs cheaper. They add no cost to any hot path.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: ffa9e66f

@danlapid
danlapid merged commit b26b6c0 into main Nov 25, 2026
21 of 25 checks passed
@danlapid
danlapid deleted the dlapid/faster-slow-tests branch November 25, 2026 20:15
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