Make memory units available

This commit is contained in:
David Peter authored and David Peter committed 2026-10-05 16:00:35 +02:00
1 parent 534d5683ab
commit f342e1f5de
8 files changed
+146 -80

No files matched your search

+1 -1
View File
@@ -9,7 +9,7 @@ use crate::options::{
CmdFailureAction, CommandInputPolicy, CommandOutputPolicy, Options, OutputStyleOption, Shell, CmdFailureAction, CommandInputPolicy, CommandOutputPolicy, Options, OutputStyleOption, Shell,
}; };
use crate::output::progress_bar::get_progress_bar; use crate::output::progress_bar::get_progress_bar;
use crate::quantity::{second, Information, InformationQuantity, Time, TimeQuantity}; use crate::quantity::{second, Information, Quantity, Time};
use crate::timer::execute_and_measure; use crate::timer::execute_and_measure;
use crate::util::randomized_environment_offset; use crate::util::randomized_environment_offset;
+1 -1
View File
@@ -22,7 +22,7 @@ use crate::output::progress_bar::{
}; };
use crate::output::warnings::{OutlierWarningOptions, Warnings}; use crate::output::warnings::{OutlierWarningOptions, Warnings};
use crate::parameter::ParameterNameAndValue; use crate::parameter::ParameterNameAndValue;
use crate::quantity::{self, const_time_from_seconds, Time, TimeQuantity}; use crate::quantity::{self, const_time_from_seconds, Time, Quantity};
use benchmark_result::BenchmarkResult; use benchmark_result::BenchmarkResult;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
+1 -1
View File
@@ -3,7 +3,7 @@ use std::cmp::Ordering;
use super::benchmark_result::BenchmarkResult; use super::benchmark_result::BenchmarkResult;
use crate::{ use crate::{
options::SortOrder, options::SortOrder,
quantity::{self, Ratio, Time, TimeQuantity}, quantity::{self, Ratio, Time, Quantity},
}; };
#[derive(Debug)] #[derive(Debug)]
+2 -2
View File
@@ -5,7 +5,7 @@ use csv::WriterBuilder;
use super::Exporter; use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult; use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::options::SortOrder; use crate::options::SortOrder;
use crate::quantity::{TimeQuantity, TimeUnit}; use crate::quantity::{Quantity, TimeUnit};
use anyhow::Result; use anyhow::Result;
@@ -71,7 +71,7 @@ impl Exporter for CsvExporter {
fn test_csv() { fn test_csv() {
use crate::benchmark::benchmark_result::Parameter; use crate::benchmark::benchmark_result::Parameter;
use crate::benchmark::measurement::{Measurement, Measurements}; use crate::benchmark::measurement::{Measurement, Measurements};
use crate::quantity::{byte, second, Information, Time, TimeQuantity}; use crate::quantity::{byte, second, Information, Quantity, Time};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::process::ExitStatus; use std::process::ExitStatus;
+1 -1
View File
@@ -3,7 +3,7 @@ use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::benchmark::measurement::{Measurement, Measurements}; use crate::benchmark::measurement::{Measurement, Measurements};
use crate::export::asciidoc::AsciidocExporter; use crate::export::asciidoc::AsciidocExporter;
use crate::export::orgmode::OrgmodeExporter; use crate::export::orgmode::OrgmodeExporter;
use crate::quantity::{byte, second, Information, Time, TimeQuantity, TimeUnit}; use crate::quantity::{byte, second, Information, Quantity, Time, TimeUnit};
use crate::{export::markdown::MarkdownExporter, options::SortOrder}; use crate::{export::markdown::MarkdownExporter, options::SortOrder};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::process::ExitStatus; use std::process::ExitStatus;
+1 -1
View File
@@ -1,7 +1,7 @@
use std::fmt; use std::fmt;
use crate::benchmark::MIN_EXECUTION_TIME; use crate::benchmark::MIN_EXECUTION_TIME;
use crate::quantity::{Time, TimeQuantity}; use crate::quantity::{Time, Quantity};
pub struct OutlierWarningOptions { pub struct OutlierWarningOptions {
pub warmup_in_use: bool, pub warmup_in_use: bool,
+62 -42
View File
@@ -3,38 +3,38 @@ use std::marker::PhantomData;
use serde::ser::SerializeStruct; use serde::ser::SerializeStruct;
use serde::Serializer; use serde::Serializer;
use uom::{si, Conversion};
pub use uom::si::information::{byte, kibibyte}; use uom::si;
pub use uom::si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};
pub use uom::si::ratio::ratio; pub use uom::si::ratio::ratio;
pub use uom::si::time::{hour, microsecond, millisecond, minute, nanosecond, second}; pub use uom::si::time::{hour, microsecond, millisecond, minute, nanosecond, second};
pub use si::f64::{Information, Ratio, Time}; pub use si::f64::{Information, Ratio, Time};
pub use si::f64::{Information, Ratio, Time};
pub use si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};
pub use si::ratio::ratio;
pub use si::time::{hour, microsecond, millisecond, minute, nanosecond, second};
pub use uom::num_traits::Zero;
pub use units::{InformationUnit, IsUnit, TimeUnit}; pub use units::{InformationUnit, IsUnit, TimeUnit};
mod units; mod units;
pub trait TimeQuantity { pub trait Quantity {
type Unit;
fn zero() -> Self;
fn suitable_unit(&self) -> Self::Unit;
fn format_with_precision(&self, unit: Self::Unit, precision: usize) -> String;
fn format(&self, unit: Self::Unit) -> String;
fn format_auto(&self) -> String;
fn format_value(&self, unit: Self::Unit) -> String;
}
impl Quantity for Time {
type Unit = TimeUnit;
fn zero() -> Time { fn zero() -> Time {
Time::new::<second>(0.0) Time::new::<second>(0.0)
} }
fn suitable_unit(&self) -> TimeUnit;
fn format_with_precision(&self, u: TimeUnit, precision: usize) -> String;
fn format(&self, u: TimeUnit) -> String;
fn format_auto(&self) -> String;
fn format_value(&self, time_unit: TimeUnit) -> String;
}
impl TimeQuantity for Time {
fn suitable_unit(&self) -> TimeUnit { fn suitable_unit(&self) -> TimeUnit {
if *self < Time::new::<millisecond>(1.0) { if *self < Time::new::<millisecond>(1.0) {
TimeUnit::MicroSecond TimeUnit::MicroSecond
@@ -56,17 +56,17 @@ impl TimeQuantity for Time {
format!("{} {}", value, unit.short_name()) format!("{} {}", value, unit.short_name())
} }
/// Format the given time duration. The unit will be determined automatically.
fn format_auto(&self) -> String {
let unit = self.suitable_unit();
let value = self.format(unit);
format!("{} {}", value, unit.short_name())
}
/// Like `format`, but without displaying the unit. /// Like `format`, but without displaying the unit.
fn format_value(&self, unit: TimeUnit) -> String { fn format_value(&self, unit: TimeUnit) -> String {
self.format_with_precision(unit, unit.preferred_precision()) self.format_with_precision(unit, unit.preferred_precision())
} }
/// Format the given time duration. The unit will be determined automatically.
fn format_auto(&self) -> String {
let time_unit = self.suitable_unit();
let value = self.format(time_unit);
format!("{} {}", value, time_unit.short_name())
}
} }
pub const fn const_time_from_seconds(value: f64) -> Time { pub const fn const_time_from_seconds(value: f64) -> Time {
@@ -78,25 +78,42 @@ pub const fn const_time_from_seconds(value: f64) -> Time {
} }
} }
pub trait InformationQuantity { impl Quantity for Information {
type Unit = InformationUnit;
fn zero() -> Information { fn zero() -> Information {
Information::new::<byte>(0.) Information::new::<byte>(0.0)
} }
fn to_string<U>(&self, u: U) -> String fn suitable_unit(&self) -> InformationUnit {
where if *self < Information::new::<kibibyte>(1.0) {
U: si::information::Unit + Conversion<f64, T = f64>; InformationUnit::Byte
} } else {
InformationUnit::KibiByte
}
}
impl InformationQuantity for Information { /// Format the information in the given unit with the given precision.
fn to_string<U>(&self, u: U) -> String fn format_with_precision(&self, u: InformationUnit, precision: usize) -> String {
where u.format(*self, precision)
U: si::information::Unit + Conversion<f64, T = f64>, }
{
format!( /// Format the information in the given unit.
"{}", fn format(&self, unit: InformationUnit) -> String {
self.into_format_args(u, uom::fmt::DisplayStyle::Abbreviation) let value = self.format_with_precision(unit, unit.preferred_precision());
) format!("{} {}", value, unit.short_name())
}
/// Format the given information. The unit will be determined automatically.
fn format_auto(&self) -> String {
let unit = self.suitable_unit();
let value = self.format(unit);
format!("{} {}", value, unit.short_name())
}
/// Like `format`, but without displaying the unit.
fn format_value(&self, unit: InformationUnit) -> String {
self.format_with_precision(unit, unit.preferred_precision())
} }
} }
@@ -204,8 +221,11 @@ fn test_format() {
assert_eq!(time.format(TimeUnit::MicroSecond), "123400.0 µs"); assert_eq!(time.format(TimeUnit::MicroSecond), "123400.0 µs");
let peak_memory_usage = Information::new::<kibibyte>(8.); let peak_memory_usage = Information::new::<kibibyte>(8.);
assert_eq!(peak_memory_usage.to_string(byte), "8192 B"); assert_eq!(peak_memory_usage.format(InformationUnit::Byte), "8192 B");
assert_eq!(peak_memory_usage.to_string(kibibyte), "8 KiB"); assert_eq!(
peak_memory_usage.format(InformationUnit::KibiByte),
"8.0 KiB"
);
} }
#[test] #[test]
+77 -31
View File
@@ -23,10 +23,6 @@ pub trait IsUnit {
} }
} }
use std::marker::PhantomData;
use crate::quantity::{hour, microsecond, millisecond, minute, second, Time};
/// Supported time units /// Supported time units
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimeUnit { pub enum TimeUnit {
@@ -37,8 +33,20 @@ pub enum TimeUnit {
Hour, Hour,
} }
impl TimeUnit { impl IsUnit for TimeUnit {
pub fn preferred_precision(self) -> usize { type Quantity = Time;
fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Time>> {
match self {
TimeUnit::MicroSecond => Box::new(TimeUnitDispatcher::<microsecond>::new()),
TimeUnit::MilliSecond => Box::new(TimeUnitDispatcher::<millisecond>::new()),
TimeUnit::Second => Box::new(TimeUnitDispatcher::<second>::new()),
TimeUnit::Minute => Box::new(TimeUnitDispatcher::<minute>::new()),
TimeUnit::Hour => Box::new(TimeUnitDispatcher::<hour>::new()),
}
}
fn preferred_precision(&self) -> usize {
match self { match self {
TimeUnit::Second => 3, TimeUnit::Second => 3,
_ => 1, _ => 1,
@@ -46,22 +54,58 @@ impl TimeUnit {
} }
} }
struct Dispatcher<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> { /// Supported information units
u: PhantomData<U>, #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InformationUnit {
Byte,
KibiByte,
MebiByte,
GibiByte,
TebiByte,
} }
impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> Dispatcher<U> { impl IsUnit for InformationUnit {
fn new() -> Self { type Quantity = Information;
Dispatcher { u: PhantomData }
fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Information>> {
match self {
InformationUnit::Byte => Box::new(InformationUnitDispatcher::<byte>::new()),
InformationUnit::KibiByte => Box::new(InformationUnitDispatcher::<kibibyte>::new()),
InformationUnit::MebiByte => Box::new(InformationUnitDispatcher::<mebibyte>::new()),
InformationUnit::GibiByte => Box::new(InformationUnitDispatcher::<gibibyte>::new()),
InformationUnit::TebiByte => Box::new(InformationUnitDispatcher::<tebibyte>::new()),
}
}
fn preferred_precision(&self) -> usize {
match self {
InformationUnit::Byte => 0,
_ => 1,
}
} }
} }
trait UnitImpl { pub trait UnitImpl {
type Quantity;
fn short_name(&self) -> &'static str; fn short_name(&self) -> &'static str;
fn format_value(&self, value: Time, precision: usize) -> String; fn format_value(&self, value: Self::Quantity, precision: usize) -> String;
} }
impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for Dispatcher<U> { struct TimeUnitDispatcher<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> {
u: PhantomData<U>,
}
impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> TimeUnitDispatcher<U> {
fn new() -> Self {
TimeUnitDispatcher { u: PhantomData }
}
}
impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for TimeUnitDispatcher<U> {
type Quantity = Time;
fn short_name(&self) -> &'static str { fn short_name(&self) -> &'static str {
U::abbreviation() U::abbreviation()
} }
@@ -71,25 +115,27 @@ impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for Dispat
} }
} }
impl TimeUnit { struct InformationUnitDispatcher<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> {
fn dispatch(self) -> Box<dyn UnitImpl> { u: PhantomData<U>,
match self { }
TimeUnit::MicroSecond => Box::new(Dispatcher::<microsecond>::new()),
TimeUnit::MilliSecond => Box::new(Dispatcher::<millisecond>::new()), impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> InformationUnitDispatcher<U> {
TimeUnit::Second => Box::new(Dispatcher::<second>::new()), fn new() -> Self {
TimeUnit::Minute => Box::new(Dispatcher::<minute>::new()), InformationUnitDispatcher { u: PhantomData }
TimeUnit::Hour => Box::new(Dispatcher::<hour>::new()), }
} }
impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> UnitImpl
for InformationUnitDispatcher<U>
{
type Quantity = Information;
fn short_name(&self) -> &'static str {
U::abbreviation()
} }
/// A short abbreviation like `s`, `ms`, or `µs`. fn format_value(&self, value: Information, precision: usize) -> String {
pub fn short_name(self) -> &'static str { format!("{value:.precision$}", value = value.get::<U>())
self.dispatch().short_name()
}
/// Formats the quantity as a string in the given Unit.
pub fn format(self, value: Time, precision: usize) -> String {
self.dispatch().format_value(value, precision)
} }
} }