diff --git a/doc/hyperfine.1 b/doc/hyperfine.1 index 2516327..862430d 100644 --- a/doc/hyperfine.1 +++ b/doc/hyperfine.1 @@ -224,7 +224,7 @@ order benchmarks by mean runtime .HP \fB\-u\fR, \fB\-\-time\-unit\fR \fIUNIT\fP .IP -Set the time unit to be used. Possible values: millisecond, second. If +Set the time unit to be used. Possible values: microsecond, millisecond, second. If the option is not given, the time unit is determined automatically. This option affects the standard output as well as all export formats except for CSV and JSON. diff --git a/src/cli.rs b/src/cli.rs index 8063253..9748579 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -227,8 +227,8 @@ fn build_command() -> Command { .short('u') .action(ArgAction::Set) .value_name("UNIT") - .value_parser(["millisecond", "second"]) - .help("Set the time unit to be used. Possible values: millisecond, second. \ + .value_parser(["microsecond", "millisecond", "second"]) + .help("Set the time unit to be used. Possible values: microsecond, millisecond, second. \ If the option is not given, the time unit is determined automatically. \ This option affects the standard output as well as all export formats except for CSV and JSON."), ) diff --git a/src/options.rs b/src/options.rs index 90460aa..a7f820e 100644 --- a/src/options.rs +++ b/src/options.rs @@ -391,6 +391,7 @@ impl Options { } options.time_unit = match matches.get_one::("time-unit").map(|s| s.as_str()) { + Some("microsecond") => Some(Unit::MicroSecond), Some("millisecond") => Some(Unit::MilliSecond), Some("second") => Some(Unit::Second), _ => None, diff --git a/src/output/format.rs b/src/output/format.rs index fdae0dc..f1aa0d0 100644 --- a/src/output/format.rs +++ b/src/output/format.rs @@ -16,7 +16,9 @@ pub fn format_duration_unit(duration: Second, unit: Option) -> (String, Un /// Like `format_duration`, but returns the target unit as well. pub fn format_duration_value(duration: Second, unit: Option) -> (String, Unit) { - if (duration < 1.0 && unit.is_none()) || unit == Some(Unit::MilliSecond) { + if (duration < 0.001 && unit.is_none()) || unit == Some(Unit::MicroSecond) { + (Unit::MicroSecond.format(duration), Unit::MicroSecond) + } else if (duration < 1.0 && unit.is_none()) || unit == Some(Unit::MilliSecond) { (Unit::MilliSecond.format(duration), Unit::MilliSecond) } else { (Unit::Second.format(duration), Unit::Second) @@ -40,10 +42,15 @@ fn test_format_duration_unit_basic() { assert_eq!("999.0 ms", out_str); assert_eq!(Unit::MilliSecond, out_unit); + let (out_str, out_unit) = format_duration_unit(0.0005, None); + + assert_eq!("500.0 µs", out_str); + assert_eq!(Unit::MicroSecond, out_unit); + let (out_str, out_unit) = format_duration_unit(0.0, None); - assert_eq!("0.0 ms", out_str); - assert_eq!(Unit::MilliSecond, out_unit); + assert_eq!("0.0 µs", out_str); + assert_eq!(Unit::MicroSecond, out_unit); let (out_str, out_unit) = format_duration_unit(1000.0, None); @@ -62,4 +69,9 @@ fn test_format_duration_unit_with_unit() { assert_eq!("1300.0 ms", out_str); assert_eq!(Unit::MilliSecond, out_unit); + + let (out_str, out_unit) = format_duration_unit(1.3, Some(Unit::MicroSecond)); + + assert_eq!("1300000.0 µs", out_str); + assert_eq!(Unit::MicroSecond, out_unit); } diff --git a/src/util/units.rs b/src/util/units.rs index 4be7451..9528c00 100644 --- a/src/util/units.rs +++ b/src/util/units.rs @@ -10,6 +10,7 @@ pub type Second = Scalar; pub enum Unit { Second, MilliSecond, + MicroSecond, } impl Unit { @@ -18,6 +19,7 @@ impl Unit { match self { Unit::Second => String::from("s"), Unit::MilliSecond => String::from("ms"), + Unit::MicroSecond => String::from("µs"), } } @@ -26,6 +28,7 @@ impl Unit { match self { Unit::Second => format!("{:.3}", value), Unit::MilliSecond => format!("{:.1}", value * 1e3), + Unit::MicroSecond => format!("{:.1}", value * 1e6), } } } @@ -34,6 +37,7 @@ impl Unit { fn test_unit_short_name() { assert_eq!("s", Unit::Second.short_name()); assert_eq!("ms", Unit::MilliSecond.short_name()); + assert_eq!("µs", Unit::MicroSecond.short_name()); } // Note - the values are rounded when formatted. @@ -42,4 +46,6 @@ fn test_unit_format() { let value: Second = 123.456789; assert_eq!("123.457", Unit::Second.format(value)); assert_eq!("123456.8", Unit::MilliSecond.format(value)); + + assert_eq!("1234.6", Unit::MicroSecond.format(0.00123456)); } diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 5a6a67e..0c8dbf0 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -261,6 +261,15 @@ fn returns_mean_time_in_correct_unit() { .assert() .success() .stdout(predicate::str::contains("Time (mean ± σ): 1234.0 ms ±")); + + hyperfine_debug() + .arg("--time-unit=microsecond") + .arg("sleep 1.234") + .assert() + .success() + .stdout(predicate::str::contains( + "Time (mean ± σ): 1234000.0 µs ±", + )); } #[test]