Skip to content

Commit d853ce3

Browse files
feat: use custom time formatting to be in line with the rest of CodSpeed
1 parent ba3f8a1 commit d853ce3

File tree

5 files changed

+98
-16
lines changed

5 files changed

+98
-16
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ sysinfo = { version = "0.33.1", features = ["serde"] }
4646
indicatif = "0.17.8"
4747
console = "0.15.8"
4848
async-trait = "0.1.82"
49-
humantime = "2.2.0"
5049

5150
[dev-dependencies]
5251
temp-env = { version = "0.3.6", features = ["async_closure"] }

src/run/helpers/format_duration.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
fn get_nearest_exponent(val: f64) -> i32 {
2+
match val {
3+
0.0 => 0,
4+
x if x >= 10f64.powi(0) => 0,
5+
x if x >= 10f64.powi(-3) => 3,
6+
x if x >= 10f64.powi(-6) => 6,
7+
_ => 9,
8+
}
9+
}
10+
11+
fn format_shifted_value(value: f64, fraction_digits: usize) -> String {
12+
if fraction_digits == 0 && value.fract() == 0.0 {
13+
format!("{:.0}", value)
14+
} else {
15+
format!("{:.1$}", value, fraction_digits)
16+
}
17+
}
18+
19+
fn format_duration_to_exponent(val: f64, exponent: i32, fraction_digits: usize) -> String {
20+
if val < 10f64.powi(-9) {
21+
return "< 1 ns".to_string();
22+
}
23+
24+
match exponent {
25+
0 => format!("{} s", format_shifted_value(val, fraction_digits)),
26+
3 => format!(
27+
"{} ms",
28+
format_shifted_value(val * 10f64.powi(3), fraction_digits)
29+
),
30+
6 => format!(
31+
"{} µs",
32+
format_shifted_value(val * 10f64.powi(6), fraction_digits)
33+
),
34+
9 => format!(
35+
"{} ns",
36+
format_shifted_value(val * 10f64.powi(9), fraction_digits)
37+
),
38+
_ => format!("{} s", val),
39+
}
40+
}
41+
42+
pub(crate) fn format_duration(val: f64, fraction_digits: Option<usize>) -> String {
43+
let fraction_digits = fraction_digits.unwrap_or(1); // Default to 1 decimal place
44+
let exponent = get_nearest_exponent(val);
45+
format_duration_to_exponent(val, exponent, fraction_digits)
46+
}
47+
48+
#[cfg(test)]
49+
mod tests {
50+
use super::*;
51+
52+
#[test]
53+
fn test_format_duration_seconds() {
54+
assert_eq!(format_duration(1.1, None), "1.1 s");
55+
assert_eq!(format_duration(1.1, Some(0)), "1 s");
56+
assert_eq!(format_duration(1.234, Some(2)), "1.23 s");
57+
}
58+
59+
#[test]
60+
fn test_format_duration_milliseconds() {
61+
assert_eq!(format_duration(0.001, None), "1.0 ms");
62+
assert_eq!(format_duration(0.001, Some(0)), "1 ms");
63+
assert_eq!(format_duration(0.001234, Some(2)), "1.23 ms");
64+
}
65+
66+
#[test]
67+
fn test_format_duration_microseconds() {
68+
assert_eq!(format_duration(0.000001, None), "1.0 µs");
69+
assert_eq!(format_duration(0.000001234, Some(2)), "1.23 µs");
70+
}
71+
72+
#[test]
73+
fn test_format_duration_nanoseconds() {
74+
assert_eq!(format_duration(0.000000001, None), "1.0 ns");
75+
assert_eq!(format_duration(0.000000001234, Some(2)), "1.23 ns");
76+
}
77+
78+
#[test]
79+
fn test_format_duration_less_than_nanosecond() {
80+
assert_eq!(format_duration(0.0000000001, None), "< 1 ns");
81+
}
82+
83+
#[test]
84+
fn test_format_duration_zero() {
85+
assert_eq!(format_duration(0.0, None), "< 1 ns");
86+
}
87+
}

src/run/helpers/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
mod download_file;
22
mod find_repository_root;
3+
mod format_duration;
34
mod get_env_var;
45
mod parse_git_remote;
56

6-
pub use download_file::download_file;
7-
pub use find_repository_root::find_repository_root;
8-
pub use get_env_var::get_env_variable;
9-
pub use parse_git_remote::*;
7+
pub(crate) use download_file::download_file;
8+
pub(crate) use find_repository_root::find_repository_root;
9+
pub(crate) use format_duration::format_duration;
10+
pub(crate) use get_env_var::get_env_variable;
11+
pub(crate) use parse_git_remote::*;

src/run/poll_results.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::api_client::{
77
CodSpeedAPIClient, FetchLocalRunReportResponse, FetchLocalRunReportVars, RunStatus,
88
};
99
use crate::prelude::*;
10+
use crate::run::helpers;
1011

1112
use super::run_environment::RunEnvironmentProvider;
1213

@@ -98,19 +99,19 @@ pub async fn poll_results(
9899
end_group!();
99100

100101
if !response.run.results.is_empty() {
101-
start_group!("Benchmarks");
102+
start_group!("Benchmark results");
102103
for result in response.run.results {
103104
let benchmark_name = result.benchmark.name;
104-
let time: humantime::Duration = Duration::from_secs_f64(result.time).into();
105-
let time_text = style(format!("{}", time)).bold();
105+
let time = helpers::format_duration(result.time, Some(2));
106+
107+
info!("{}: {}", benchmark_name, style(time).bold());
106108

107109
if output_json {
108110
log_json!(format!(
109111
"{{\"event\": \"benchmark_ran\", \"name\": \"{}\", \"time\": \"{}\"}}",
110112
benchmark_name, result.time,
111113
));
112114
}
113-
info!("{} - Time: {}", benchmark_name, time_text);
114115
}
115116
end_group!();
116117
}

0 commit comments

Comments
 (0)