From 694541661b5a831440cd7a8a5ce782e2fcca2627 Mon Sep 17 00:00:00 2001 From: ismaeelakin Date: Tue, 3 Nov 2026 08:22:03 -0500 Subject: [PATCH 1/6] bench(rwlock): time upgradable acquisition and promotion --- benchmarks/Cargo.toml | 5 ++ benchmarks/README.md | 2 + benchmarks/benches/upgradable_stages.rs | 114 ++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 benchmarks/benches/upgradable_stages.rs diff --git a/benchmarks/Cargo.toml b/benchmarks/Cargo.toml index fffebeee..7e473142 100644 --- a/benchmarks/Cargo.toml +++ b/benchmarks/Cargo.toml @@ -71,5 +71,10 @@ harness = false name = "ecosystem" path = "benches/ecosystem/main.rs" +[[bench]] +harness = false +name = "upgradable_stages" +path = "benches/upgradable_stages.rs" + [lints] workspace = true diff --git a/benchmarks/README.md b/benchmarks/README.md index 21509de3..95b3930c 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -60,3 +60,5 @@ cargo x bench --bench primitives -- rwlock::upgradable --repeat 5 ``` The `primitives` target measures Asyncband-only operation cycles for focused before/after comparisons, including ready paths, queued handoffs, wake registration, cancellation, fan-out, and contended tables. Hand-polled cases use a reference-counted waker and exercise bookkeeping without executor scheduling. For `threads=N` cases the workers share a fixture, start at a barrier, and divide a fixed total number of rounds, including the remainder. Smoke mode runs one outer round per worker, preserving inner message batches, topology and correctness checks without making timing claims. + +For an uncontended upgradable-lock breakdown, run `cargo x bench --bench upgradable_stages`. It times acquisition and promotion separately on one reused lock. A clock read around every operation adds measurable overhead, so treat the numbers as diagnostic and compare runs made with the same toolchain and machine. Use `upgradable_read_then_upgrade` in the `primitives` target for the uninstrumented whole-cycle measurement. diff --git a/benchmarks/benches/upgradable_stages.rs b/benchmarks/benches/upgradable_stages.rs new file mode 100644 index 00000000..5bd67cb4 --- /dev/null +++ b/benchmarks/benches/upgradable_stages.rs @@ -0,0 +1,114 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::hint::black_box; +use std::time::Duration; +use std::time::Instant; + +use asyncband::rwlock::RwLockUpgradableReadGuard; +use asyncband::rwlock::UpgradableRwLock; +use benchmarks::support::bench_context; +use benchmarks::support::poll_ready; +use clap::Parser; + +#[derive(Parser)] +#[command(about = "Time upgradable-lock acquisition and promotion separately.")] +struct Options { + #[arg(long, default_value_t = 2_000_000)] + rounds: usize, + #[arg(long, default_value_t = 10)] + repeat: usize, + #[arg(long = "bench", hide = true)] + _bench: bool, +} + +struct Results { + acquisition: Vec, + promotion: Vec, +} + +fn measure(rounds: usize, repeat: usize) -> Results { + let lock = UpgradableRwLock::new(0usize); + let mut context = bench_context(); + let mut results = Results { + acquisition: Vec::with_capacity(repeat), + promotion: Vec::with_capacity(repeat), + }; + + for _ in 0..repeat { + let mut acquisition = Duration::ZERO; + let mut promotion = Duration::ZERO; + + for _ in 0..rounds { + let start = Instant::now(); + let guard = poll_ready(lock.upgradable_read(), &mut context); + acquisition += start.elapsed(); + + let start = Instant::now(); + let writer = poll_ready(RwLockUpgradableReadGuard::upgrade(guard), &mut context); + promotion += start.elapsed(); + + black_box(*writer); + drop(writer); + } + + results.acquisition.push(acquisition); + results.promotion.push(promotion); + } + + results +} + +fn report(name: &str, rounds: usize, mut samples: Vec) { + samples.sort_unstable(); + let middle = samples.len() / 2; + let median = if samples.len() % 2 == 0 { + let lower = samples[middle - 1]; + lower + (samples[middle] - lower) / 2 + } else { + samples[middle] + }; + let minimum = samples.first().unwrap(); + let maximum = samples.last().unwrap(); + let ns_per_round = median.as_nanos() as f64 / rounds as f64; + + println!( + "{name:<24} median {:>8.2} ns/round ({:.2}..{:.2} ms; runs: {:?})", + ns_per_round, + minimum.as_secs_f64() * 1_000.0, + maximum.as_secs_f64() * 1_000.0, + samples + .iter() + .map(|sample| sample.as_millis()) + .collect::>(), + ); +} + +fn main() { + let options = Options::parse(); + assert!(options.rounds > 0, "rounds must be positive"); + assert!(options.repeat > 0, "repeat must be positive"); + + let results = measure(options.rounds, options.repeat); + println!( + "UpgradableRwLock, {} rounds per run, {} runs", + options.rounds, options.repeat + ); + println!("Each operation is timed separately; the clock reads add measurable overhead."); + report("acquisition", options.rounds, results.acquisition); + report("promotion", options.rounds, results.promotion); +} From 715d994f045538218ecda710b30df7623278627f Mon Sep 17 00:00:00 2001 From: ismaeelakin Date: Tue, 3 Nov 2026 08:38:22 -0500 Subject: [PATCH 2/6] bench(rwlock): report upgradable lock stages --- benchmarks/README.md | 2 +- benchmarks/benches/upgradable_stages.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmarks/README.md b/benchmarks/README.md index 95b3930c..6cc2b707 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -61,4 +61,4 @@ cargo x bench --bench primitives -- rwlock::upgradable --repeat 5 The `primitives` target measures Asyncband-only operation cycles for focused before/after comparisons, including ready paths, queued handoffs, wake registration, cancellation, fan-out, and contended tables. Hand-polled cases use a reference-counted waker and exercise bookkeeping without executor scheduling. For `threads=N` cases the workers share a fixture, start at a barrier, and divide a fixed total number of rounds, including the remainder. Smoke mode runs one outer round per worker, preserving inner message batches, topology and correctness checks without making timing claims. -For an uncontended upgradable-lock breakdown, run `cargo x bench --bench upgradable_stages`. It times acquisition and promotion separately on one reused lock. A clock read around every operation adds measurable overhead, so treat the numbers as diagnostic and compare runs made with the same toolchain and machine. Use `upgradable_read_then_upgrade` in the `primitives` target for the uninstrumented whole-cycle measurement. +For an uncontended upgradable-lock breakdown, run `cargo x bench --bench upgradable_stages`. It times acquisition and promotion separately on one reused lock. The clock reads add measurable overhead, so treat the numbers as diagnostic and compare runs made with the same toolchain and machine. Use `upgradable_read_then_upgrade` in the `primitives` target for the uninstrumented whole-cycle measurement. diff --git a/benchmarks/benches/upgradable_stages.rs b/benchmarks/benches/upgradable_stages.rs index 5bd67cb4..f02e07a1 100644 --- a/benchmarks/benches/upgradable_stages.rs +++ b/benchmarks/benches/upgradable_stages.rs @@ -108,7 +108,7 @@ fn main() { "UpgradableRwLock, {} rounds per run, {} runs", options.rounds, options.repeat ); - println!("Each operation is timed separately; the clock reads add measurable overhead."); + println!("The clock reads add measurable overhead; treat these numbers as diagnostic."); report("acquisition", options.rounds, results.acquisition); report("promotion", options.rounds, results.promotion); } From b199028cd9b9f91ea2fee056b6367c08b32c6399 Mon Sep 17 00:00:00 2001 From: ismaeelakin Date: Wed, 4 Nov 2026 00:42:21 -0500 Subject: [PATCH 3/6] bench(rwlock): cover queued upgradable lock stages --- benchmarks/README.md | 2 +- benchmarks/benches/upgradable_stages.rs | 111 ++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/benchmarks/README.md b/benchmarks/README.md index 6cc2b707..faeb75a2 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -61,4 +61,4 @@ cargo x bench --bench primitives -- rwlock::upgradable --repeat 5 The `primitives` target measures Asyncband-only operation cycles for focused before/after comparisons, including ready paths, queued handoffs, wake registration, cancellation, fan-out, and contended tables. Hand-polled cases use a reference-counted waker and exercise bookkeeping without executor scheduling. For `threads=N` cases the workers share a fixture, start at a barrier, and divide a fixed total number of rounds, including the remainder. Smoke mode runs one outer round per worker, preserving inner message batches, topology and correctness checks without making timing claims. -For an uncontended upgradable-lock breakdown, run `cargo x bench --bench upgradable_stages`. It times acquisition and promotion separately on one reused lock. The clock reads add measurable overhead, so treat the numbers as diagnostic and compare runs made with the same toolchain and machine. Use `upgradable_read_then_upgrade` in the `primitives` target for the uninstrumented whole-cycle measurement. +For an upgradable-lock breakdown, run `cargo x bench --bench upgradable_stages`. It reports acquisition and promotion separately on one reused lock, then repeats them with ordinary readers held or a writer queued for the reservation. The reader cases queue promotion before releasing the readers; their times include that release and wake handoff. The writer case checks that an upgrade already holding the reservation completes ahead of the queued writer. A final case measures a later upgradable acquisition behind that writer, including the queued writer's handoff. The clock reads add measurable overhead, so treat all results as diagnostic and compare runs made with the same toolchain and machine. `cargo x bench --bench upgradable_stages -- --test` runs a single smoke pass without reporting measurements, as in CI. Use `upgradable_read_then_upgrade` in the `primitives` target for the uninstrumented uncontended whole-cycle measurement. diff --git a/benchmarks/benches/upgradable_stages.rs b/benchmarks/benches/upgradable_stages.rs index f02e07a1..f3a53f3e 100644 --- a/benchmarks/benches/upgradable_stages.rs +++ b/benchmarks/benches/upgradable_stages.rs @@ -16,12 +16,15 @@ // under the License. use std::hint::black_box; +use std::pin::pin; use std::time::Duration; use std::time::Instant; use asyncband::rwlock::RwLockUpgradableReadGuard; use asyncband::rwlock::UpgradableRwLock; use benchmarks::support::bench_context; +use benchmarks::support::poll_pending; +use benchmarks::support::poll_pinned_ready; use benchmarks::support::poll_ready; use clap::Parser; @@ -32,13 +35,21 @@ struct Options { rounds: usize, #[arg(long, default_value_t = 10)] repeat: usize, + #[arg(long)] + test: bool, #[arg(long = "bench", hide = true)] _bench: bool, } +const READER_COUNTS: &[usize] = &[1, 4, 16]; + struct Results { acquisition: Vec, promotion: Vec, + acquisition_with_reader: Vec, + promotion_with_readers: Vec>, + promotion_with_waiting_writer: Vec, + acquisition_after_waiting_writer: Vec, } fn measure(rounds: usize, repeat: usize) -> Results { @@ -47,11 +58,22 @@ fn measure(rounds: usize, repeat: usize) -> Results { let mut results = Results { acquisition: Vec::with_capacity(repeat), promotion: Vec::with_capacity(repeat), + acquisition_with_reader: Vec::with_capacity(repeat), + promotion_with_readers: READER_COUNTS + .iter() + .map(|_| Vec::with_capacity(repeat)) + .collect(), + promotion_with_waiting_writer: Vec::with_capacity(repeat), + acquisition_after_waiting_writer: Vec::with_capacity(repeat), }; for _ in 0..repeat { let mut acquisition = Duration::ZERO; let mut promotion = Duration::ZERO; + let mut acquisition_with_reader = Duration::ZERO; + let mut promotion_with_readers = vec![Duration::ZERO; READER_COUNTS.len()]; + let mut promotion_with_waiting_writer = Duration::ZERO; + let mut acquisition_after_waiting_writer = Duration::ZERO; for _ in 0..rounds { let start = Instant::now(); @@ -64,10 +86,72 @@ fn measure(rounds: usize, repeat: usize) -> Results { black_box(*writer); drop(writer); + + let reader = poll_ready(lock.read(), &mut context); + let start = Instant::now(); + let guard = poll_ready(lock.upgradable_read(), &mut context); + acquisition_with_reader += start.elapsed(); + black_box(*guard); + drop(guard); + drop(reader); + + for (index, &reader_count) in READER_COUNTS.iter().enumerate() { + let guard = poll_ready(lock.upgradable_read(), &mut context); + let readers = (0..reader_count) + .map(|_| poll_ready(lock.read(), &mut context)) + .collect::>(); + let mut upgrade = pin!(RwLockUpgradableReadGuard::upgrade(guard)); + let start = Instant::now(); + poll_pending(upgrade.as_mut(), &mut context); + drop(readers); + let writer = poll_pinned_ready(upgrade.as_mut(), &mut context); + promotion_with_readers[index] += start.elapsed(); + black_box(*writer); + drop(writer); + } + + let guard = poll_ready(lock.upgradable_read(), &mut context); + let mut queued_writer = pin!(lock.write()); + poll_pending(queued_writer.as_mut(), &mut context); + let start = Instant::now(); + let writer = poll_ready(RwLockUpgradableReadGuard::upgrade(guard), &mut context); + promotion_with_waiting_writer += start.elapsed(); + black_box(*writer); + drop(writer); + drop(poll_pinned_ready(queued_writer.as_mut(), &mut context)); + + let first_guard = poll_ready(lock.upgradable_read(), &mut context); + let mut queued_writer = pin!(lock.write()); + poll_pending(queued_writer.as_mut(), &mut context); + let mut queued_upgradable = pin!(lock.upgradable_read()); + poll_pending(queued_upgradable.as_mut(), &mut context); + let start = Instant::now(); + drop(first_guard); + drop(poll_pinned_ready(queued_writer.as_mut(), &mut context)); + let guard = poll_pinned_ready(queued_upgradable.as_mut(), &mut context); + acquisition_after_waiting_writer += start.elapsed(); + black_box(*guard); + drop(guard); } results.acquisition.push(acquisition); results.promotion.push(promotion); + results + .acquisition_with_reader + .push(acquisition_with_reader); + for (samples, elapsed) in results + .promotion_with_readers + .iter_mut() + .zip(promotion_with_readers) + { + samples.push(elapsed); + } + results + .promotion_with_waiting_writer + .push(promotion_with_waiting_writer); + results + .acquisition_after_waiting_writer + .push(acquisition_after_waiting_writer); } results @@ -100,6 +184,11 @@ fn report(name: &str, rounds: usize, mut samples: Vec) { fn main() { let options = Options::parse(); + if options.test { + let _ = measure(1, 1); + println!("ok upgradable-lock stage benchmark smoke checks"); + return; + } assert!(options.rounds > 0, "rounds must be positive"); assert!(options.repeat > 0, "repeat must be positive"); @@ -111,4 +200,26 @@ fn main() { println!("The clock reads add measurable overhead; treat these numbers as diagnostic."); report("acquisition", options.rounds, results.acquisition); report("promotion", options.rounds, results.promotion); + report( + "acquisition + reader held", + options.rounds, + results.acquisition_with_reader, + ); + for (&reader_count, samples) in READER_COUNTS.iter().zip(results.promotion_with_readers) { + report( + &format!("promotion + {reader_count} readers"), + options.rounds, + samples, + ); + } + report( + "promotion + writer queued", + options.rounds, + results.promotion_with_waiting_writer, + ); + report( + "acquire behind queued writer", + options.rounds, + results.acquisition_after_waiting_writer, + ); } From 0c52088e7298178eeeb1e235ce1802024b60baaf Mon Sep 17 00:00:00 2001 From: ismaeelakin Date: Wed, 4 Nov 2026 01:04:23 -0500 Subject: [PATCH 4/6] docs(benchmarks): clarify reader timing cases --- benchmarks/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/benchmarks/README.md b/benchmarks/README.md index faeb75a2..1dfef6ea 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -61,4 +61,6 @@ cargo x bench --bench primitives -- rwlock::upgradable --repeat 5 The `primitives` target measures Asyncband-only operation cycles for focused before/after comparisons, including ready paths, queued handoffs, wake registration, cancellation, fan-out, and contended tables. Hand-polled cases use a reference-counted waker and exercise bookkeeping without executor scheduling. For `threads=N` cases the workers share a fixture, start at a barrier, and divide a fixed total number of rounds, including the remainder. Smoke mode runs one outer round per worker, preserving inner message batches, topology and correctness checks without making timing claims. -For an upgradable-lock breakdown, run `cargo x bench --bench upgradable_stages`. It reports acquisition and promotion separately on one reused lock, then repeats them with ordinary readers held or a writer queued for the reservation. The reader cases queue promotion before releasing the readers; their times include that release and wake handoff. The writer case checks that an upgrade already holding the reservation completes ahead of the queued writer. A final case measures a later upgradable acquisition behind that writer, including the queued writer's handoff. The clock reads add measurable overhead, so treat all results as diagnostic and compare runs made with the same toolchain and machine. `cargo x bench --bench upgradable_stages -- --test` runs a single smoke pass without reporting measurements, as in CI. Use `upgradable_read_then_upgrade` in the `primitives` target for the uninstrumented uncontended whole-cycle measurement. +For an upgradable-lock breakdown, run `cargo x bench --bench upgradable_stages`. It reports uncontended acquisition and promotion separately on one reused lock, then measures acquisition while an ordinary reader is held. It also measures promotion with 1, 4, and 16 readers held, promotion while a writer waits behind the reservation, and a later upgradable acquisition queued behind a writer. + +For the promotion-with-readers cases, the benchmark queues the upgrade before dropping the readers; those times include releasing the guards and the resulting wake/handoff. The promotion-with-writer case checks that an upgrade holding the reservation completes before the queued writer. The later acquisition includes the writer's handoff. Clock reads add measurable overhead, so treat all results as diagnostic and compare runs made with the same toolchain and machine. `cargo x bench --bench upgradable_stages -- --test` runs a single smoke pass without reporting measurements, as in CI. Use `upgradable_read_then_upgrade` in the `primitives` target for the uninstrumented uncontended whole-cycle measurement. From d48ef627c2017d21542aa1979e564005396d0ab5 Mon Sep 17 00:00:00 2001 From: ismaeelakin Date: Wed, 4 Nov 2026 03:09:18 -0500 Subject: [PATCH 5/6] docs(benchmarks): clarify hand-polled timings --- benchmarks/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/README.md b/benchmarks/README.md index 1dfef6ea..7eb2211b 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -63,4 +63,4 @@ The `primitives` target measures Asyncband-only operation cycles for focused bef For an upgradable-lock breakdown, run `cargo x bench --bench upgradable_stages`. It reports uncontended acquisition and promotion separately on one reused lock, then measures acquisition while an ordinary reader is held. It also measures promotion with 1, 4, and 16 readers held, promotion while a writer waits behind the reservation, and a later upgradable acquisition queued behind a writer. -For the promotion-with-readers cases, the benchmark queues the upgrade before dropping the readers; those times include releasing the guards and the resulting wake/handoff. The promotion-with-writer case checks that an upgrade holding the reservation completes before the queued writer. The later acquisition includes the writer's handoff. Clock reads add measurable overhead, so treat all results as diagnostic and compare runs made with the same toolchain and machine. `cargo x bench --bench upgradable_stages -- --test` runs a single smoke pass without reporting measurements, as in CI. Use `upgradable_read_then_upgrade` in the `primitives` target for the uninstrumented uncontended whole-cycle measurement. +For the promotion-with-readers cases, the benchmark queues the upgrade before dropping the readers; those times include releasing the guards and the resulting wake/handoff. The promotion-with-writer case checks that an upgrade holding the reservation completes before the queued writer. The later acquisition includes the writer's handoff. This target manually polls futures with the benchmark's non-scheduling waker, so its timings exclude executor scheduling. Clock reads add measurable overhead, so treat all results as diagnostic and compare runs made with the same toolchain and machine. `cargo x bench --bench upgradable_stages -- --test` runs a single smoke pass without reporting measurements, as in CI. Use `upgradable_read_then_upgrade` in the `primitives` target for the uninstrumented uncontended whole-cycle measurement. From 69d7c060849ac48a660612c9bd851b4cffa34e5a Mon Sep 17 00:00:00 2001 From: ismaeelakin Date: Sun, 8 Nov 2026 07:05:52 -0500 Subject: [PATCH 6/6] bench(rwlock): address stage timing review --- .github/workflows/ci.yml | 2 + benchmarks/README.md | 4 +- benchmarks/benches/upgradable_stages.rs | 35 ++++++++++---- xtask/src/main.rs | 62 ++++++++++++++++--------- 4 files changed, 71 insertions(+), 32 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2f8a9c37..38e16018 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -65,6 +65,8 @@ jobs: run: cargo x check - name: Smoke benchmarks run: cargo x bench -- --test + - name: Smoke upgradable-lock stage benchmark + run: cargo x bench --bench upgradable_stages -- --test test: name: Run tests diff --git a/benchmarks/README.md b/benchmarks/README.md index 7eb2211b..b8a9e467 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -28,6 +28,8 @@ cargo x bench --no-run cargo x bench -- --test ``` +The `upgradable_stages` target is selected explicitly so it does not interfere with the default benchmark listing. CI smoke-tests it in a separate command. + ## Compare implementations ```shell @@ -63,4 +65,4 @@ The `primitives` target measures Asyncband-only operation cycles for focused bef For an upgradable-lock breakdown, run `cargo x bench --bench upgradable_stages`. It reports uncontended acquisition and promotion separately on one reused lock, then measures acquisition while an ordinary reader is held. It also measures promotion with 1, 4, and 16 readers held, promotion while a writer waits behind the reservation, and a later upgradable acquisition queued behind a writer. -For the promotion-with-readers cases, the benchmark queues the upgrade before dropping the readers; those times include releasing the guards and the resulting wake/handoff. The promotion-with-writer case checks that an upgrade holding the reservation completes before the queued writer. The later acquisition includes the writer's handoff. This target manually polls futures with the benchmark's non-scheduling waker, so its timings exclude executor scheduling. Clock reads add measurable overhead, so treat all results as diagnostic and compare runs made with the same toolchain and machine. `cargo x bench --bench upgradable_stages -- --test` runs a single smoke pass without reporting measurements, as in CI. Use `upgradable_read_then_upgrade` in the `primitives` target for the uninstrumented uncontended whole-cycle measurement. +For the promotion-with-readers cases, the benchmark queues the upgrade before dropping the readers; those times include releasing the guards and the resulting wake/handoff. The reader vector is allocated before the timed loop and reused; clearing it drops the guards without freeing its backing allocation in the timed interval. The promotion-with-writer case checks that an upgrade holding the reservation completes before the queued writer. The later acquisition includes the writer's handoff. This target manually polls futures with the benchmark's non-scheduling waker, so its timings exclude executor scheduling. Each run reports an empty timing interval using the same clock reads; measured values remain unadjusted and should be treated as diagnostic. Compare runs made with the same toolchain and machine. `cargo x bench --bench upgradable_stages -- --test` runs a single smoke pass without reporting measurements, as in CI. Use `upgradable_read_then_upgrade` in the `primitives` target for the uninstrumented uncontended whole-cycle measurement. diff --git a/benchmarks/benches/upgradable_stages.rs b/benchmarks/benches/upgradable_stages.rs index f3a53f3e..b80f59b5 100644 --- a/benchmarks/benches/upgradable_stages.rs +++ b/benchmarks/benches/upgradable_stages.rs @@ -44,6 +44,7 @@ struct Options { const READER_COUNTS: &[usize] = &[1, 4, 16]; struct Results { + empty_interval: Vec, acquisition: Vec, promotion: Vec, acquisition_with_reader: Vec, @@ -56,6 +57,7 @@ fn measure(rounds: usize, repeat: usize) -> Results { let lock = UpgradableRwLock::new(0usize); let mut context = bench_context(); let mut results = Results { + empty_interval: Vec::with_capacity(repeat), acquisition: Vec::with_capacity(repeat), promotion: Vec::with_capacity(repeat), acquisition_with_reader: Vec::with_capacity(repeat), @@ -66,8 +68,10 @@ fn measure(rounds: usize, repeat: usize) -> Results { promotion_with_waiting_writer: Vec::with_capacity(repeat), acquisition_after_waiting_writer: Vec::with_capacity(repeat), }; + let mut readers = Vec::with_capacity(*READER_COUNTS.last().unwrap()); for _ in 0..repeat { + let mut empty_interval = Duration::ZERO; let mut acquisition = Duration::ZERO; let mut promotion = Duration::ZERO; let mut acquisition_with_reader = Duration::ZERO; @@ -76,6 +80,9 @@ fn measure(rounds: usize, repeat: usize) -> Results { let mut acquisition_after_waiting_writer = Duration::ZERO; for _ in 0..rounds { + let start = Instant::now(); + empty_interval += start.elapsed(); + let start = Instant::now(); let guard = poll_ready(lock.upgradable_read(), &mut context); acquisition += start.elapsed(); @@ -97,13 +104,13 @@ fn measure(rounds: usize, repeat: usize) -> Results { for (index, &reader_count) in READER_COUNTS.iter().enumerate() { let guard = poll_ready(lock.upgradable_read(), &mut context); - let readers = (0..reader_count) - .map(|_| poll_ready(lock.read(), &mut context)) - .collect::>(); + for _ in 0..reader_count { + readers.push(poll_ready(lock.read(), &mut context)); + } let mut upgrade = pin!(RwLockUpgradableReadGuard::upgrade(guard)); let start = Instant::now(); poll_pending(upgrade.as_mut(), &mut context); - drop(readers); + readers.clear(); let writer = poll_pinned_ready(upgrade.as_mut(), &mut context); promotion_with_readers[index] += start.elapsed(); black_box(*writer); @@ -134,6 +141,7 @@ fn measure(rounds: usize, repeat: usize) -> Results { drop(guard); } + results.empty_interval.push(empty_interval); results.acquisition.push(acquisition); results.promotion.push(promotion); results @@ -169,16 +177,17 @@ fn report(name: &str, rounds: usize, mut samples: Vec) { let minimum = samples.first().unwrap(); let maximum = samples.last().unwrap(); let ns_per_round = median.as_nanos() as f64 / rounds as f64; + let runs = samples + .iter() + .map(|sample| format!("{:.3}", sample.as_secs_f64() * 1_000.0)) + .collect::>() + .join(", "); println!( - "{name:<24} median {:>8.2} ns/round ({:.2}..{:.2} ms; runs: {:?})", + "{name:<24} median {:>8.2} ns/round ({:.2}..{:.2} ms; runs (ms): [{runs}])", ns_per_round, minimum.as_secs_f64() * 1_000.0, maximum.as_secs_f64() * 1_000.0, - samples - .iter() - .map(|sample| sample.as_millis()) - .collect::>(), ); } @@ -198,6 +207,14 @@ fn main() { options.rounds, options.repeat ); println!("The clock reads add measurable overhead; treat these numbers as diagnostic."); + println!( + "The empty interval uses the same clock reads and is reported without subtracting it." + ); + report( + "empty timing interval", + options.rounds, + results.empty_interval, + ); report("acquisition", options.rounds, results.acquisition); report("promotion", options.rounds, results.promotion); report( diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 565d7793..e40c0e45 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -74,7 +74,7 @@ struct CommandBench { #[arg( long, - help = "Select a target (ecosystem or primitives; default: all)." + help = "Select a target (ecosystem, primitives, or upgradable_stages; default: ecosystem and primitives)." )] bench: Option, @@ -88,11 +88,9 @@ struct CommandBench { impl CommandBench { fn run(self) { - run_command(make_bench_cmd( - self.no_run, - self.bench.as_deref(), - &self.args, - )); + for cmd in make_bench_cmds(self.no_run, self.bench.as_deref(), &self.args) { + run_command(cmd); + } } } @@ -394,22 +392,23 @@ fn classify_release_type(baseline: &Version, release: &Version) -> SemverRelease } } -fn make_bench_cmd(no_run: bool, bench: Option<&str>, args: &[String]) -> StdCommand { - let mut cmd = find_command("cargo"); - cmd.args([ - "bench", - "--workspace", - "--all-features", - "--bench", - bench.unwrap_or("*"), - ]); - if no_run { - cmd.arg("--no-run"); - } - if !args.is_empty() { - cmd.arg("--").args(args); - } - cmd +fn make_bench_cmds(no_run: bool, bench: Option<&str>, args: &[String]) -> Vec { + let benches = bench.map_or_else(|| vec!["ecosystem", "primitives"], |bench| vec![bench]); + + benches + .into_iter() + .map(|bench| { + let mut cmd = find_command("cargo"); + cmd.args(["bench", "--workspace", "--all-features", "--bench", bench]); + if no_run { + cmd.arg("--no-run"); + } + if !args.is_empty() { + cmd.arg("--").args(args); + } + cmd + }) + .collect() } fn make_build_cmd(locked: bool) -> StdCommand { @@ -598,4 +597,23 @@ mod tests { SemverReleaseType::Patch ); } + + #[test] + fn default_benchmarks_leave_the_stage_target_opt_in() { + let commands = make_bench_cmds(false, None, &[]); + let selected = commands + .iter() + .map(|cmd| cmd.get_args().last().unwrap().to_str().unwrap()) + .collect::>(); + + assert_eq!(selected, ["ecosystem", "primitives"]); + + let stage_command = make_bench_cmds(false, Some("upgradable_stages"), &[]); + assert_eq!(stage_command.len(), 1); + assert!( + stage_command[0] + .get_args() + .any(|arg| arg == "upgradable_stages") + ); + } }