mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-01 17:00:18 +00:00
Move quantity module
This commit is contained in:
1 parent
e006105e2f
commit
be66a6668f
13 files changed
+19
-217
No files matched your search
@@ -2,7 +2,8 @@ use std::collections::BTreeMap;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::benchmark::{measurement::Measurements, quantity::Time};
|
||||
use crate::benchmark::measurement::Measurements;
|
||||
use crate::quantity::Time;
|
||||
|
||||
/// Parameter value and whether it was used in the command line template
|
||||
#[derive(Debug, Default, Clone, Serialize, PartialEq)]
|
||||
|
||||
@@ -4,14 +4,14 @@ use std::process::ExitStatus;
|
||||
|
||||
use crate::benchmark::measurement::Measurement;
|
||||
use crate::benchmark::measurement::Measurements;
|
||||
use crate::benchmark::quantity::Information;
|
||||
use crate::benchmark::quantity::InformationQuantity;
|
||||
use crate::benchmark::quantity::{Time, TimeQuantity};
|
||||
use crate::command::Command;
|
||||
use crate::options::{
|
||||
CmdFailureAction, CommandInputPolicy, CommandOutputPolicy, Options, OutputStyleOption, Shell,
|
||||
};
|
||||
use crate::output::progress_bar::get_progress_bar;
|
||||
use crate::quantity::Information;
|
||||
use crate::quantity::InformationQuantity;
|
||||
use crate::quantity::{Time, TimeQuantity};
|
||||
use crate::timer::execute_and_measure;
|
||||
use crate::util::randomized_environment_offset;
|
||||
|
||||
|
||||
@@ -2,11 +2,11 @@ use std::process::ExitStatus;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::benchmark::quantity::{
|
||||
use crate::outlier_detection::modified_zscores;
|
||||
use crate::quantity::{
|
||||
max, mean, median, min, second, serialize_information, serialize_time, standard_deviation,
|
||||
Information, Time, TimeQuantity,
|
||||
};
|
||||
use crate::outlier_detection::modified_zscores;
|
||||
use crate::util::exit_code::extract_exit_code;
|
||||
|
||||
fn serialize_exit_status<S>(exit_status: &ExitStatus, serializer: S) -> Result<S::Ok, S::Error>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
pub mod benchmark_result;
|
||||
pub mod executor;
|
||||
pub mod measurement;
|
||||
pub mod quantity;
|
||||
pub mod relative_speed;
|
||||
pub mod scheduler;
|
||||
|
||||
@@ -12,7 +11,6 @@ use std::time::Instant;
|
||||
use crate::benchmark::benchmark_result::Parameter;
|
||||
use crate::benchmark::executor::BenchmarkIteration;
|
||||
use crate::benchmark::measurement::{Measurement, Measurements};
|
||||
use crate::benchmark::quantity::{const_time_from_seconds, Time, TimeQuantity};
|
||||
use crate::command::Command;
|
||||
use crate::options::{
|
||||
CmdFailureAction, CommandOutputPolicy, ExecutorKind, Options, OutputStyleOption,
|
||||
@@ -25,6 +23,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 benchmark_result::BenchmarkResult;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
@@ -1,198 +0,0 @@
|
||||
use core::f64;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use serde::ser::SerializeStruct;
|
||||
use serde::Serializer;
|
||||
use uom::{si, Conversion};
|
||||
|
||||
pub use uom::si::information::{byte, kibibyte};
|
||||
pub use uom::si::ratio::ratio;
|
||||
pub use uom::si::time::{microsecond, millisecond, nanosecond, second};
|
||||
|
||||
pub use si::f64::Ratio;
|
||||
pub use si::f64::Time;
|
||||
pub use si::u64::Information;
|
||||
|
||||
pub trait TimeQuantity {
|
||||
fn zero() -> Time {
|
||||
Time::new::<second>(0.0)
|
||||
}
|
||||
|
||||
fn from_seconds(value: f64) -> Time {
|
||||
Time::new::<second>(value)
|
||||
}
|
||||
|
||||
fn from_milliseconds(value: f64) -> Time {
|
||||
Time::new::<millisecond>(value)
|
||||
}
|
||||
|
||||
fn from_microseconds(value: f64) -> Time {
|
||||
Time::new::<microsecond>(value)
|
||||
}
|
||||
|
||||
fn from_nanoseconds(value: f64) -> Time {
|
||||
Time::new::<nanosecond>(value)
|
||||
}
|
||||
|
||||
// fn square(&self) -> Time;
|
||||
|
||||
fn value_in<U>(&self) -> f64
|
||||
where
|
||||
U: si::time::Unit + Conversion<f64, T = f64>;
|
||||
|
||||
fn to_string<U>(&self, u: U) -> String
|
||||
where
|
||||
U: si::time::Unit + Conversion<f64, T = f64>;
|
||||
}
|
||||
|
||||
impl TimeQuantity for Time {
|
||||
fn value_in<U>(&self) -> f64
|
||||
where
|
||||
U: si::time::Unit + Conversion<f64, T = f64>,
|
||||
{
|
||||
self.get::<U>()
|
||||
}
|
||||
|
||||
fn to_string<U>(&self, u: U) -> String
|
||||
where
|
||||
U: si::time::Unit + Conversion<f64, T = f64>,
|
||||
{
|
||||
format!(
|
||||
"{:.3}",
|
||||
self.into_format_args(u, uom::fmt::DisplayStyle::Abbreviation)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn const_time_from_seconds(value: f64) -> Time {
|
||||
// Quantity::new in uom is not yet const: https://docs.rs/uom/0.36.0/uom/si/struct.Quantity.html
|
||||
Time {
|
||||
dimension: PhantomData,
|
||||
units: PhantomData,
|
||||
value,
|
||||
}
|
||||
}
|
||||
|
||||
pub trait InformationQuantity {
|
||||
fn zero() -> Information {
|
||||
Information::new::<byte>(0)
|
||||
}
|
||||
|
||||
fn from_bytes(value: u64) -> Information {
|
||||
Information::new::<byte>(value)
|
||||
}
|
||||
|
||||
fn from_kibibytes(value: u64) -> Information {
|
||||
Information::new::<kibibyte>(value)
|
||||
}
|
||||
|
||||
fn value_in<U>(&self) -> u64
|
||||
where
|
||||
U: si::information::Unit + Conversion<u64, T = uom::num_rational::Ratio<u64>>;
|
||||
|
||||
fn to_string<U>(&self, u: U) -> String
|
||||
where
|
||||
U: si::information::Unit + Conversion<u64, T = uom::num_rational::Ratio<u64>>;
|
||||
}
|
||||
|
||||
impl InformationQuantity for Information {
|
||||
fn value_in<U>(&self) -> u64
|
||||
where
|
||||
U: si::information::Unit + Conversion<u64, T = uom::num_rational::Ratio<u64>>,
|
||||
{
|
||||
self.get::<U>()
|
||||
}
|
||||
|
||||
fn to_string<U>(&self, u: U) -> String
|
||||
where
|
||||
U: si::information::Unit + Conversion<u64, T = uom::num_rational::Ratio<u64>>,
|
||||
{
|
||||
format!(
|
||||
"{}",
|
||||
self.into_format_args(u, uom::fmt::DisplayStyle::Abbreviation)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialize_time<S>(t: &Time, s: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut state = s.serialize_struct("Time", 3)?;
|
||||
state.serialize_field("value", &t.value)?;
|
||||
state.serialize_field("unit", "second")?;
|
||||
state.end()
|
||||
}
|
||||
|
||||
pub fn serialize_information<S>(i: &Information, s: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut state = s.serialize_struct("Information", 3)?;
|
||||
state.serialize_field("value", &i.value)?;
|
||||
state.serialize_field("unit", "byte")?;
|
||||
state.end()
|
||||
}
|
||||
|
||||
macro_rules! quantity_fn {
|
||||
($name:ident, $unwrapped_values:ident, $body:expr) => {
|
||||
pub fn $name(values: &[Time]) -> Time {
|
||||
let $unwrapped_values: Vec<_> = values.iter().map(|q| q.value_in::<second>()).collect();
|
||||
let result_value = $body;
|
||||
|
||||
Time::new::<second>(result_value)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
quantity_fn!(mean, values, statistical::mean(&values));
|
||||
quantity_fn!(median, values, statistical::median(&values));
|
||||
quantity_fn!(min, values, crate::util::min_max::min(&values));
|
||||
quantity_fn!(max, values, crate::util::min_max::max(&values));
|
||||
quantity_fn!(standard_deviation, values, {
|
||||
let mean_value = statistical::mean(&values);
|
||||
statistical::standard_deviation(&values, Some(mean_value))
|
||||
});
|
||||
|
||||
#[test]
|
||||
fn test_time() {
|
||||
let time = Time::from_milliseconds(123.4);
|
||||
assert_eq!(time.value_in::<millisecond>(), 123.4);
|
||||
|
||||
let time_s = time.value_in::<second>();
|
||||
approx::assert_relative_eq!(time_s, 0.1234);
|
||||
|
||||
let time_us = time.value_in::<microsecond>();
|
||||
approx::assert_relative_eq!(time_us, 123400.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_information() {
|
||||
let information = Information::from_kibibytes(8);
|
||||
assert_eq!(information.value_in::<byte>(), 8192);
|
||||
|
||||
let information_kib = information.value_in::<kibibyte>();
|
||||
assert_eq!(information_kib, 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format() {
|
||||
let time = Time::from_milliseconds(123.4);
|
||||
assert_eq!(time.to_string(second), "0.123 s");
|
||||
assert_eq!(time.to_string(millisecond), "123.400 ms");
|
||||
assert_eq!(time.to_string(microsecond), "123400.000 µs");
|
||||
|
||||
let peak_memory_usage = Information::from_kibibytes(8);
|
||||
assert_eq!(peak_memory_usage.to_string(byte), "8192 B");
|
||||
assert_eq!(peak_memory_usage.to_string(kibibyte), "8 KiB");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mean() {
|
||||
let values = vec![
|
||||
Time::from_milliseconds(123.4),
|
||||
Time::from_milliseconds(234.5),
|
||||
];
|
||||
let result = mean(&values);
|
||||
assert_eq!(result.to_string(millisecond), "178.950 ms");
|
||||
}
|
||||
@@ -2,8 +2,8 @@ use std::cmp::Ordering;
|
||||
|
||||
use super::benchmark_result::BenchmarkResult;
|
||||
use crate::{
|
||||
benchmark::quantity::{self, Ratio, Time, TimeQuantity},
|
||||
options::SortOrder,
|
||||
quantity::{self, Ratio, Time, TimeQuantity},
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
|
||||
Reference in new issue
Block a user