Make memory units available
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 534d568 · Browse files · 8 files·+143 added−77 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+143 added−77 removed
- other:+143 added−77 removed
Provenance
- Commit
f342e1f- Tree
65da5e4- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
David Peter <mail@david-peter.de>- Parent
534d568
8 files changed+143 added−77 removed
modified:src/benchmark/executor.rs+1 added-1 removed
7 unmodified lines8 use crate::options::{9 CmdFailureAction, CommandInputPolicy, CommandOutputPolicy, Options, OutputStyleOption, Shell,10 };11 use crate::output::progress_bar::get_progress_bar;12-use crate::quantity::{second, Information, InformationQuantity, Time, TimeQuantity};12+use crate::quantity::{second, Information, Quantity, Time};13 use crate::timer::execute_and_measure;14 use crate::util::randomized_environment_offset;15 16 use anyhow::{bail, Context, Result};358 unmodified linesmodified:src/benchmark/mod.rs+1 added-1 removed
20 unmodified lines21 finish_initial_measurement, get_progress_bar, start_initial_measurement,22 };23 use crate::output::warnings::{OutlierWarningOptions, Warnings};24 use crate::parameter::ParameterNameAndValue;25-use crate::quantity::{self, const_time_from_seconds, Time, TimeQuantity};25+use crate::quantity::{self, const_time_from_seconds, Time, Quantity};26 use benchmark_result::BenchmarkResult;27 28 use anyhow::{anyhow, Result};29 use colored::*;463 unmodified linesmodified:src/benchmark/relative_speed.rs+1 added-1 removed
1 unmodified line2 3 use super::benchmark_result::BenchmarkResult;4 use crate::{5 options::SortOrder,6- quantity::{self, Ratio, Time, TimeQuantity},6+ quantity::{self, Ratio, Time, Quantity},7 };8 9 #[derive(Debug)]10 pub struct BenchmarkResultWithRelativeSpeed<'a> {225 unmodified linesmodified:src/export/csv.rs+2 added-2 removed
3 unmodified lines4 5 use super::Exporter;6 use crate::benchmark::benchmark_result::BenchmarkResult;7 use crate::options::SortOrder;8-use crate::quantity::{TimeQuantity, TimeUnit};8+use crate::quantity::{Quantity, TimeUnit};9 10 use anyhow::Result;11 12 #[derive(Default)]57 unmodified lines70 #[test]71 fn test_csv() {72 use crate::benchmark::benchmark_result::Parameter;73 use crate::benchmark::measurement::{Measurement, Measurements};74- use crate::quantity::{byte, second, Information, Time, TimeQuantity};74+ use crate::quantity::{byte, second, Information, Quantity, Time};75 76 use std::collections::BTreeMap;77 use std::process::ExitStatus;78 106 unmodified linesmodified:src/export/tests.rs+1 added-1 removed
1 unmodified line2 use crate::benchmark::benchmark_result::BenchmarkResult;3 use crate::benchmark::measurement::{Measurement, Measurements};4 use crate::export::asciidoc::AsciidocExporter;5 use crate::export::orgmode::OrgmodeExporter;6-use crate::quantity::{byte, second, Information, Time, TimeQuantity, TimeUnit};6+use crate::quantity::{byte, second, Information, Quantity, Time, TimeUnit};7 use crate::{export::markdown::MarkdownExporter, options::SortOrder};8 use std::collections::BTreeMap;9 use std::process::ExitStatus;10 407 unmodified linesmodified:src/output/warnings.rs+1 added-1 removed
1 use std::fmt;2 3 use crate::benchmark::MIN_EXECUTION_TIME;4-use crate::quantity::{Time, TimeQuantity};4+use crate::quantity::{Time, Quantity};5 6 pub struct OutlierWarningOptions {7 pub warmup_in_use: bool,8 pub prepare_in_use: bool,60 unmodified linesmodified:src/quantity/mod.rs+61 added-41 removed
1 unmodified line2 use std::marker::PhantomData;3 4 use serde::ser::SerializeStruct;5 use serde::Serializer;6-use uom::{si, Conversion};6 7+use uom::si;8-pub use uom::si::information::{byte, kibibyte};8+pub use uom::si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};9 pub use uom::si::ratio::ratio;10 pub use uom::si::time::{hour, microsecond, millisecond, minute, nanosecond, second};11 12 pub use si::f64::{Information, Ratio, Time};13 14-pub use si::f64::{Information, Ratio, Time};15-pub use si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};16-pub use si::ratio::ratio;17-pub use si::time::{hour, microsecond, millisecond, minute, nanosecond, second};18-pub use uom::num_traits::Zero;19-14 pub use units::{InformationUnit, IsUnit, TimeUnit};15 16 mod units;17 24-pub trait TimeQuantity {25- fn zero() -> Time {26- Time::new::<second>(0.0)27- }18+pub trait Quantity {19+ type Unit;20 29- fn suitable_unit(&self) -> TimeUnit;21+ fn zero() -> Self;22 23+ fn suitable_unit(&self) -> Self::Unit;24+31- fn format_with_precision(&self, u: TimeUnit, precision: usize) -> String;32- fn format(&self, u: TimeUnit) -> String;25+ fn format_with_precision(&self, unit: Self::Unit, precision: usize) -> String;26+ fn format(&self, unit: Self::Unit) -> String;27 fn format_auto(&self) -> String;34- fn format_value(&self, time_unit: TimeUnit) -> String;28+ fn format_value(&self, unit: Self::Unit) -> String;29 }30 37-impl TimeQuantity for Time {31+impl Quantity for Time {32+ type Unit = TimeUnit;33+34+ fn zero() -> Time {35+ Time::new::<second>(0.0)36+ }37+38 fn suitable_unit(&self) -> TimeUnit {39 if *self < Time::new::<millisecond>(1.0) {40 TimeUnit::MicroSecond41 } else if *self < Time::new::<second>(1.0) {13 unmodified lines55 let value = self.format_with_precision(unit, unit.preferred_precision());56 format!("{} {}", value, unit.short_name())57 }58 59+ /// Format the given time duration. The unit will be determined automatically.60+ fn format_auto(&self) -> String {61+ let unit = self.suitable_unit();62+ let value = self.format(unit);63+ format!("{} {}", value, unit.short_name())64+ }65+66 /// Like `format`, but without displaying the unit.67 fn format_value(&self, unit: TimeUnit) -> String {68 self.format_with_precision(unit, unit.preferred_precision())69 }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- }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.html3 unmodified lines77 value,78 }79 }80 81-pub trait InformationQuantity {81+impl Quantity for Information {82+ type Unit = InformationUnit;83+84 fn zero() -> Information {83- Information::new::<byte>(0.)85+ Information::new::<byte>(0.0)86 }87 86- fn to_string<U>(&self, u: U) -> String87- where88- U: si::information::Unit + Conversion<f64, T = f64>;89-}88+ fn suitable_unit(&self) -> InformationUnit {89+ if *self < Information::new::<kibibyte>(1.0) {90+ InformationUnit::Byte91+ } else {92+ InformationUnit::KibiByte93+ }94+ }95 91-impl InformationQuantity for Information {92- fn to_string<U>(&self, u: U) -> String93- where94- U: si::information::Unit + Conversion<f64, T = f64>,95- {96- format!(97- "{}",98- self.into_format_args(u, uom::fmt::DisplayStyle::Abbreviation)99- )96+ /// Format the information in the given unit with the given precision.97+ fn format_with_precision(&self, u: InformationUnit, precision: usize) -> String {98+ u.format(*self, precision)99 }100+101+ /// Format the information in the given unit.102+ fn format(&self, unit: InformationUnit) -> String {103+ let value = self.format_with_precision(unit, unit.preferred_precision());104+ format!("{} {}", value, unit.short_name())105+ }106+107+ /// Format the given information. The unit will be determined automatically.108+ fn format_auto(&self) -> String {109+ let unit = self.suitable_unit();110+ let value = self.format(unit);111+ format!("{} {}", value, unit.short_name())112+ }113+114+ /// Like `format`, but without displaying the unit.115+ fn format_value(&self, unit: InformationUnit) -> String {116+ self.format_with_precision(unit, unit.preferred_precision())117+ }118 }119 120 pub trait UnsafeRawValue {121 fn unsafe_raw_value(&self) -> f64;98 unmodified lines220 assert_eq!(time.format(TimeUnit::MilliSecond), "123.4 ms");221 assert_eq!(time.format(TimeUnit::MicroSecond), "123400.0 µs");222 223 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");224+ assert_eq!(peak_memory_usage.format(InformationUnit::Byte), "8192 B");225+ assert_eq!(226+ peak_memory_usage.format(InformationUnit::KibiByte),227+ "8.0 KiB"228+ );229 }230 231 #[test]232 fn test_mean() {41 unmodified linesmodified:src/quantity/units.rs+75 added-29 removed
21 unmodified lines22 self.dispatch().format_value(value, precision)23 }24 }25 26-use std::marker::PhantomData;27-28-use crate::quantity::{hour, microsecond, millisecond, minute, second, Time};29-26 /// Supported time units27 #[derive(Debug, Clone, Copy, PartialEq, Eq)]28 pub enum TimeUnit {29 MicroSecond,2 unmodified lines32 Minute,33 Hour,34 }35 40-impl TimeUnit {41- pub fn preferred_precision(self) -> usize {36+impl IsUnit for TimeUnit {37+ type Quantity = Time;38+39+ fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Time>> {40 match self {41+ TimeUnit::MicroSecond => Box::new(TimeUnitDispatcher::<microsecond>::new()),42+ TimeUnit::MilliSecond => Box::new(TimeUnitDispatcher::<millisecond>::new()),43+ TimeUnit::Second => Box::new(TimeUnitDispatcher::<second>::new()),44+ TimeUnit::Minute => Box::new(TimeUnitDispatcher::<minute>::new()),45+ TimeUnit::Hour => Box::new(TimeUnitDispatcher::<hour>::new()),46+ }47+ }48+49+ fn preferred_precision(&self) -> usize {50+ match self {51 TimeUnit::Second => 3,52 _ => 1,53 }54 }55 }56 49-struct Dispatcher<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> {50- u: PhantomData<U>,57+/// Supported information units58+#[derive(Debug, Clone, Copy, PartialEq, Eq)]59+60+pub enum InformationUnit {61+ Byte,62+ KibiByte,63+ MebiByte,64+ GibiByte,65+ TebiByte,66 }67 53-impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> Dispatcher<U> {54- fn new() -> Self {55- Dispatcher { u: PhantomData }68+impl IsUnit for InformationUnit {69+ type Quantity = Information;70+71+ fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Information>> {72+ match self {73+ InformationUnit::Byte => Box::new(InformationUnitDispatcher::<byte>::new()),74+ InformationUnit::KibiByte => Box::new(InformationUnitDispatcher::<kibibyte>::new()),75+ InformationUnit::MebiByte => Box::new(InformationUnitDispatcher::<mebibyte>::new()),76+ InformationUnit::GibiByte => Box::new(InformationUnitDispatcher::<gibibyte>::new()),77+ InformationUnit::TebiByte => Box::new(InformationUnitDispatcher::<tebibyte>::new()),78+ }79 }80+81+ fn preferred_precision(&self) -> usize {82+ match self {83+ InformationUnit::Byte => 0,84+ _ => 1,85+ }86+ }87 }88 59-trait UnitImpl {89+pub trait UnitImpl {90+ type Quantity;91+92 fn short_name(&self) -> &'static str;61- fn format_value(&self, value: Time, precision: usize) -> String;93+ fn format_value(&self, value: Self::Quantity, precision: usize) -> String;94 }95 96+struct TimeUnitDispatcher<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> {97+ u: PhantomData<U>,98+}99+100+impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> TimeUnitDispatcher<U> {101+ fn new() -> Self {102+ TimeUnitDispatcher { u: PhantomData }103+ }104+}105+64-impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for Dispatcher<U> {106+impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for TimeUnitDispatcher<U> {107+ type Quantity = Time;108+109 fn short_name(&self) -> &'static str {110 U::abbreviation()111 }112 1 unmodified line114 format!("{value:.precision$}", value = value.get::<U>())115 }116 }117 74-impl TimeUnit {75- fn dispatch(self) -> Box<dyn UnitImpl> {76- match self {77- TimeUnit::MicroSecond => Box::new(Dispatcher::<microsecond>::new()),78- TimeUnit::MilliSecond => Box::new(Dispatcher::<millisecond>::new()),79- TimeUnit::Second => Box::new(Dispatcher::<second>::new()),80- TimeUnit::Minute => Box::new(Dispatcher::<minute>::new()),81- TimeUnit::Hour => Box::new(Dispatcher::<hour>::new()),82- }118+struct InformationUnitDispatcher<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> {119+ u: PhantomData<U>,120+}121+122+impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> InformationUnitDispatcher<U> {123+ fn new() -> Self {124+ InformationUnitDispatcher { u: PhantomData }125 }126+}127 85- /// A short abbreviation like `s`, `ms`, or `µs`.86- pub fn short_name(self) -> &'static str {87- self.dispatch().short_name()128+impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> UnitImpl129+ for InformationUnitDispatcher<U>130+{131+ type Quantity = Information;132+133+ fn short_name(&self) -> &'static str {134+ U::abbreviation()135 }136 90- /// Formats the quantity as a string in the given Unit.91- pub fn format(self, value: Time, precision: usize) -> String {92- self.dispatch().format_value(value, precision)137+ fn format_value(&self, value: Information, precision: usize) -> String {138+ format!("{value:.precision$}", value = value.get::<U>())139 }140 }141 142 /// Supported information units92 unmodified lines