Skip to content

Commit 177c4d1

Browse files
committed
fix: divan walltime metric
1 parent 5caa771 commit 177c4d1

4 files changed

Lines changed: 49 additions & 1 deletion

File tree

crates/codspeed/src/walltime.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ impl RawWallTimeData {
5050
/// Entry point called in patched integration to harvest raw walltime data
5151
///
5252
/// `CODSPEED_CARGO_WORKSPACE_ROOT` is expected to be set for this to work
53+
///
54+
/// # Arguments
55+
///
56+
/// - `scope`: The used integration, e.g. "divan" or "criterion"
57+
/// - `name`: The name of the benchmark
58+
/// - `uri`: The URI of the benchmark
59+
/// - `iter_per_round`: The number of iterations per round for the benchmark (=sample_size)
60+
/// - `max_time_ns`: The time limit for the benchmark in nanoseconds (if defined)
61+
/// - `times_ns`: The **average** time per iteration in nanoseconds. Calculated as `total_time_ns / iter_per_round`
62+
///
5363
pub fn collect_raw_walltime_results(
5464
scope: &str,
5565
name: String,

crates/divan_compat/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ codspeed-divan-compat-macros = { version = "=2.10.1", path = './macros' }
2525
[[bench]]
2626
name = "basic_example"
2727
harness = false
28+
29+
[[bench]]
30+
name = "sleep_benches"
31+
harness = false
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#[divan::bench]
2+
fn sleep_1ms() {
3+
std::thread::sleep(std::time::Duration::from_millis(1));
4+
}
5+
6+
#[divan::bench]
7+
fn sleep_10ms() {
8+
std::thread::sleep(std::time::Duration::from_millis(10));
9+
}
10+
11+
#[divan::bench]
12+
fn sleep_50ms() {
13+
std::thread::sleep(std::time::Duration::from_millis(50));
14+
}
15+
16+
#[divan::bench]
17+
fn sleep_100ms() {
18+
std::thread::sleep(std::time::Duration::from_millis(100));
19+
}
20+
21+
// Tests COD-1044, do not modify the sample size or count!
22+
#[divan::bench(sample_size = 3, sample_count = 6)]
23+
fn sleep_100ms_with_custom_sample() {
24+
std::thread::sleep(std::time::Duration::from_millis(100));
25+
}
26+
27+
fn main() {
28+
codspeed_divan_compat::main();
29+
}

crates/divan_compat/divan_fork/src/divan.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,13 @@ mod codspeed {
424424
};
425425

426426
let iter_per_round = bench_context.samples.sample_size;
427+
428+
// Note: times_ns = total time of the round execution, but we need the time per iteration.
429+
// See: BenchContext::compute_stats in bench/mod.rs
427430
let times_ns: Vec<_> =
428431
bench_context.samples.time_samples.iter().map(|s| s.duration.picos / 1_000).collect();
432+
let avg_times_ns: Vec<_> = times_ns.iter().map(|&t| t / iter_per_round as u128).collect();
433+
429434
let max_time_ns = bench_context.options.max_time.map(|t| t.as_nanos());
430435

431436
if let Err(error) = ::codspeed::fifo::send_cmd(codspeed::fifo::Command::CurrentBenchmark {
@@ -443,7 +448,7 @@ mod codspeed {
443448
uri,
444449
iter_per_round,
445450
max_time_ns,
446-
times_ns,
451+
avg_times_ns,
447452
);
448453
}
449454
}

0 commit comments

Comments
 (0)