From ba3f8a15a14efbfc5d90aa0fb858808f34fbdf8a Mon Sep 17 00:00:00 2001 From: Guillaume Lagrange Date: Fri, 4 Apr 2025 10:53:49 +0200 Subject: [PATCH 1/2] fix: fix test randomly failing due to other test run in parallel --- src/run/helpers/get_env_var.rs | 4 ++-- src/run/instruments/mongo_tracer.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/run/helpers/get_env_var.rs b/src/run/helpers/get_env_var.rs index ac37de1d..5380c7c7 100644 --- a/src/run/helpers/get_env_var.rs +++ b/src/run/helpers/get_env_var.rs @@ -22,11 +22,11 @@ mod tests { #[test] fn test_get_env_variable_not_found() { - let result = get_env_variable("MY_CUSTOM_ENV_VAR"); + let result = get_env_variable("MY_CUSTOM_ENV_VAR_NOT_FOUND"); assert!(result.is_err()); assert_eq!( result.unwrap_err().to_string(), - "MY_CUSTOM_ENV_VAR environment variable not found" + "MY_CUSTOM_ENV_VAR_NOT_FOUND environment variable not found" ); } } diff --git a/src/run/instruments/mongo_tracer.rs b/src/run/instruments/mongo_tracer.rs index 298b1b9b..7078ac6c 100644 --- a/src/run/instruments/mongo_tracer.rs +++ b/src/run/instruments/mongo_tracer.rs @@ -290,7 +290,7 @@ mod tests { fn test_try_from_empty_env() { let profile_folder = PathBuf::from("/tmp/codspeed"); let mongodb_config = MongoDBConfig { - uri_env_name: Some("MONGO_URL".into()), + uri_env_name: Some("MONGO_URL_NOT_FOUND".into()), }; let tracer = MongoTracer::try_from(&profile_folder, &mongodb_config); @@ -298,7 +298,7 @@ mod tests { assert!(tracer.is_err()); assert_eq!( tracer.unwrap_err().to_string(), - "MONGO_URL environment variable not found" + "MONGO_URL_NOT_FOUND environment variable not found" ); } From d853ce36b97d66f711f043dc120bef7ef31eddcd Mon Sep 17 00:00:00 2001 From: Guillaume Lagrange Date: Fri, 4 Apr 2025 10:58:43 +0200 Subject: [PATCH 2/2] feat: use custom time formatting to be in line with the rest of CodSpeed --- Cargo.lock | 7 --- Cargo.toml | 1 - src/run/helpers/format_duration.rs | 87 ++++++++++++++++++++++++++++++ src/run/helpers/mod.rs | 10 ++-- src/run/poll_results.rs | 9 ++-- 5 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 src/run/helpers/format_duration.rs diff --git a/Cargo.lock b/Cargo.lock index 7e9a7dc3..d3e473d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -268,7 +268,6 @@ dependencies = [ "console", "git2", "gql_client", - "humantime", "indicatif", "insta", "itertools", @@ -722,12 +721,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" -[[package]] -name = "humantime" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b112acc8b3adf4b107a8ec20977da0273a8c386765a3ec0229bd500a1443f9f" - [[package]] name = "hyper" version = "0.14.32" diff --git a/Cargo.toml b/Cargo.toml index 1b38281d..c1d026ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,7 +46,6 @@ sysinfo = { version = "0.33.1", features = ["serde"] } indicatif = "0.17.8" console = "0.15.8" async-trait = "0.1.82" -humantime = "2.2.0" [dev-dependencies] temp-env = { version = "0.3.6", features = ["async_closure"] } diff --git a/src/run/helpers/format_duration.rs b/src/run/helpers/format_duration.rs new file mode 100644 index 00000000..bcca12a2 --- /dev/null +++ b/src/run/helpers/format_duration.rs @@ -0,0 +1,87 @@ +fn get_nearest_exponent(val: f64) -> i32 { + match val { + 0.0 => 0, + x if x >= 10f64.powi(0) => 0, + x if x >= 10f64.powi(-3) => 3, + x if x >= 10f64.powi(-6) => 6, + _ => 9, + } +} + +fn format_shifted_value(value: f64, fraction_digits: usize) -> String { + if fraction_digits == 0 && value.fract() == 0.0 { + format!("{:.0}", value) + } else { + format!("{:.1$}", value, fraction_digits) + } +} + +fn format_duration_to_exponent(val: f64, exponent: i32, fraction_digits: usize) -> String { + if val < 10f64.powi(-9) { + return "< 1 ns".to_string(); + } + + match exponent { + 0 => format!("{} s", format_shifted_value(val, fraction_digits)), + 3 => format!( + "{} ms", + format_shifted_value(val * 10f64.powi(3), fraction_digits) + ), + 6 => format!( + "{} µs", + format_shifted_value(val * 10f64.powi(6), fraction_digits) + ), + 9 => format!( + "{} ns", + format_shifted_value(val * 10f64.powi(9), fraction_digits) + ), + _ => format!("{} s", val), + } +} + +pub(crate) fn format_duration(val: f64, fraction_digits: Option) -> String { + let fraction_digits = fraction_digits.unwrap_or(1); // Default to 1 decimal place + let exponent = get_nearest_exponent(val); + format_duration_to_exponent(val, exponent, fraction_digits) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_format_duration_seconds() { + assert_eq!(format_duration(1.1, None), "1.1 s"); + assert_eq!(format_duration(1.1, Some(0)), "1 s"); + assert_eq!(format_duration(1.234, Some(2)), "1.23 s"); + } + + #[test] + fn test_format_duration_milliseconds() { + assert_eq!(format_duration(0.001, None), "1.0 ms"); + assert_eq!(format_duration(0.001, Some(0)), "1 ms"); + assert_eq!(format_duration(0.001234, Some(2)), "1.23 ms"); + } + + #[test] + fn test_format_duration_microseconds() { + assert_eq!(format_duration(0.000001, None), "1.0 µs"); + assert_eq!(format_duration(0.000001234, Some(2)), "1.23 µs"); + } + + #[test] + fn test_format_duration_nanoseconds() { + assert_eq!(format_duration(0.000000001, None), "1.0 ns"); + assert_eq!(format_duration(0.000000001234, Some(2)), "1.23 ns"); + } + + #[test] + fn test_format_duration_less_than_nanosecond() { + assert_eq!(format_duration(0.0000000001, None), "< 1 ns"); + } + + #[test] + fn test_format_duration_zero() { + assert_eq!(format_duration(0.0, None), "< 1 ns"); + } +} diff --git a/src/run/helpers/mod.rs b/src/run/helpers/mod.rs index cc26c283..9f5d7422 100644 --- a/src/run/helpers/mod.rs +++ b/src/run/helpers/mod.rs @@ -1,9 +1,11 @@ mod download_file; mod find_repository_root; +mod format_duration; mod get_env_var; mod parse_git_remote; -pub use download_file::download_file; -pub use find_repository_root::find_repository_root; -pub use get_env_var::get_env_variable; -pub use parse_git_remote::*; +pub(crate) use download_file::download_file; +pub(crate) use find_repository_root::find_repository_root; +pub(crate) use format_duration::format_duration; +pub(crate) use get_env_var::get_env_variable; +pub(crate) use parse_git_remote::*; diff --git a/src/run/poll_results.rs b/src/run/poll_results.rs index acd8198d..c1e4ede5 100644 --- a/src/run/poll_results.rs +++ b/src/run/poll_results.rs @@ -7,6 +7,7 @@ use crate::api_client::{ CodSpeedAPIClient, FetchLocalRunReportResponse, FetchLocalRunReportVars, RunStatus, }; use crate::prelude::*; +use crate::run::helpers; use super::run_environment::RunEnvironmentProvider; @@ -98,11 +99,12 @@ pub async fn poll_results( end_group!(); if !response.run.results.is_empty() { - start_group!("Benchmarks"); + start_group!("Benchmark results"); for result in response.run.results { let benchmark_name = result.benchmark.name; - let time: humantime::Duration = Duration::from_secs_f64(result.time).into(); - let time_text = style(format!("{}", time)).bold(); + let time = helpers::format_duration(result.time, Some(2)); + + info!("{}: {}", benchmark_name, style(time).bold()); if output_json { log_json!(format!( @@ -110,7 +112,6 @@ pub async fn poll_results( benchmark_name, result.time, )); } - info!("{} - Time: {}", benchmark_name, time_text); } end_group!(); }