Repository navigation
Move workerd bench's report building and startup timing to Rust - #8677
Conversation
guybedford
left a comment
There was a problem hiding this comment.
The port reads as faithful to the C++, and the GlobFilter port matches kj::GlobFilter line for line. Two things on the new fillBenchReport() shim, inline.
Move the code that turns a group's samples into a BenchReport.Group out of api/bench.c++ into a new crate, bench-core (src/workerd/api/bench-core/): subtracting the runner's overhead, the HDR statistics, the atFloor and highVariance flags, and failing cases with calls longer than an hour. This is the first step of porting `workerd bench` to Rust. The crate has no JSG dependency, because api/bench.c++ is built into the C++ io library, which the controller's crate will depend on through io-context. fillBenchReport() becomes a shim: it hands the results to Rust through a cxx bridge and copies the encoded group into the caller's builder. Case names and errors cross as bytes and are written to the report's Text fields unchanged, since C++ strings needn't be valid UTF-8. A case's error is set by a separate call, so an empty error stays set, as with the kj::Maybe<kj::String> before. The shim reads the encoded group with no traversal limit: a group holds up to MAX_SAMPLES wall and CPU samples per case, which can be more than capnp's default limit of 8M words, as when the report was written straight to the builder before. The fillBenchReport tests move from bench-test.c++ to bench-core/lib-test.rs with the same inputs and expected values; ffi-test.rs pins the bridge's behavior. bench-test.c++ keeps two tests of the shim itself: a round trip of a group through it, and a group larger than the default traversal limit. The C++ median() stays, with a TODO, for the overhead measurement. No behavior change.
b4f306d to
b2665ed
Compare
There was a problem hiding this comment.
Superseded by the review of
fb391e79.
This review, of b2665eda
💬 Approved with comments — The port matches the C++ it replaces, and the traversal-limit fix and the new shim tests are in place. Two INFO notes remain: peak memory on very large groups, and a median doc comment that doesn't match its code.
Findings: 2 info (2 are inline comments 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: 💬 2 info
The port matches the C++ and I found no bugs. Two INFO notes: memory for large groups and a median doc comment.
Recommendation: 💬 Optional Nits
- [INFO] Encoding copies the full message a second time (bytes into a
Vec<u64>), which adds to the peak memory of large groups. (src/workerd/api/bench-core/lib.rs:149) - [INFO]
median's doc comment says the input must not be empty, but the function returns NaN for empty input. (src/workerd/api/bench-core/lib.rs:105)
View full review
The new bench-core crate matches the C++ it replaces. The traversal-limit issue raised earlier in the discussion is fixed: fillBenchReport() now reads the encoded group with traversalLimitInWords = kj::maxValue. The shim also has round-trip and large-group tests in bench-test.c++.
-
INFO: The report path now makes several full copies of each group's samples:
add_casecopies them withto_vec.subtractcreates an intermediate vector.- The encoded message is written as bytes, then collected again into
Vec<u64>. setCasesdeep-copies the result into the C++ builder.
This happens once per group and only in
workerd bench, but withMAX_SAMPLEScases it can reach several hundred MB at peak. -
INFO: The doc comment on
mediansays the input must not be empty, but the function returns NaN for empty input. The C++ version asserted instead.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security concerns. Only workerd bench dispatches the bench event, and the new FFI surface takes byte slices and plain values.
📖 AGENTS.md: ✅ LGTM
No issues. The JS benchmarks row in the root AGENTS.md could mention src/workerd/api/bench-core/ in a later pull request.
⚡ Performance: ✅ LGTM
Report building runs once per group, at the end, and only in workerd bench. Its transient peak memory is covered in the code quality section.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: b2665eda
Move the code that turns a group's samples into a BenchReport.Group out of api/bench.c++ into a new crate, bench-core (src/workerd/api/bench-core/): subtracting the runner's overhead, the HDR statistics, the atFloor and highVariance flags, and failing cases with calls longer than an hour. This is the first step of porting `workerd bench` to Rust. The crate has no JSG dependency, because api/bench.c++ is built into the C++ io library, which the controller's crate will depend on through io-context. fillBenchReport() becomes a shim: it hands the results to Rust through a cxx bridge and copies the encoded group into the caller's builder. Case names and errors cross as bytes and are written to the report's Text fields unchanged, since C++ strings needn't be valid UTF-8. A case's error is set by a separate call, so an empty error stays set, as with the kj::Maybe<kj::String> before. The shim reads the encoded group with no traversal limit: a group holds up to MAX_SAMPLES wall and CPU samples per case, which can be more than capnp's default limit of 8M words, as when the report was written straight to the builder before. The fillBenchReport tests move from bench-test.c++ to bench-core/lib-test.rs with the same inputs and expected values; ffi-test.rs pins the bridge's behavior. bench-test.c++ keeps two tests of the shim itself: a round trip of a group through it, and a group larger than the default traversal limit. The C++ median() stays, with a TODO, for the overhead measurement. No behavior change.
Move the loop that times a Worker's startup for a startup group (BenchReport.Group.startup) from api/bench.c++ into bench-core's new startup module: the warmup, the sample count and sampling budget, the --quick caps, the case filter on the phases, and failing the group when a start fails. workerd-server calls it from WorkerService::bench_startup(). The factory now only starts one throwaway copy of the Worker and returns how long its phases took: factory_bench_startup() becomes factory_start_throwaway_worker(), which returns a WorkerStartupTiming shared struct instead of an encoded group. The clocks are still read in the factory, between the phases. A start that fails gives its kj::Exception description as the group's error, as before. The case filter is kj::GlobFilter itself, which bench-core reaches through a small cxx bridge (bench-core's GlobFilter), so case names match exactly as in C++. BenchController's own case filter will use it once it moves to Rust. The startup tests move from bench-test.c++ to bench-core/startup-test.rs with the same inputs and expected values; bench-test.c++ keeps the tests of the fillBenchReport() shim. The defaults the loop shares with BenchController stay duplicated in api/bench.c++, with a TODO, until BenchController moves. No behavior change.
b2665ed to
fb391e7
Compare
There was a problem hiding this comment.
Superseded by the review of
2474e223.
This review, of fb391e79
💬 Approved with comments — No correctness, security or performance issues. The earlier notes are fixed, and one INFO note remains about the crate doc comment.
Findings: 1 info (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.
📝 Documentation: 💬 1 info
The crate doc comment overstates where names and errors come from
Recommendation: 💬 Optional Nits
- [INFO] Crate doc says report names and errors all come from C++ strings, but the startup module makes its phase names in Rust and takes errors as
String. (src/workerd/api/bench-core/lib.rs:15)
View full review
lib.rs:15-16says names and errors come from C++ strings. On the startup path, though,startup::time_startupcreates the phase names in Rust and takes start errors asString.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security concerns. Only workerd bench dispatches the bench event.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
Both earlier notes are fixed. to_words (lib.rs:154-161) sizes its buffer with compute_serialized_size_in_words and writes the message straight into a Vec<u64>, so the second copy as bytes is gone. The median doc comment now matches its code, and the empty case has a test.
In startup.rs, the loop handles the warmup, STARTUP_MIN_SAMPLES and the sampling budget. A failed start is written as the group's error. An empty filter becomes *, and a filter that selects no phase returns before starting anything.
⚡ Performance: ✅ LGTM
Writing the encoded message straight into a pre-sized word buffer removes one full copy, which lowers peak memory for large groups. In startup.rs, timings is allocated up front.
Reviewed commit: fb391e79
The crate doc said all names and errors come from C++ strings, but the startup module makes its own phase names and gets a failed start's error as a String.
There was a problem hiding this comment.
✅ Approved — No issues found: the crate doc note from the last review is fixed, and the traversal-limit fix and its shim tests are in place.
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. Only workerd bench dispatches the bench event, so no deployed Worker can reach this code. The report the shim decodes comes from the process's own encoder.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
to_words sizes its buffer with compute_serialized_size_in_words and writes the message straight into a Vec<u64>. The C++ shim reads that buffer as aligned words. The median doc comment matches its code, and stddev is scaled the same way as in the removed C++ code.
⚡ Performance: ✅ LGTM
The shim's copies are linear and run once per group, after sampling ends. Writing the encoded message straight into a pre-sized word buffer avoids an extra copy.
📝 Documentation: ✅ LGTM
The crate doc (lib.rs:15-18) now correctly separates bytes that come through the C++ bridge from the phase names and errors the startup module handles as Rust strings.
Reviewed commit: 2474e223
This is the first step of porting
workerd benchfrom C++ to Rust. Onlyworkerd benchdispatches the bench event, so nothing a deployed Workercan reach changes, which makes it a low-risk subcommand to work out the
approach on.
A new crate,
bench-core, now builds a group's report (overheadsubtraction, statistics and flags) and runs the loop that times a
Worker's startup. The C++ side keeps a thin shim that hands results to
Rust and copies the encoded report back, and the case filter is still
kj::GlobFilter.bench-corehas no JSG dependency, so both the workerside and
workerd-servercan use it;BenchControllerfollows in itsown crate.
No behavior change is intended, apart from a failed Worker start's error
description, which now shows a placeholder for invalid UTF-8 instead of
raw bytes that
workerd benchcouldn't print. The existing tests moveto Rust with the same inputs and expected values, and the bench tests
and
@smoketargets pass unchanged.