Skip to content
Myr

Merge pull request #960 from sharkdp/codex/time-unit-aliases

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Support minutes, hours, and time unit aliases

authored by David Peter · committed by GitHub · parents 73cf51e, 09faea5 · Browse files · 5 files+41 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+41 added−10 removed

  • other:+41 added−10 removed

5 files+41 added−10 removed

modified:doc/hyperfine.1+15 added-2 removed
253 unmodified lines
254.RE255.HP256\fB\-u\fR, \fB\-\-time\-unit\fR \fIUNIT\fP257.IP258Set the time unit to be used. Possible values: microsecond, millisecond, second. If259the option is not given, the time unit is determined automatically.258Set the time unit to be used. If the option is not given, the time unit is259determined automatically. Possible values:260.RS261.IP "microsecond"262Aliases: µs, us, microseconds.263.IP "millisecond"264Aliases: ms, milliseconds.265.IP "second"266Aliases: s, seconds.267.IP "minute"268Aliases: min, minutes.269.IP "hour"270Aliases: h, hours.271.RE272.IP273This option affects the standard output as well as all export formats274except for CSV and JSON.275.HP276\fB\-\-export\-asciidoc\fR \fIFILE\fP
127 unmodified lines
modified:src/output/format.rs+2 added-1 removed
19 unmodified lines
20 (Unit::MicroSecond.format(duration), Unit::MicroSecond)21 } else if (duration < 1.0 && unit.is_none()) || unit == Some(Unit::MilliSecond) {22 (Unit::MilliSecond.format(duration), Unit::MilliSecond)23 } else {24 (Unit::Second.format(duration), Unit::Second)24 let unit = unit.unwrap_or(Unit::Second);25 (unit.format(duration), unit)26 }27}2829#[test]
49 unmodified lines
modified:src/util/units.rs+10 added
9 unmodified lines
10pub enum Unit {11 Second,12 MilliSecond,13 MicroSecond,14 Minute,15 Hour,16}1718impl Unit {19 /// The abbreviation of the Unit.
1 unmodified line
21 match self {22 Unit::Second => String::from("s"),23 Unit::MilliSecond => String::from("ms"),24 Unit::MicroSecond => String::from("µs"),25 Unit::Minute => String::from("min"),26 Unit::Hour => String::from("h"),27 }28 }2930 /// Returns the Second value formatted for the Unit.
1 unmodified line
32 match self {33 Unit::Second => format!("{value:.3}"),34 Unit::MilliSecond => format!("{:.1}", value * 1e3),35 Unit::MicroSecond => format!("{:.1}", value * 1e6),36 Unit::Minute => format!("{:.1}", value / 60.0),37 Unit::Hour => format!("{:.1}", value / 3600.0),38 }39 }40}41
1 unmodified line
43fn test_unit_short_name() {44 assert_eq!("s", Unit::Second.short_name());45 assert_eq!("ms", Unit::MilliSecond.short_name());46 assert_eq!("µs", Unit::MicroSecond.short_name());47 assert_eq!("min", Unit::Minute.short_name());48 assert_eq!("h", Unit::Hour.short_name());49}5051// Note - the values are rounded when formatted.52#[test]
2 unmodified lines
55 assert_eq!("123.457", Unit::Second.format(value));56 assert_eq!("123456.8", Unit::MilliSecond.format(value));5758 assert_eq!("1234.6", Unit::MicroSecond.format(0.00123456));59 assert_eq!("2.1", Unit::Minute.format(value));60 assert_eq!("0.0", Unit::Hour.format(value));61}
modified:src/cli.rs+9 added-4 removed
276 unmodified lines
277 .long("time-unit")278 .short('u')279 .action(ArgAction::Set)280 .value_name("UNIT")281 .value_parser(["microsecond", "millisecond", "second"])282 .help("Set the time unit to be used. Possible values: microsecond, millisecond, second. \283 If the option is not given, the time unit is determined automatically. \284 This option affects the standard output as well as all export formats except for CSV and JSON."),281 .value_parser(["µs", "us", "microsecond", "microseconds", "ms", "millisecond", "milliseconds", "s", "second", "seconds", "min", "minute", "minutes", "h", "hour", "hours"])282 .help("Set the time unit to be used. If the option is not given, the time unit is determined automatically. \283 This option affects the standard output as well as all export formats except for CSV and JSON.\n\284 Possible values:\n \285 * 'µs', 'us', 'microsecond', 'microseconds'\n \286 * 'ms', 'millisecond', 'milliseconds'\n \287 * 's', 'second', 'seconds'\n \288 * 'min', 'minute', 'minutes'\n \289 * 'h', 'hour', 'hours'"),290 )291 .arg(292 Arg::new("export-asciidoc")293 .long("export-asciidoc")
130 unmodified lines
modified:src/options.rs+5 added-3 removed
427 unmodified lines
428 };429 }430431 options.time_unit = match matches.get_one::<String>("time-unit").map(|s| s.as_str()) {432 Some("microsecond") => Some(Unit::MicroSecond),433 Some("millisecond") => Some(Unit::MilliSecond),434 Some("second") => Some(Unit::Second),432 Some("µs" | "us" | "microsecond" | "microseconds") => Some(Unit::MicroSecond),433 Some("ms" | "millisecond" | "milliseconds") => Some(Unit::MilliSecond),434 Some("s" | "second" | "seconds") => Some(Unit::Second),435 Some("min" | "minute" | "minutes") => Some(Unit::Minute),436 Some("h" | "hour" | "hours") => Some(Unit::Hour),437 _ => None,438 };439440 if let Some(time) = matches.get_one::<String>("min-benchmarking-time") {
102 unmodified lines