Repository navigation
Move workerd bench's report building and startup timing to Rust #8677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
316ba34
Build workerd bench's group reports in Rust
bartlomieju cfb17c1
Build workerd bench's group reports in Rust
bartlomieju fb391e7
Time workerd bench's startup groups in Rust
bartlomieju 2474e22
Say which bench-core names and errors come from C++ as bytes
bartlomieju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| load("//:build/wd_rust_crate.bzl", "wd_rust_crate") | ||
|
|
||
| # The parts of `workerd bench` that need no JSG: statistics over a group's samples, written as a | ||
| # `BenchReport.Group` (bench.capnp), and the timing of startup groups. With no JSG dependencies, | ||
| # both the worker side (api/bench.c++) and the server (workerd-server) can use it. | ||
| wd_rust_crate( | ||
| name = "bench-core", | ||
| cxx_bridge_deps = ["@capnp-cpp//src/kj"], | ||
| cxx_bridge_hdrs = ["glob-filter.h"], | ||
| cxx_bridge_src = "ffi.rs", | ||
| cxx_bridge_srcs = ["glob_filter.rs"], | ||
| cxx_bridge_visibility = ["//visibility:public"], | ||
| test_deps = ["@crates_vendor//futures"], | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//src/rust/histogram", | ||
| "//src/workerd/io:bench_capnp_rust", | ||
| "@crates_vendor//capnp", | ||
| "@crates_vendor//thiserror", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Copyright (c) 2026 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| use bench_capnp::bench_report; | ||
|
|
||
| use super::*; | ||
|
|
||
| /// The group that `report` encodes, read back as C++ reads it. | ||
| fn decode(report: &BenchGroupReport) -> capnp::message::Reader<capnp::serialize::OwnedSegments> { | ||
| let words = report.encode().unwrap(); | ||
| let bytes: Vec<u8> = words.iter().flat_map(|word| word.to_ne_bytes()).collect(); | ||
| capnp::serialize::read_message(bytes.as_slice(), capnp::message::ReaderOptions::new()).unwrap() | ||
| } | ||
|
|
||
| fn cases( | ||
| message: &capnp::message::Reader<capnp::serialize::OwnedSegments>, | ||
| ) -> capnp::struct_list::Reader<'_, bench_report::case::Owned> { | ||
| message | ||
| .get_root::<bench_report::group::Reader<'_>>() | ||
| .unwrap() | ||
| .get_cases() | ||
| .unwrap() | ||
| } | ||
|
|
||
| /// Adds a failed case named `name`, without samples. | ||
| fn add_failed(report: &mut BenchGroupReport, name: &[u8]) { | ||
| report | ||
| .add_case(name, Status::Failed as u16, 0, &[], &[], false) | ||
| .unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_empty_case_error_is_set() { | ||
| let mut report = new_bench_group_report(); | ||
| add_failed(&mut report, b"case"); | ||
| report.set_case_error(b""); | ||
|
|
||
| let message = decode(&report); | ||
| let case = cases(&message).get(0); | ||
| assert!(case.has_error()); | ||
| assert_eq!(case.get_error().unwrap().as_bytes(), b""); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_case_has_no_error_unless_one_is_set() { | ||
| let mut report = new_bench_group_report(); | ||
| add_failed(&mut report, b"case"); | ||
|
|
||
| let message = decode(&report); | ||
| assert!(!cases(&message).get(0).has_error()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_case_error_goes_to_the_case_added_last() { | ||
| let mut report = new_bench_group_report(); | ||
| add_failed(&mut report, b"first"); | ||
| add_failed(&mut report, b"second"); | ||
| report.set_case_error(b"boom"); | ||
|
|
||
| let message = decode(&report); | ||
| let cases = cases(&message); | ||
| assert!(!cases.get(0).has_error()); | ||
| assert_eq!(cases.get(1).get_error().unwrap().as_bytes(), b"boom"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_case_error_before_any_case_is_ignored() { | ||
| let mut report = new_bench_group_report(); | ||
| report.set_case_error(b"boom"); | ||
| add_failed(&mut report, b"case"); | ||
|
|
||
| let message = decode(&report); | ||
| assert!(!cases(&message).get(0).has_error()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn strings_that_arent_utf8_are_written_unchanged() { | ||
| // A lone surrogate as WTF-8, and a truncated sequence. | ||
| let name = b"case \xed\xa0\x80"; | ||
| let error = b"error \xe2\x82"; | ||
| let mut report = new_bench_group_report(); | ||
| report.set_error(error); | ||
| add_failed(&mut report, name); | ||
| report.set_case_error(error); | ||
|
|
||
| let message = decode(&report); | ||
| let group = message | ||
| .get_root::<bench_report::group::Reader<'_>>() | ||
| .unwrap(); | ||
| assert_eq!(group.get_error().unwrap().as_bytes(), error); | ||
| let case = group.get_cases().unwrap().get(0); | ||
| assert_eq!(case.get_name().unwrap().as_bytes(), name); | ||
| assert_eq!(case.get_error().unwrap().as_bytes(), error); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_unknown_status_is_an_error() { | ||
| let mut report = new_bench_group_report(); | ||
| assert!(report.add_case(b"case", 3, 0, &[], &[], false).is_err()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright (c) 2026 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| //! The bridge through which C++ hands a group's results to Rust to encode as a | ||
| //! `BenchReport.Group`. | ||
|
|
||
| use bench_capnp::bench_report::case::Status; | ||
|
|
||
| use crate::CaseResult; | ||
| use crate::GroupResult; | ||
| use crate::Overhead; | ||
|
|
||
| #[cxx::bridge(namespace = "workerd::api")] | ||
| #[expect(unsafe_code, reason = "the cxx bridge expands to unsafe FFI glue")] | ||
| #[expect(clippy::unnecessary_box_returns, reason = "cxx requires a Box")] | ||
| mod bridge { | ||
| extern "Rust" { | ||
| /// A group's results, collected from C++. | ||
| type BenchGroupReport; | ||
|
|
||
| fn new_bench_group_report() -> Box<BenchGroupReport>; | ||
|
|
||
| /// Sets the group's error: `bench()` itself failed. Strings are bytes, which needn't be | ||
| /// UTF-8, and are written to the report unchanged. | ||
| fn set_error(self: &mut BenchGroupReport, error: &[u8]); | ||
|
|
||
| /// Sets the overhead that was subtracted; see `BenchReport.Overhead`. | ||
| fn set_overhead( | ||
| self: &mut BenchGroupReport, | ||
| sync_ns: f64, | ||
| sync_cpu_ns: f64, | ||
| async_ns: f64, | ||
| async_cpu_ns: f64, | ||
| black_box_ns: f64, | ||
| ); | ||
|
|
||
| /// Adds a case. `status` is a `BenchReport.Case.Status`; `returns_promise` selects the | ||
| /// overhead that is subtracted. | ||
| fn add_case( | ||
| self: &mut BenchGroupReport, | ||
| name: &[u8], | ||
| status: u16, | ||
| iterations_per_sample: u64, | ||
| wall_ns: &[f64], | ||
| cpu_ns: &[f64], | ||
| returns_promise: bool, | ||
| ) -> Result<()>; | ||
|
|
||
| /// Sets the error of the case added last, even if `error` is empty: a case has an error | ||
| /// exactly when this was called for it, as a C++ `kj::Maybe<kj::String>` that is set. | ||
| fn set_case_error(self: &mut BenchGroupReport, error: &[u8]); | ||
|
|
||
| /// The group, as an encoded `BenchReport.Group` message (segment table, then segments) | ||
| /// in words. | ||
| fn encode(self: &BenchGroupReport) -> Result<Vec<u64>>; | ||
| } | ||
| } | ||
|
|
||
| /// A group's results, collected from C++. | ||
| pub struct BenchGroupReport(GroupResult); | ||
|
|
||
| #[expect(clippy::unnecessary_box_returns, reason = "cxx requires a Box")] | ||
| fn new_bench_group_report() -> Box<BenchGroupReport> { | ||
| Box::new(BenchGroupReport(GroupResult::default())) | ||
| } | ||
|
|
||
| impl BenchGroupReport { | ||
| fn set_error(&mut self, error: &[u8]) { | ||
| self.0.error = Some(error.to_vec()); | ||
| } | ||
|
|
||
| fn set_overhead( | ||
| &mut self, | ||
| sync_ns: f64, | ||
| sync_cpu_ns: f64, | ||
| async_ns: f64, | ||
| async_cpu_ns: f64, | ||
| black_box_ns: f64, | ||
| ) { | ||
| self.0.overhead = Some(Overhead { | ||
| sync_ns, | ||
| sync_cpu_ns, | ||
| async_ns, | ||
| async_cpu_ns, | ||
| black_box_ns, | ||
| }); | ||
| } | ||
|
|
||
| fn add_case( | ||
| &mut self, | ||
| name: &[u8], | ||
| status: u16, | ||
| iterations_per_sample: u64, | ||
| wall_ns: &[f64], | ||
| cpu_ns: &[f64], | ||
| returns_promise: bool, | ||
| ) -> Result<(), capnp::NotInSchema> { | ||
| self.0.cases.push(CaseResult { | ||
| name: name.to_vec(), | ||
| status: Status::try_from(status)?, | ||
| error: None, | ||
| iterations_per_sample, | ||
| wall_ns: wall_ns.to_vec(), | ||
| cpu_ns: cpu_ns.to_vec(), | ||
| returns_promise, | ||
| }); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn set_case_error(&mut self, error: &[u8]) { | ||
| if let Some(case) = self.0.cases.last_mut() { | ||
| case.error = Some(error.to_vec()); | ||
| } | ||
| } | ||
|
|
||
| fn encode(&self) -> Result<Vec<u64>, crate::Error> { | ||
| crate::encode_group(&self.0) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "ffi-test.rs"] | ||
| mod tests; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (c) 2026 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| // The C++ side of the bridge in glob_filter.rs. | ||
|
|
||
| #include <rust/cxx.h> | ||
|
|
||
| #include <kj/glob-filter.h> | ||
| #include <kj/string.h> | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| namespace workerd::api { | ||
|
|
||
| // Whether `name` matches `pattern`, both bytes, as `kj::GlobFilter` matches them. | ||
| inline bool glob_filter_matches( | ||
| ::rust::Slice<const uint8_t> pattern, ::rust::Slice<const uint8_t> name) { | ||
| kj::GlobFilter filter( | ||
| kj::arrayPtr(reinterpret_cast<const char*>(pattern.data()), pattern.size())); | ||
| // matches() takes a NUL-terminated kj::StringPtr, so the name is copied. | ||
| return filter.matches( | ||
| kj::str(kj::arrayPtr(reinterpret_cast<const char*>(name.data()), name.size()))); | ||
| } | ||
|
|
||
| } // namespace workerd::api |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright (c) 2026 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| use super::*; | ||
|
|
||
| fn matches(pattern: &str, name: &str) -> bool { | ||
| GlobFilter::new(pattern.as_bytes()).matches(name.as_bytes()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn matches_literals_exactly() { | ||
| assert!(matches("encode", "encode")); | ||
| assert!(!matches("encode", "encoder")); | ||
| assert!(!matches("encode", "encod")); | ||
| assert!(matches("", "")); | ||
| assert!(!matches("", "a")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn star_matches_any_run_but_not_a_separator() { | ||
| assert!(matches("*", "")); | ||
| assert!(matches("*", "anything")); | ||
| assert!(matches("enc*", "encode")); | ||
| assert!(matches("*code", "encode")); | ||
| assert!(matches("e*o*e", "encode")); | ||
| assert!(matches("**", "encode")); | ||
| assert!(!matches("a*c", "a/c")); | ||
| assert!(!matches("a*c", "a\\c")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn question_mark_matches_one_byte_but_not_a_separator() { | ||
| assert!(matches("enc?de", "encode")); | ||
| assert!(!matches("enc?de", "encde")); | ||
| assert!(!matches("a?c", "a/c")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn matching_starts_again_after_a_separator() { | ||
| assert!(matches("b", "a/b")); | ||
| assert!(matches("b", "a\\b")); | ||
| assert!(matches("c*", "a/b/cd")); | ||
| assert!(matches("b/c", "a/b/c")); | ||
| assert!(!matches("a", "a/b")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn startup_phases_arent_selected_by_patterns_for_cases() { | ||
| // A ':' isn't a separator, so a pattern has to name the "startup:" prefix. | ||
| assert!(matches("startup:*", "startup:total")); | ||
| assert!(matches("startup:*r*", "startup:script")); | ||
| assert!(!matches("startup:*r*", "startup:total")); | ||
| assert!(!matches("total", "startup:total")); | ||
| assert!(!matches("w*", "startup:worker")); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright (c) 2026 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| //! The case filter of `workerd bench`: `kj::GlobFilter` (kj/glob-filter.h), so that case names | ||
| //! match in Rust exactly as they do in C++. | ||
|
|
||
| #[cxx::bridge(namespace = "workerd::api")] | ||
| #[expect(unsafe_code, reason = "the cxx bridge expands to unsafe FFI glue")] | ||
| mod bridge { | ||
| unsafe extern "C++" { | ||
| include!("workerd/api/bench-core/glob-filter.h"); | ||
|
|
||
| /// Whether `name` matches `pattern`, as `kj::GlobFilter(pattern).matches(name)`. | ||
| fn glob_filter_matches(pattern: &[u8], name: &[u8]) -> bool; | ||
| } | ||
| } | ||
|
|
||
| /// A glob that matches a name, or the part of it after any `/` or `\`. | ||
| /// | ||
| /// `*` matches any run of characters and `?` any one character, but neither matches `/` or `\`. | ||
| /// Names and patterns are bytes, compared one byte at a time, as `kj::GlobFilter` compares | ||
| /// `char`s. | ||
| #[derive(Debug, Clone)] | ||
| pub struct GlobFilter { | ||
| pattern: Vec<u8>, | ||
| } | ||
|
|
||
| impl GlobFilter { | ||
| #[must_use] | ||
| pub fn new(pattern: &[u8]) -> Self { | ||
| Self { | ||
| pattern: pattern.to_vec(), | ||
| } | ||
| } | ||
|
|
||
| /// Whether `name` matches. | ||
| #[must_use] | ||
| pub fn matches(&self, name: &[u8]) -> bool { | ||
| bridge::glob_filter_matches(&self.pattern, name) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "glob_filter-test.rs"] | ||
| mod tests; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.