mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-10-12 02:22:35 +00:00
Add support for microseconds, closes #684
This commit is contained in:
+1
-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.
|
||||
|
||||
+2
-2
@@ -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."),
|
||||
)
|
||||
|
||||
@@ -391,6 +391,7 @@ impl Options {
|
||||
}
|
||||
|
||||
options.time_unit = match matches.get_one::<String>("time-unit").map(|s| s.as_str()) {
|
||||
Some("microsecond") => Some(Unit::MicroSecond),
|
||||
Some("millisecond") => Some(Unit::MilliSecond),
|
||||
Some("second") => Some(Unit::Second),
|
||||
_ => None,
|
||||
|
||||
+15
-3
@@ -16,7 +16,9 @@ pub fn format_duration_unit(duration: Second, unit: Option<Unit>) -> (String, Un
|
||||
|
||||
/// Like `format_duration`, but returns the target unit as well.
|
||||
pub fn format_duration_value(duration: Second, unit: Option<Unit>) -> (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);
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user