Avoid duplicated units in automatic quantity formatting
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 cf38199 · Browse files · 4 files·+28 added−96 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+28 added−96 removed
- other:+28 added−96 removed
Provenance
- Commit
8ed8757- Tree
31d309a- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
David Peter <mail@david-peter.de>- Parent
cf38199
4 files changed+28 added−96 removed
modified:src/benchmark/mod.rs+4 added-1 removed
329 unmodified lines330 let benchmark_iteration = BenchmarkIteration::Benchmark(i + 1);331 332 let msg = {333 let t_wall_clock_mean = measurements.time_wall_clock_mean();334- let time_unit = t_wall_clock_mean.suitable_unit();334+ let time_unit = self335+ .options336+ .time_unit337+ .unwrap_or(t_wall_clock_mean.suitable_unit());338 let mean = t_wall_clock_mean.format(time_unit);339 format!("Current estimate: {}", mean.to_string().green())340 };341 155 unmodified linesmodified:src/output/warnings.rs+3 added-3 removed
1 use std::fmt;2 3 use crate::benchmark::MIN_EXECUTION_TIME;4-use crate::quantity::{FormatQuantity, Time};4+use crate::quantity::{FormatQuantity, Time, TimeUnit};5 6 pub struct OutlierWarningOptions {7 pub warmup_in_use: bool,8 pub prepare_in_use: bool,11 unmodified lines20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {21 match *self {22 Warnings::FastExecutionTime => write!(23 f,24- "Command took less than {min_execution_time} to complete. Note that the results might be \24+ "Command took less than {} ms 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()28+ MIN_EXECUTION_TIME.format_with_precision(TimeUnit::MilliSecond, 0)29 ),30 Warnings::NonZeroExitCode => write!(f, "Ignoring non-zero exit code."),31 Warnings::SlowInitialRun(time_first_run, ref options) => write!(32 f,36 unmodified linesmodified:src/quantity/mod.rs+4 added-7 removed
1-use core::f64;1 use std::marker::PhantomData;2 3 use serde::ser::SerializeStruct;4 use serde::Serializer;47 unmodified lines52 }53 54 /// Format the given time duration. The unit will be determined automatically.55 fn format_auto(&self) -> String {57- let unit = self.suitable_unit();58- let value = self.format(unit);59- format!("{} {}", value, unit.short_name())56+ self.format(self.suitable_unit())57 }58 59 /// Like `format`, but without displaying the unit.60 fn format_value(&self, unit: TimeUnit) -> String {37 unmodified lines98 }99 100 /// Format the given information. The unit will be determined automatically.101 fn format_auto(&self) -> String {105- let unit = self.suitable_unit();106- let value = self.format(unit);107- format!("{} {}", value, unit.short_name())102+ self.format(self.suitable_unit())103 }104 105 /// Like `format`, but without displaying the unit.106 fn format_value(&self, unit: InformationUnit) -> String {93 unmodified lines200 fn test_format() {201 let time = Time::new::<millisecond>(123.4);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_auto(), "123.4 ms");205 assert_eq!(time.format(TimeUnit::MicroSecond), "123400.0 µs");206 207 let peak_memory_usage = Information::new::<kibibyte>(8.);208+ assert_eq!(peak_memory_usage.format_auto(), "8.0 KiB");209 assert_eq!(peak_memory_usage.format(InformationUnit::Byte), "8192 B");210 assert_eq!(211 peak_memory_usage.format(InformationUnit::KibiByte),212 "8.0 KiB"14 unmodified linesmodified:src/quantity/units.rs+17 added-85 removed
137 unmodified lines138 format!("{value:.precision$}", value = value.get::<U>())139 }140 }141 142-/// Supported information units143-#[derive(Debug, Clone, Copy, PartialEq, Eq)]144-145-pub enum InformationUnit {146- Byte,147- KibiByte,148- MebiByte,149- GibiByte,150- TebiByte,151-}152-153-impl IsUnit for InformationUnit {154- type Quantity = Information;155-156- fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Information>> {157- match self {158- InformationUnit::Byte => Box::new(InformationUnitDispatcher::<byte>::new()),159- InformationUnit::KibiByte => Box::new(InformationUnitDispatcher::<kibibyte>::new()),160- InformationUnit::MebiByte => Box::new(InformationUnitDispatcher::<mebibyte>::new()),161- InformationUnit::GibiByte => Box::new(InformationUnitDispatcher::<gibibyte>::new()),162- InformationUnit::TebiByte => Box::new(InformationUnitDispatcher::<tebibyte>::new()),163- }164- }165-166- fn preferred_precision(&self) -> usize {167- match self {168- InformationUnit::Byte => 0,169- _ => 1,170- }171- }172-}173-174-pub trait UnitImpl {175- type Quantity;176-177- fn short_name(&self) -> &'static str;178- fn format_value(&self, value: Self::Quantity, precision: usize) -> String;179-}180-181-struct TimeUnitDispatcher<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> {182- u: PhantomData<U>,183-}184-185-impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> TimeUnitDispatcher<U> {186- fn new() -> Self {187- TimeUnitDispatcher { u: PhantomData }188- }189-}190-191-impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for TimeUnitDispatcher<U> {192- type Quantity = Time;193-194- fn short_name(&self) -> &'static str {195- U::abbreviation()196- }197-198- fn format_value(&self, value: Time, precision: usize) -> String {199- format!("{value:.precision$}", value = value.get::<U>())200- }201-}202-203-struct InformationUnitDispatcher<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> {204- u: PhantomData<U>,205-}206-207-impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> InformationUnitDispatcher<U> {208- fn new() -> Self {209- InformationUnitDispatcher { u: PhantomData }210- }211-}212-213-impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> UnitImpl214- for InformationUnitDispatcher<U>215-{216- type Quantity = Information;217-218- fn short_name(&self) -> &'static str {219- U::abbreviation()220- }221-222- fn format_value(&self, value: Information, precision: usize) -> String {223- format!("{value:.precision$}", value = value.get::<U>())224- }225-}226-142 #[test]143 fn test_time_unit_short_name() {144 assert_eq!("s", TimeUnit::Second.short_name());145 assert_eq!("ms", TimeUnit::MilliSecond.short_name());146 assert_eq!("µs", TimeUnit::MicroSecond.short_name());147 assert_eq!("min", TimeUnit::Minute.short_name());148 assert_eq!("h", TimeUnit::Hour.short_name());149 }150+151+// Note - the values are rounded when formatted.152+#[test]153+fn test_unit_format() {154+ use crate::quantity::FormatQuantity;155+156+ let value = Time::new::<second>(123.456789);157+ assert_eq!("123.457", value.format_value(TimeUnit::Second));158+ assert_eq!("123456.8", value.format_value(TimeUnit::MilliSecond));159+160+ assert_eq!(161+ "1234.6",162+ Time::new::<second>(0.00123456).format_value(TimeUnit::MicroSecond)163+ );164+ assert_eq!("2.1", value.format_value(TimeUnit::Minute));165+ assert_eq!("0.0", value.format_value(TimeUnit::Hour));166+}