mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-10-12 02:22:35 +00:00
Make memory units available
This commit is contained in:
@@ -9,7 +9,7 @@ use crate::options::{
|
||||
CmdFailureAction, CommandInputPolicy, CommandOutputPolicy, Options, OutputStyleOption, Shell,
|
||||
};
|
||||
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::util::randomized_environment_offset;
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ use crate::output::progress_bar::{
|
||||
};
|
||||
use crate::output::warnings::{OutlierWarningOptions, Warnings};
|
||||
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 anyhow::{anyhow, Result};
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::cmp::Ordering;
|
||||
use super::benchmark_result::BenchmarkResult;
|
||||
use crate::{
|
||||
options::SortOrder,
|
||||
quantity::{self, Ratio, Time, TimeQuantity},
|
||||
quantity::{self, Ratio, Time, Quantity},
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@ use csv::WriterBuilder;
|
||||
use super::Exporter;
|
||||
use crate::benchmark::benchmark_result::BenchmarkResult;
|
||||
use crate::options::SortOrder;
|
||||
use crate::quantity::{TimeQuantity, TimeUnit};
|
||||
use crate::quantity::{Quantity, TimeUnit};
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
@@ -71,7 +71,7 @@ impl Exporter for CsvExporter {
|
||||
fn test_csv() {
|
||||
use crate::benchmark::benchmark_result::Parameter;
|
||||
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::process::ExitStatus;
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ use crate::benchmark::benchmark_result::BenchmarkResult;
|
||||
use crate::benchmark::measurement::{Measurement, Measurements};
|
||||
use crate::export::asciidoc::AsciidocExporter;
|
||||
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 std::collections::BTreeMap;
|
||||
use std::process::ExitStatus;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::fmt;
|
||||
|
||||
use crate::benchmark::MIN_EXECUTION_TIME;
|
||||
use crate::quantity::{Time, TimeQuantity};
|
||||
use crate::quantity::{Time, Quantity};
|
||||
|
||||
pub struct OutlierWarningOptions {
|
||||
pub warmup_in_use: bool,
|
||||
|
||||
+62
-42
@@ -3,38 +3,38 @@ use std::marker::PhantomData;
|
||||
|
||||
use serde::ser::SerializeStruct;
|
||||
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::time::{hour, microsecond, millisecond, minute, nanosecond, second};
|
||||
|
||||
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};
|
||||
|
||||
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 {
|
||||
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 {
|
||||
if *self < Time::new::<millisecond>(1.0) {
|
||||
TimeUnit::MicroSecond
|
||||
@@ -56,17 +56,17 @@ impl TimeQuantity for Time {
|
||||
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.
|
||||
fn format_value(&self, unit: TimeUnit) -> String {
|
||||
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 {
|
||||
@@ -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 {
|
||||
Information::new::<byte>(0.)
|
||||
Information::new::<byte>(0.0)
|
||||
}
|
||||
|
||||
fn to_string<U>(&self, u: U) -> String
|
||||
where
|
||||
U: si::information::Unit + Conversion<f64, T = f64>;
|
||||
}
|
||||
fn suitable_unit(&self) -> InformationUnit {
|
||||
if *self < Information::new::<kibibyte>(1.0) {
|
||||
InformationUnit::Byte
|
||||
} else {
|
||||
InformationUnit::KibiByte
|
||||
}
|
||||
}
|
||||
|
||||
impl InformationQuantity for Information {
|
||||
fn to_string<U>(&self, u: U) -> String
|
||||
where
|
||||
U: si::information::Unit + Conversion<f64, T = f64>,
|
||||
{
|
||||
format!(
|
||||
"{}",
|
||||
self.into_format_args(u, uom::fmt::DisplayStyle::Abbreviation)
|
||||
)
|
||||
/// Format the information in the given unit with the given precision.
|
||||
fn format_with_precision(&self, u: InformationUnit, precision: usize) -> String {
|
||||
u.format(*self, precision)
|
||||
}
|
||||
|
||||
/// Format the information in the given unit.
|
||||
fn format(&self, unit: InformationUnit) -> String {
|
||||
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");
|
||||
|
||||
let peak_memory_usage = Information::new::<kibibyte>(8.);
|
||||
assert_eq!(peak_memory_usage.to_string(byte), "8192 B");
|
||||
assert_eq!(peak_memory_usage.to_string(kibibyte), "8 KiB");
|
||||
assert_eq!(peak_memory_usage.format(InformationUnit::Byte), "8192 B");
|
||||
assert_eq!(
|
||||
peak_memory_usage.format(InformationUnit::KibiByte),
|
||||
"8.0 KiB"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+77
-31
@@ -23,10 +23,6 @@ pub trait IsUnit {
|
||||
}
|
||||
}
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::quantity::{hour, microsecond, millisecond, minute, second, Time};
|
||||
|
||||
/// Supported time units
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TimeUnit {
|
||||
@@ -37,8 +33,20 @@ pub enum TimeUnit {
|
||||
Hour,
|
||||
}
|
||||
|
||||
impl TimeUnit {
|
||||
pub fn preferred_precision(self) -> usize {
|
||||
impl IsUnit for TimeUnit {
|
||||
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 {
|
||||
TimeUnit::Second => 3,
|
||||
_ => 1,
|
||||
@@ -46,22 +54,58 @@ impl TimeUnit {
|
||||
}
|
||||
}
|
||||
|
||||
struct Dispatcher<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> {
|
||||
u: PhantomData<U>,
|
||||
/// Supported information units
|
||||
#[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> {
|
||||
fn new() -> Self {
|
||||
Dispatcher { u: PhantomData }
|
||||
impl IsUnit for InformationUnit {
|
||||
type Quantity = Information;
|
||||
|
||||
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 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 {
|
||||
U::abbreviation()
|
||||
}
|
||||
@@ -71,25 +115,27 @@ impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for Dispat
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeUnit {
|
||||
fn dispatch(self) -> Box<dyn UnitImpl> {
|
||||
match self {
|
||||
TimeUnit::MicroSecond => Box::new(Dispatcher::<microsecond>::new()),
|
||||
TimeUnit::MilliSecond => Box::new(Dispatcher::<millisecond>::new()),
|
||||
TimeUnit::Second => Box::new(Dispatcher::<second>::new()),
|
||||
TimeUnit::Minute => Box::new(Dispatcher::<minute>::new()),
|
||||
TimeUnit::Hour => Box::new(Dispatcher::<hour>::new()),
|
||||
}
|
||||
struct InformationUnitDispatcher<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> {
|
||||
u: PhantomData<U>,
|
||||
}
|
||||
|
||||
impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> InformationUnitDispatcher<U> {
|
||||
fn new() -> Self {
|
||||
InformationUnitDispatcher { u: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
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`.
|
||||
pub fn short_name(self) -> &'static str {
|
||||
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)
|
||||
fn format_value(&self, value: Information, precision: usize) -> String {
|
||||
format!("{value:.precision$}", value = value.get::<U>())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user