Skip to content
Myr

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

8 files+143 added−77 removed

modified:src/benchmark/executor.rs+1 added-1 removed
7 unmodified lines
8use crate::options::{9 CmdFailureAction, CommandInputPolicy, CommandOutputPolicy, Options, OutputStyleOption, Shell,10};11use crate::output::progress_bar::get_progress_bar;12use crate::quantity::{second, Information, InformationQuantity, Time, TimeQuantity};12use crate::quantity::{second, Information, Quantity, Time};13use crate::timer::execute_and_measure;14use crate::util::randomized_environment_offset;1516use anyhow::{bail, Context, Result};
358 unmodified lines
modified:src/benchmark/mod.rs+1 added-1 removed
20 unmodified lines
21 finish_initial_measurement, get_progress_bar, start_initial_measurement,22};23use crate::output::warnings::{OutlierWarningOptions, Warnings};24use crate::parameter::ParameterNameAndValue;25use crate::quantity::{self, const_time_from_seconds, Time, TimeQuantity};25use crate::quantity::{self, const_time_from_seconds, Time, Quantity};26use benchmark_result::BenchmarkResult;2728use anyhow::{anyhow, Result};29use colored::*;
463 unmodified lines
modified:src/benchmark/relative_speed.rs+1 added-1 removed
1 unmodified line
23use super::benchmark_result::BenchmarkResult;4use crate::{5 options::SortOrder,6 quantity::{self, Ratio, Time, TimeQuantity},6 quantity::{self, Ratio, Time, Quantity},7};89#[derive(Debug)]10pub struct BenchmarkResultWithRelativeSpeed<'a> {
225 unmodified lines
modified:src/export/csv.rs+2 added-2 removed
3 unmodified lines
45use super::Exporter;6use crate::benchmark::benchmark_result::BenchmarkResult;7use crate::options::SortOrder;8use crate::quantity::{TimeQuantity, TimeUnit};8use crate::quantity::{Quantity, TimeUnit};910use anyhow::Result;1112#[derive(Default)]
57 unmodified lines
70#[test]71fn 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};7576 use std::collections::BTreeMap;77 use std::process::ExitStatus;78
106 unmodified lines
modified:src/export/tests.rs+1 added-1 removed
1 unmodified line
2use crate::benchmark::benchmark_result::BenchmarkResult;3use crate::benchmark::measurement::{Measurement, Measurements};4use crate::export::asciidoc::AsciidocExporter;5use crate::export::orgmode::OrgmodeExporter;6use crate::quantity::{byte, second, Information, Time, TimeQuantity, TimeUnit};6use crate::quantity::{byte, second, Information, Quantity, Time, TimeUnit};7use crate::{export::markdown::MarkdownExporter, options::SortOrder};8use std::collections::BTreeMap;9use std::process::ExitStatus;10
407 unmodified lines
modified:src/output/warnings.rs+1 added-1 removed
1use std::fmt;23use crate::benchmark::MIN_EXECUTION_TIME;4use crate::quantity::{Time, TimeQuantity};4use crate::quantity::{Time, Quantity};56pub struct OutlierWarningOptions {7 pub warmup_in_use: bool,8 pub prepare_in_use: bool,
60 unmodified lines
modified:src/quantity/mod.rs+61 added-41 removed
1 unmodified line
2use std::marker::PhantomData;34use serde::ser::SerializeStruct;5use serde::Serializer;6use uom::{si, Conversion};67use uom::si;8pub use uom::si::information::{byte, kibibyte};8pub use uom::si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};9pub use uom::si::ratio::ratio;10pub use uom::si::time::{hour, microsecond, millisecond, minute, nanosecond, second};1112pub use si::f64::{Information, Ratio, Time};1314pub use si::f64::{Information, Ratio, Time};15pub use si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};16pub use si::ratio::ratio;17pub use si::time::{hour, microsecond, millisecond, minute, nanosecond, second};18pub use uom::num_traits::Zero;1914pub use units::{InformationUnit, IsUnit, TimeUnit};1516mod units;1724pub trait TimeQuantity {25 fn zero() -> Time {26 Time::new::<second>(0.0)27 }18pub trait Quantity {19 type Unit;2029 fn suitable_unit(&self) -> TimeUnit;21 fn zero() -> Self;2223 fn suitable_unit(&self) -> Self::Unit;2431 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}3037impl TimeQuantity for Time {31impl Quantity for Time {32 type Unit = TimeUnit;3334 fn zero() -> Time {35 Time::new::<second>(0.0)36 }3738 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 lines
55 let value = self.format_with_precision(unit, unit.preferred_precision());56 format!("{} {}", value, unit.short_name())57 }5859 /// 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 }6566 /// 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 }6364 /// 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}7172pub 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.html
3 unmodified lines
77 value,78 }79}8081pub trait InformationQuantity {81impl Quantity for Information {82 type Unit = InformationUnit;8384 fn zero() -> Information {83 Information::new::<byte>(0.)85 Information::new::<byte>(0.0)86 }8786 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 }9591impl 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 }100101 /// 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 }106107 /// 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 }113114 /// 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}119120pub trait UnsafeRawValue {121 fn unsafe_raw_value(&self) -> f64;
98 unmodified lines
220 assert_eq!(time.format(TimeUnit::MilliSecond), "123.4 ms");221 assert_eq!(time.format(TimeUnit::MicroSecond), "123400.0 µs");222223 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}230231#[test]232fn test_mean() {
41 unmodified lines
modified:src/quantity/units.rs+75 added-29 removed
21 unmodified lines
22 self.dispatch().format_value(value, precision)23 }24}2526use std::marker::PhantomData;2728use crate::quantity::{hour, microsecond, millisecond, minute, second, Time};2926/// Supported time units27#[derive(Debug, Clone, Copy, PartialEq, Eq)]28pub enum TimeUnit {29 MicroSecond,
2 unmodified lines
32 Minute,33 Hour,34}3540impl TimeUnit {41 pub fn preferred_precision(self) -> usize {36impl IsUnit for TimeUnit {37 type Quantity = Time;3839 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 }4849 fn preferred_precision(&self) -> usize {50 match self {51 TimeUnit::Second => 3,52 _ => 1,53 }54 }55}5649struct 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)]5960pub enum InformationUnit {61 Byte,62 KibiByte,63 MebiByte,64 GibiByte,65 TebiByte,66}6753impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> Dispatcher<U> {54 fn new() -> Self {55 Dispatcher { u: PhantomData }68impl IsUnit for InformationUnit {69 type Quantity = Information;7071 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 }8081 fn preferred_precision(&self) -> usize {82 match self {83 InformationUnit::Byte => 0,84 _ => 1,85 }86 }87}8859trait UnitImpl {89pub trait UnitImpl {90 type Quantity;9192 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}9596struct TimeUnitDispatcher<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> {97 u: PhantomData<U>,98}99100impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> TimeUnitDispatcher<U> {101 fn new() -> Self {102 TimeUnitDispatcher { u: PhantomData }103 }104}10564impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for Dispatcher<U> {106impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for TimeUnitDispatcher<U> {107 type Quantity = Time;108109 fn short_name(&self) -> &'static str {110 U::abbreviation()111 }112
1 unmodified line
114 format!("{value:.precision$}", value = value.get::<U>())115 }116}11774impl 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 }118struct InformationUnitDispatcher<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> {119 u: PhantomData<U>,120}121122impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> InformationUnitDispatcher<U> {123 fn new() -> Self {124 InformationUnitDispatcher { u: PhantomData }125 }126}12785 /// A short abbreviation like `s`, `ms`, or `µs`.86 pub fn short_name(self) -> &'static str {87 self.dispatch().short_name()128impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> UnitImpl129 for InformationUnitDispatcher<U>130{131 type Quantity = Information;132133 fn short_name(&self) -> &'static str {134 U::abbreviation()135 }13690 /// 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}141142/// Supported information units
92 unmodified lines