Cleanup
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 3a2c8ab · Browse files · 8 files·+59 added−46 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+59 added−46 removed
- other:+59 added−46 removed
Provenance
- Commit
9c2ee56- Tree
ee1a9f5- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
David Peter <mail@david-peter.de>- Parent
3a2c8ab
8 files changed+59 added−46 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, Quantity, Time};12+use crate::quantity::{second, Information, Time, Zero};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/measurement.rs+1 added-1 removed
84 unmodified lines85 }86 87 /// The median wall clock time88 pub fn median(&self) -> Time {89- median(&self.wall_clock_times().collect::<Vec<_>>())89+ median(self.wall_clock_times())90 }91 92 /// The minimum wall clock time93 pub fn min(&self) -> Time {31 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, Quantity, Time};25+use crate::quantity::{self, const_time_from_seconds, FormatQuantity, Time, Zero};26 use benchmark_result::BenchmarkResult;27 28 use anyhow::{anyhow, Result};29 use colored::*;462 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, Quantity},6+ quantity::{self, Ratio, Time, Zero},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::{Quantity, TimeUnit};8+use crate::quantity::{FormatQuantity, 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, Quantity, Time};74+ use crate::quantity::{byte, second, Information, Time, Zero};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, Quantity, Time, TimeUnit};6+use crate::quantity::{byte, second, Information, Time, TimeUnit, Zero};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, Quantity};4+use crate::quantity::{FormatQuantity, Time};5 6 pub struct OutlierWarningOptions {7 pub warmup_in_use: bool,8 pub prepare_in_use: bool,60 unmodified linesmodified:src/quantity/mod.rs+51 added-38 removed
1 use core::f64;2 use std::marker::PhantomData;3+use std::ops::Add;4 use std::ops::AddAssign;5 use std::ops::Div;6 7 use serde::ser::SerializeStruct;8 use serde::Serializer;9 10 use uom::num_traits;11 use uom::si;11-pub use uom::si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};12-pub use uom::si::ratio::ratio;13-pub use uom::si::time::{hour, microsecond, millisecond, minute, nanosecond, second};12 13 pub use si::f64::{Information, Ratio, Time};14+pub use si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};15+pub use si::ratio::ratio;16+pub use si::time::{hour, microsecond, millisecond, minute, nanosecond, second};17+pub use uom::num_traits::Zero;18 19 pub use units::{InformationUnit, IsUnit, TimeUnit};20 21 mod units;22 21-pub trait Quantity {23+pub trait FormatQuantity {24 type Unit;25 24- fn zero() -> Self;25-26 fn suitable_unit(&self) -> Self::Unit;27 28 fn format_with_precision(&self, unit: Self::Unit, precision: usize) -> String;29 fn format(&self, unit: Self::Unit) -> String;30 fn format_auto(&self) -> String;31 fn format_value(&self, unit: Self::Unit) -> String;32 }33 34-impl Quantity for Time {34+impl FormatQuantity for Time {35 type Unit = TimeUnit;36 37- fn zero() -> Time {38- Time::new::<second>(0.0)39- }40-37 fn suitable_unit(&self) -> TimeUnit {38 if *self < Time::new::<millisecond>(1.0) {39 TimeUnit::MicroSecond40 } else if *self < Time::new::<second>(1.0) {35 unmodified lines76 value,77 }78 }79 84-impl Quantity for Information {80+impl FormatQuantity for Information {81 type Unit = InformationUnit;82 87- fn zero() -> Information {88- Information::new::<byte>(0.0)89- }90-83 fn suitable_unit(&self) -> InformationUnit {84 if *self < Information::new::<kibibyte>(1.0) {85 InformationUnit::Byte86 } else {24 unmodified lines111 self.format_with_precision(unit, unit.preferred_precision())112 }113 }114 115+pub fn serialize_time<S>(t: &Time, s: S) -> Result<S::Ok, S::Error>116+where117+ S: Serializer,118+{119+ let mut state = s.serialize_struct("Time", 3)?;120+ state.serialize_field("value", &t.get::<second>())?;121+ state.serialize_field("unit", "second")?;122+ state.end()123+}124+125+pub fn serialize_information<S>(i: &Information, s: S) -> Result<S::Ok, S::Error>126+where127+ S: Serializer,128+{129+ let mut state = s.serialize_struct("Information", 3)?;130+ state.serialize_field("value", &i.get::<byte>())?;131+ state.serialize_field("unit", "byte")?;132+ state.end()133+}134+135 pub trait UnsafeRawValue {136 fn unsafe_raw_value(&self) -> f64;137 fn unsafe_from_raw_value(value: f64) -> Self;138 }17 unmodified lines156 Information::new::<byte>(value)157 }158 }159 148-pub fn serialize_time<S>(t: &Time, s: S) -> Result<S::Ok, S::Error>149-where150- S: Serializer,151-{152- let mut state = s.serialize_struct("Time", 3)?;153- state.serialize_field("value", &t.value)?;154- state.serialize_field("unit", "second")?;155- state.end()156-}157-158-pub fn serialize_information<S>(i: &Information, s: S) -> Result<S::Ok, S::Error>159-where160- S: Serializer,161-{162- let mut state = s.serialize_struct("Information", 3)?;163- state.serialize_field("value", &i.value)?;164- state.serialize_field("unit", "byte")?;165- state.end()166-}167-160 macro_rules! quantity_fn {161 ($name:ident, $unwrapped_values:ident, $body:expr) => {162 pub fn $name<Q: UnsafeRawValue>(values: &[Q]) -> Q {163 let $unwrapped_values: Vec<_> = values.iter().map(|q| q.unsafe_raw_value()).collect();36 unmodified lines200 let count = Ratio::new::<ratio>(count as f64);201 (sum / count).into()202 }203 212-quantity_fn!(median, values, statistical::median(&values));204+pub fn median<Q, P>(values: impl IntoIterator<Item = Q>) -> Q205+where206+ Q: Copy + PartialOrd + Add<Output = Q> + Div<Ratio, Output = P>,207+ P: Into<Q>,208+{209+ let mut values = values.into_iter().collect::<Vec<_>>();210+ values.sort_by(|a, b| a.partial_cmp(b).expect("No NaN values"));211+212+ let len = values.len();213+ if len % 2 == 0 {214+ let mid = len / 2;215+ let a = &values[mid - 1];216+ let b = &values[mid];217+ ((*a + *b) / Ratio::new::<ratio>(2.)).into()218+ } else {219+ values[len / 2]220+ }221+}222+223 quantity_fn!(standard_deviation, values, {224 let mean_value = statistical::mean(&values);225 statistical::standard_deviation(&values, Some(mean_value))226 });108 unmodified lines335 assert_eq!(336 mean(values.iter().copied()).format(TimeUnit::Second),337 "2.000 s"338 );329- assert_eq!(median(&values).format(TimeUnit::Second), "2.000 s");339+ assert_eq!(340+ median(values.iter().copied()).format(TimeUnit::Second),341+ "2.000 s"342+ );343 assert_eq!(min(&values).format(TimeUnit::Second), "1.000 s");344 assert_eq!(max(&values).format(TimeUnit::Second), "3.000 s");345 assert_eq!(346 standard_deviation(&values).format(TimeUnit::Second),10 unmodified lines