Yet another API iteration
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent 7d408d6 · Browse files · 6 files·+50 added−41 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+50 added−41 removed
- other:+50 added−41 removed
Provenance
- Commit
534d568- Tree
f3d13be- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
David Peter <mail@david-peter.de>- Parent
7d408d6
6 files changed+50 added−41 removed
modified:src/benchmark/mod.rs+9 added-9 removed
328 unmodified lines329 for i in 0..count_remaining {330 let benchmark_iteration = BenchmarkIteration::Benchmark(i + 1);331 332 let msg = {333- let mean = measurements334- .time_wall_clock_mean()335- .format_auto(self.options.time_unit);333+ let t_wall_clock_mean = measurements.time_wall_clock_mean();334+ let time_unit = t_wall_clock_mean.suitable_unit();335+ let mean = t_wall_clock_mean.format(time_unit);336 format!("Current estimate: {}", mean.to_string().green())337 };338 339 if let Some(bar) = progress_bar.as_ref() {29 unmodified lines369 let time_unit = self370 .options371 .time_unit372 .unwrap_or(t_wall_clock_mean.suitable_unit());373- let mean_str = t_wall_clock_mean.format_auto(Some(time_unit));374- let min_str = measurements.min().format_auto(Some(time_unit));375- let max_str = measurements.max().format_auto(Some(time_unit));373+ let mean_str = t_wall_clock_mean.format(time_unit);374+ let min_str = measurements.min().format(time_unit);375+ let max_str = measurements.max().format(time_unit);376 let num_str = format!("{num_runs} runs", num_runs = measurements.len());377 378- let user_str = measurements.time_user_mean().format_auto(Some(time_unit));379- let system_str = measurements.time_system_mean().format_auto(Some(time_unit));378+ let user_str = measurements.time_user_mean().format(time_unit);379+ let system_str = measurements.time_system_mean().format(time_unit);380 381 if self.options.output_style != OutputStyleOption::Disabled {382 if measurements.len() == 1 {383 println!(4 unmodified lines388 user_str.blue(),389 system_str.blue()390 )?;391 } else {392- let stddev_str = measurements.stddev().unwrap().format_auto(Some(time_unit));392+ let stddev_str = measurements.stddev().unwrap().format(time_unit);393 394 console_writeln!(395 stdout,396 " Time ({} ± {}): {:>8} ± {:>8} [User: {}, System: {}]",96 unmodified linesmodified:src/export/csv.rs+2 added-1 removed
51 unmodified lines52 res.measurements.min(),53 res.measurements.max(),54 ] {55 fields.push(Cow::Owned(56- f.format(CSV_UNIT, Some(CSV_PRECISION)).into_bytes(),56+ f.format_with_precision(CSV_UNIT, CSV_PRECISION)57+ .into_bytes(),58 ))59 }60 for v in res.parameters.values() {61 fields.push(Cow::Borrowed(v.value.as_bytes()))123 unmodified linesmodified:src/export/markup.rs+2 added-4 removed
49 unmodified lines50 for entry in entries {51 let result = &entry.result;52 // prepare data row strings53 let cmd_str = result.command_with_unused_parameters().replace('|', "\\|");54- let mean_str = result55- .mean_wall_clock_time()56- .format_value_auto(Some(time_unit));54+ let mean_str = result.mean_wall_clock_time().format_value(time_unit);55 let stddev_str = if let Some(stddev) = result.measurements.stddev() {58- format!(" ± {}", stddev.format_value_auto(Some(time_unit)))56+ format!(" ± {}", stddev.format_value(time_unit))57 } else {58 "".into()59 };60 let min_str = result.measurements.min().format_value(time_unit);86 unmodified linesmodified:src/output/warnings.rs+2 added-2 removed
23 unmodified lines24 "Command took less than {min_execution_time} to complete. Note that the results might be \25 inaccurate because hyperfine can not calibrate the shell startup time much \26 more precise than this limit. You can try to use the `-N`/`--shell=none` \27 option to disable the shell completely.",28- min_execution_time = MIN_EXECUTION_TIME.format_auto(None)28+ min_execution_time = MIN_EXECUTION_TIME.format_auto()29 ),30 Warnings::NonZeroExitCode => write!(f, "Ignoring non-zero exit code."),31 Warnings::SlowInitialRun(time_first_run, ref options) => write!(32 f,33 "The first benchmarking run for this command was significantly slower than the \34 rest ({time}). This could be caused by (filesystem) caches that were not filled until \35 after the first run. {hints}",36- time=time_first_run.format_auto(None),36+ time=time_first_run.format_auto(),37 hints=match (options.warmup_in_use, options.prepare_in_use) {38 (true, true) => "You are already using both the '--warmup' option as well \39 as the '--prepare' option. Consider re-running the benchmark on a quiet system. \40 Maybe it was a random outlier. Alternatively, consider increasing the warmup \28 unmodified linesmodified:src/quantity/mod.rs+26 added-25 removed
26 unmodified lines27 }28 29 fn suitable_unit(&self) -> TimeUnit;30 31- fn format(&self, u: TimeUnit, precision: Option<usize>) -> String;32- fn format_auto(&self, time_unit: Option<TimeUnit>) -> String;33- fn format_value_auto(&self, time_unit: Option<TimeUnit>) -> String;31+ fn format_with_precision(&self, u: TimeUnit, precision: usize) -> String;32+ fn format(&self, u: TimeUnit) -> String;33+ fn format_auto(&self) -> String;34+ fn format_value(&self, time_unit: TimeUnit) -> String;35 }36 37 impl TimeQuantity for Time {38 fn suitable_unit(&self) -> TimeUnit {5 unmodified lines44 TimeUnit::Second45 }46 }47 47- /// Format the given time duration as a string.48- fn format(&self, u: TimeUnit, precision: Option<usize>) -> String {49- let precision = precision.unwrap_or_else(|| if u == TimeUnit::Second { 3 } else { 1 });48+ /// Format the time duration in the given unit with the given precision.49+ fn format_with_precision(&self, u: TimeUnit, precision: usize) -> String {50 u.format(*self, precision)51 }52 53- /// Format the given time duration as a string. The output-unit can be enforced by setting `unit` to54- /// `Some(target_unit)`. If `unit` is `None`, it will be determined automatically.55- fn format_auto(&self, time_unit: Option<TimeUnit>) -> String {56- let time_unit = time_unit.unwrap_or_else(|| self.suitable_unit());53+ /// Format the time duration in the given unit.54+ fn format(&self, unit: TimeUnit) -> String {55+ let value = self.format_with_precision(unit, unit.preferred_precision());56+ format!("{} {}", value, unit.short_name())57+ }58 58- let value = self.format(time_unit, None);59+ /// Like `format`, but without displaying the unit.60+ fn format_value(&self, unit: TimeUnit) -> String {61+ self.format_with_precision(unit, unit.preferred_precision())62+ }63 64+ /// Format the given time duration. The unit will be determined automatically.65+ fn format_auto(&self) -> String {66+ let time_unit = self.suitable_unit();67+ let value = self.format(time_unit);68 format!("{} {}", value, time_unit.short_name())69 }62-63- /// Like `format_duration`, but returns the target unit as well.64- fn format_value_auto(&self, time_unit: Option<TimeUnit>) -> String {65- let time_unit = time_unit.unwrap_or_else(|| self.suitable_unit());66-67- self.format(time_unit, None)68- }70 }71 72 pub const fn const_time_from_seconds(value: f64) -> Time {73 // Quantity::new in uom is not yet const: https://docs.rs/uom/0.36.0/uom/si/struct.Quantity.html124 unmodified lines198 199 #[test]200 fn test_format() {201 let time = Time::new::<millisecond>(123.4);201- assert_eq!(time.format_auto(Some(TimeUnit::Second)), "0.123 s");202- assert_eq!(time.format_auto(Some(TimeUnit::MilliSecond)), "123.4 ms");203- assert_eq!(time.format_auto(Some(TimeUnit::MicroSecond)), "123400.0 µs");202+ assert_eq!(time.format(TimeUnit::Second), "0.123 s");203+ assert_eq!(time.format(TimeUnit::MilliSecond), "123.4 ms");204+ assert_eq!(time.format(TimeUnit::MicroSecond), "123400.0 µs");205 206 let peak_memory_usage = Information::new::<kibibyte>(8.);207 assert_eq!(peak_memory_usage.to_string(byte), "8192 B");208 assert_eq!(peak_memory_usage.to_string(kibibyte), "8 KiB");5 unmodified lines214 Time::new::<millisecond>(123.4),215 Time::new::<millisecond>(234.5),216 ];217 let result = mean(&values);217- assert_eq!(result.format_auto(Some(TimeUnit::MilliSecond)), "178.9 ms");218+ assert_eq!(result.format(TimeUnit::MilliSecond), "178.9 ms");219 }220 221 #[test]222 fn test_suiteable_unit() {18 unmodified lines241 }242 243 #[test]244 fn test_format_duration_unit_with_unit() {244- let out = Time::new::<second>(1.3).format_auto(Some(TimeUnit::Second));245+ let out = Time::new::<second>(1.3).format(TimeUnit::Second);246 assert_eq!("1.300 s", out);247 247- let out = Time::new::<second>(1.3).format_auto(Some(TimeUnit::MilliSecond));248+ let out = Time::new::<second>(1.3).format(TimeUnit::MilliSecond);249 assert_eq!("1300.0 ms", out);250 250- let out = Time::new::<second>(1.3).format_auto(Some(TimeUnit::MicroSecond));251+ let out = Time::new::<second>(1.3).format(TimeUnit::MicroSecond);252 assert_eq!("1300000.0 µs", out);253 }modified:src/quantity/units.rs+9 added
35 unmodified lines36 Minute,37 Hour,38 }39 40+impl TimeUnit {41+ pub fn preferred_precision(self) -> usize {42+ match self {43+ TimeUnit::Second => 3,44+ _ => 1,45+ }46+ }47+}48+49 struct Dispatcher<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> {50 u: PhantomData<U>,51 }52 136 unmodified lines