From 1cb16683bcaf3223de560a7560647aadfea78d31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 2 Aug 2023 16:01:49 +0200 Subject: [PATCH] fix(bench): iter/s calculation (#20016) Ref https://github.com/denoland/deno/pull/19994#discussion_r1281267606 Ref https://github.com/denoland/deno/pull/19994#discussion_r1281243602 --- cli/tools/bench/mitata.rs | 63 ++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/cli/tools/bench/mitata.rs b/cli/tools/bench/mitata.rs index 9049b6462b..1e20e40168 100644 --- a/cli/tools/bench/mitata.rs +++ b/cli/tools/bench/mitata.rs @@ -18,16 +18,17 @@ fn avg_to_iter_per_s(time: f64) -> String { /// Return a tuple representing decimal part of provided float, as well as its /// first fractional digit. fn into_decimal_and_fractional_parts(num: f64) -> (i64, i64) { - let decimal_part = num.floor(); + let mut decimal_part = num.floor() as i64; let fractional_part = { let decs = ((num - num.floor()) * 10.0).round(); if decs == 10.0 { + decimal_part += 1; 0 } else { decs as i64 } }; - (decimal_part as i64, fractional_part) + (decimal_part, fractional_part) } fn human_readable_decimal_with_fractional( @@ -49,32 +50,6 @@ fn human_readable_decimal_with_fractional( format!("{}.{}", fmt_decimal, fractional) } -#[test] -fn test_into_decimal_and_fractional_parts() { - assert_eq!(into_decimal_and_fractional_parts(10.0), (10, 0)); - assert_eq!(into_decimal_and_fractional_parts(10.1), (10, 1)); - assert_eq!(into_decimal_and_fractional_parts(10.2), (10, 2)); - assert_eq!(into_decimal_and_fractional_parts(10.3), (10, 3)); - assert_eq!(into_decimal_and_fractional_parts(10.4), (10, 4)); - assert_eq!(into_decimal_and_fractional_parts(10.5), (10, 5)); - assert_eq!(into_decimal_and_fractional_parts(10.6), (10, 6)); - assert_eq!(into_decimal_and_fractional_parts(10.7), (10, 7)); - assert_eq!(into_decimal_and_fractional_parts(10.8), (10, 8)); - assert_eq!(into_decimal_and_fractional_parts(10.9), (10, 9)); -} - -#[test] -fn test_avg_to_iter_per_s() { - assert_eq!(avg_to_iter_per_s(55.85), "17,905,102.0"); - assert_eq!(avg_to_iter_per_s(64_870_000.0), "15.4"); - assert_eq!(avg_to_iter_per_s(104_370_000.0), "9.6"); - assert_eq!(avg_to_iter_per_s(6_400_000.0), "156.3"); - assert_eq!(avg_to_iter_per_s(46_890_000.0), "21.3"); - assert_eq!(avg_to_iter_per_s(100_000_000.0), "10.0"); - assert_eq!(avg_to_iter_per_s(1_000_000_000.0), "1.0"); - assert_eq!(avg_to_iter_per_s(5_920_000_000.0), "0.2"); -} - pub fn fmt_duration(time: f64) -> String { // SAFETY: this is safe since its just reformatting numbers unsafe { @@ -453,3 +428,35 @@ pub mod reporter { s } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_into_decimal_and_fractional_parts() { + assert_eq!(into_decimal_and_fractional_parts(10.0), (10, 0)); + assert_eq!(into_decimal_and_fractional_parts(10.1), (10, 1)); + assert_eq!(into_decimal_and_fractional_parts(10.2), (10, 2)); + assert_eq!(into_decimal_and_fractional_parts(10.3), (10, 3)); + assert_eq!(into_decimal_and_fractional_parts(10.4), (10, 4)); + assert_eq!(into_decimal_and_fractional_parts(10.5), (10, 5)); + assert_eq!(into_decimal_and_fractional_parts(10.6), (10, 6)); + assert_eq!(into_decimal_and_fractional_parts(10.7), (10, 7)); + assert_eq!(into_decimal_and_fractional_parts(10.8), (10, 8)); + assert_eq!(into_decimal_and_fractional_parts(10.9), (10, 9)); + assert_eq!(into_decimal_and_fractional_parts(10.99), (11, 0)); + } + + #[test] + fn test_avg_to_iter_per_s() { + assert_eq!(avg_to_iter_per_s(55.85), "17,905,103.0"); + assert_eq!(avg_to_iter_per_s(64_870_000.0), "15.4"); + assert_eq!(avg_to_iter_per_s(104_370_000.0), "9.6"); + assert_eq!(avg_to_iter_per_s(6_400_000.0), "156.3"); + assert_eq!(avg_to_iter_per_s(46_890_000.0), "21.3"); + assert_eq!(avg_to_iter_per_s(100_000_000.0), "10.0"); + assert_eq!(avg_to_iter_per_s(1_000_000_000.0), "1.0"); + assert_eq!(avg_to_iter_per_s(5_920_000_000.0), "0.2"); + } +}