Skip to content
Open
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
35 changes: 33 additions & 2 deletions crates/codspeed/src/walltime_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ impl WalltimeBenchmark {
let time_per_iteration_per_round_ns: Vec<_> = times_per_round_ns
.into_iter()
.zip(&iters_per_round)
.map(|(time_per_round, iter_per_round)| time_per_round / iter_per_round)
.map(|t| t as f64)
// Divide as floats: integer division truncates every round to whole nanoseconds, so a
// 1.7 ns/iter benchmark was reported as 1 ns and anything under 1 ns as 0 ns.
.map(|(time_per_round, iter_per_round)| time_per_round as f64 / *iter_per_round as f64)
.collect::<Vec<f64>>();

let mut data = Data::new(time_per_iteration_per_round_ns);
Expand Down Expand Up @@ -325,4 +326,34 @@ mod tests {
42. * total_rounds / 1_000_000_000.0
);
}

#[test]
fn test_time_per_iteration_keeps_sub_nanosecond_precision() {
// e.g. criterion's linear sampling on a ~1.7 ns/iter function
let benchmark = WalltimeBenchmark::from_runtime_data(
NAME.to_string(),
URI.to_string(),
vec![1_000, 2_000, 3_000, 4_000],
vec![1_700, 3_400, 5_100, 6_800],
None,
);
assert_eq!(benchmark.stats.min_ns, 1.7);
assert_eq!(benchmark.stats.max_ns, 1.7);
assert!((benchmark.stats.mean_ns - 1.7).abs() < 1e-12);
assert!((benchmark.stats.median_ns - 1.7).abs() < 1e-12);
}

#[test]
fn test_sub_nanosecond_iterations_are_not_reported_as_zero() {
let benchmark = WalltimeBenchmark::from_runtime_data(
NAME.to_string(),
URI.to_string(),
vec![10_000, 10_000],
vec![4_000, 6_000],
None,
);
assert_eq!(benchmark.stats.min_ns, 0.4);
assert_eq!(benchmark.stats.max_ns, 0.6);
assert!((benchmark.stats.mean_ns - 0.5).abs() < 1e-12);
}
}