diff --git a/crates/codspeed/src/walltime_results.rs b/crates/codspeed/src/walltime_results.rs index dc67de84..215fae84 100644 --- a/crates/codspeed/src/walltime_results.rs +++ b/crates/codspeed/src/walltime_results.rs @@ -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::>(); let mut data = Data::new(time_per_iteration_per_round_ns); @@ -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); + } }