Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/codspeed/src/instrument_hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ mod linux_impl {
}

/// Returns a singleton instance of `InstrumentHooks`.
///
/// The first call initializes the hooks, which does IPC with the runner and can be slow.
/// Harnesses must make that first call at startup, never inside a timed or warm-up region.
#[inline(always)]
pub fn instance() -> &'static Self {
static INSTANCE: OnceLock<InstrumentHooks> = OnceLock::new();
Expand Down
20 changes: 12 additions & 8 deletions crates/criterion_compat/criterion_fork/src/bencher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ impl<'a, M: Measurement> Bencher<'a, M> {
self.iterated = true;
let batch_size = size.iters_per_batch(self.iters);
assert!(batch_size != 0, "Batch size must not be zero.");
let hooks = InstrumentHooks::instance();
let time_start = Instant::now();
self.value = self.measurement.zero();

Expand All @@ -300,7 +301,7 @@ impl<'a, M: Measurement> Bencher<'a, M> {
let output = routine(input);
let end = self.measurement.end(start);
let bench_end = InstrumentHooks::current_timestamp();
InstrumentHooks::instance().add_benchmark_timestamps(bench_start, bench_end);
hooks.add_benchmark_timestamps(bench_start, bench_end);

self.value = self.measurement.add(&self.value, &end);

Expand All @@ -320,7 +321,7 @@ impl<'a, M: Measurement> Bencher<'a, M> {
outputs.extend(inputs.into_iter().map(&mut routine));
let end = self.measurement.end(start);
let bench_end = InstrumentHooks::current_timestamp();
InstrumentHooks::instance().add_benchmark_timestamps(bench_start, bench_end);
hooks.add_benchmark_timestamps(bench_start, bench_end);

self.value = self.measurement.add(&self.value, &end);

Expand Down Expand Up @@ -400,6 +401,7 @@ impl<'a, M: Measurement> Bencher<'a, M> {
self.iterated = true;
let batch_size = size.iters_per_batch(self.iters);
assert!(batch_size != 0, "Batch size must not be zero.");
let hooks = InstrumentHooks::instance();
let time_start = Instant::now();
self.value = self.measurement.zero();

Expand All @@ -412,7 +414,7 @@ impl<'a, M: Measurement> Bencher<'a, M> {
let output = routine(&mut input);
let end = self.measurement.end(start);
let bench_end = InstrumentHooks::current_timestamp();
InstrumentHooks::instance().add_benchmark_timestamps(bench_start, bench_end);
hooks.add_benchmark_timestamps(bench_start, bench_end);

self.value = self.measurement.add(&self.value, &end);

Expand All @@ -433,7 +435,7 @@ impl<'a, M: Measurement> Bencher<'a, M> {
outputs.extend(inputs.iter_mut().map(&mut routine));
let end = self.measurement.end(start);
let bench_end = InstrumentHooks::current_timestamp();
InstrumentHooks::instance().add_benchmark_timestamps(bench_start, bench_end);
hooks.add_benchmark_timestamps(bench_start, bench_end);

self.value = self.measurement.add(&self.value, &end);

Expand Down Expand Up @@ -743,6 +745,7 @@ impl<'a, 'b, A: AsyncExecutor, M: Measurement> AsyncBencher<'a, 'b, A, M> {
b.iterated = true;
let batch_size = size.iters_per_batch(b.iters);
assert!(batch_size != 0, "Batch size must not be zero.");
let hooks = InstrumentHooks::instance();
let time_start = Instant::now();
b.value = b.measurement.zero();

Expand All @@ -755,7 +758,7 @@ impl<'a, 'b, A: AsyncExecutor, M: Measurement> AsyncBencher<'a, 'b, A, M> {
let output = routine(input).await;
let end = b.measurement.end(start);
let bench_end = InstrumentHooks::current_timestamp();
InstrumentHooks::instance().add_benchmark_timestamps(bench_start, bench_end);
hooks.add_benchmark_timestamps(bench_start, bench_end);

b.value = b.measurement.add(&b.value, &end);

Expand All @@ -778,7 +781,7 @@ impl<'a, 'b, A: AsyncExecutor, M: Measurement> AsyncBencher<'a, 'b, A, M> {
}
let end = b.measurement.end(start);
let bench_end = InstrumentHooks::current_timestamp();
InstrumentHooks::instance().add_benchmark_timestamps(bench_start, bench_end);
hooks.add_benchmark_timestamps(bench_start, bench_end);

b.value = b.measurement.add(&b.value, &end);

Expand Down Expand Up @@ -864,6 +867,7 @@ impl<'a, 'b, A: AsyncExecutor, M: Measurement> AsyncBencher<'a, 'b, A, M> {
b.iterated = true;
let batch_size = size.iters_per_batch(b.iters);
assert!(batch_size != 0, "Batch size must not be zero.");
let hooks = InstrumentHooks::instance();
let time_start = Instant::now();
b.value = b.measurement.zero();

Expand All @@ -876,7 +880,7 @@ impl<'a, 'b, A: AsyncExecutor, M: Measurement> AsyncBencher<'a, 'b, A, M> {
let output = routine(&mut input).await;
let end = b.measurement.end(start);
let bench_end = InstrumentHooks::current_timestamp();
InstrumentHooks::instance().add_benchmark_timestamps(bench_start, bench_end);
hooks.add_benchmark_timestamps(bench_start, bench_end);

b.value = b.measurement.add(&b.value, &end);

Expand All @@ -900,7 +904,7 @@ impl<'a, 'b, A: AsyncExecutor, M: Measurement> AsyncBencher<'a, 'b, A, M> {
}
let end = b.measurement.end(start);
let bench_end = InstrumentHooks::current_timestamp();
InstrumentHooks::instance().add_benchmark_timestamps(bench_start, bench_end);
hooks.add_benchmark_timestamps(bench_start, bench_end);

b.value = b.measurement.add(&b.value, &end);

Expand Down
Loading