Move more modules around
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 47091a5 · Browse files · 10 files·+158 added−158 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+158 added−158 removed
- other:+158 added−158 removed
Provenance
- Commit
999e7c4- Tree
784a2ab- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
47091a5
10 files changed+158 added−158 removed
modified:src/benchmark/mod.rs+1 added
1+pub mod relative_speed;2 pub mod result;3 4 use std::cmp;5 use std::process::{ExitStatus, Stdio};514 unmodified linesadded:src/benchmark/relative_speed.rs+100 added
1+use std::cmp::Ordering;2+3+use super::result::BenchmarkResult;4+use crate::units::Scalar;5+6+#[derive(Debug)]7+pub struct BenchmarkResultWithRelativeSpeed<'a> {8+ pub result: &'a BenchmarkResult,9+ pub relative_speed: Scalar,10+ pub relative_speed_stddev: Option<Scalar>,11+ pub is_fastest: bool,12+}13+14+pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {15+ l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)16+}17+18+pub fn compute(results: &[BenchmarkResult]) -> Option<Vec<BenchmarkResultWithRelativeSpeed>> {19+ let fastest: &BenchmarkResult = results20+ .iter()21+ .min_by(|&l, &r| compare_mean_time(l, r))22+ .expect("at least one benchmark result");23+24+ if fastest.mean == 0.0 {25+ return None;26+ }27+28+ Some(29+ results30+ .iter()31+ .map(|result| {32+ let ratio = result.mean / fastest.mean;33+34+ // https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas35+ // Covariance asssumed to be 0, i.e. variables are assumed to be independent36+ let ratio_stddev = match (result.stddev, fastest.stddev) {37+ (Some(result_stddev), Some(fastest_stddev)) => Some(38+ ratio39+ * ((result_stddev / result.mean).powi(2)40+ + (fastest_stddev / fastest.mean).powi(2))41+ .sqrt(),42+ ),43+ _ => None,44+ };45+46+ BenchmarkResultWithRelativeSpeed {47+ result,48+ relative_speed: ratio,49+ relative_speed_stddev: ratio_stddev,50+ is_fastest: result == fastest,51+ }52+ })53+ .collect(),54+ )55+}56+57+#[cfg(test)]58+fn create_result(name: &str, mean: Scalar) -> BenchmarkResult {59+ use std::collections::BTreeMap;60+61+ BenchmarkResult {62+ command: name.into(),63+ mean,64+ stddev: Some(1.0),65+ median: mean,66+ user: mean,67+ system: 0.0,68+ min: mean,69+ max: mean,70+ times: None,71+ exit_codes: Vec::new(),72+ parameters: BTreeMap::new(),73+ }74+}75+76+#[test]77+fn test_compute_relative_speed() {78+ use approx::assert_relative_eq;79+80+ let results = vec![81+ create_result("cmd1", 3.0),82+ create_result("cmd2", 2.0),83+ create_result("cmd3", 5.0),84+ ];85+86+ let annotated_results = compute(&results).unwrap();87+88+ assert_relative_eq!(1.5, annotated_results[0].relative_speed);89+ assert_relative_eq!(1.0, annotated_results[1].relative_speed);90+ assert_relative_eq!(2.5, annotated_results[2].relative_speed);91+}92+93+#[test]94+fn test_compute_relative_speed_for_zero_times() {95+ let results = vec![create_result("cmd1", 1.0), create_result("cmd2", 0.0)];96+97+ let annotated_results = compute(&results);98+99+ assert!(annotated_results.is_none());100+}modified:src/export/asciidoc.rs+1 added-1 removed
1+use crate::benchmark::relative_speed::{self, BenchmarkResultWithRelativeSpeed};2 use crate::benchmark::result::BenchmarkResult;3 use crate::output::format::format_duration_value;3-use crate::relative_speed::{self, BenchmarkResultWithRelativeSpeed};4 use crate::units::Unit;5 6 use super::Exporter;7 266 unmodified linesmodified:src/export/markdown.rs+1 added-1 removed
1 use super::Exporter;2+use crate::benchmark::relative_speed::{self, BenchmarkResultWithRelativeSpeed};3 use crate::benchmark::result::BenchmarkResult;4 use crate::output::format::format_duration_value;4-use crate::relative_speed::{self, BenchmarkResultWithRelativeSpeed};5 use crate::units::Unit;6 7 use anyhow::{anyhow, Result};8 285 unmodified linesmodified:src/parameter/mod.rs+2 added-2 removed
1-use crate::numeric::NumericType;1+use crate::number::Number;2 3 pub mod range;4 pub mod tokenize;5 6 #[derive(Debug, Clone, PartialEq, Eq)]7 pub enum ParameterValue {8 Text(String),9- Numeric(NumericType),9+ Numeric(Number),10 }11 12 impl<'a> ToString for ParameterValue {13 fn to_string(&self) -> String {6 unmodified linesmodified:src/parameter/range.rs+3 added-3 removed
6 unmodified lines7 8 use super::ParameterValue;9 use crate::command::Command;10 use crate::error::ParameterScanError;11-use crate::numeric::NumericType;11+use crate::number::Number;12 13 trait Numeric:14 Add<Output = Self>15 + Sub<Output = Self>2 unmodified lines18 + PartialOrd19 + Copy20 + Clone21 + From<i32>22- + Into<NumericType>22+ + Into<Number>23 {24 }25 impl<26 T: Add<Output = Self>3 unmodified lines30 + PartialOrd31 + Copy32 + Clone33 + From<i32>34- + Into<NumericType>,34+ + Into<Number>,35 > Numeric for T36 {37 }38 280 unmodified linesmodified:src/main.rs+2 added-3 removed
1 use std::env;2 3 use app::get_arg_matches;4 use benchmark::result::BenchmarkResult;5-use benchmark::{mean_shell_spawning_time, run_benchmark};5+use benchmark::{mean_shell_spawning_time, relative_speed, run_benchmark};6 use command::{build_commands, Command};7 use export::ExportManager;8 use options::{Options, OutputStyleOption};9 5 unmodified lines15 pub mod command;16 pub mod error;17 pub mod export;18 pub mod min_max;19-pub mod numeric;19+pub mod number;20 pub mod options;21 pub mod outlier_detection;22 pub mod output;23 pub mod parameter;24-pub mod relative_speed;24 pub mod shell;25 pub mod timer;26 pub mod units;27 95 unmodified linesadded:src/number.rs+48 added
1+use std::convert::TryFrom;2+use std::fmt;3+4+use rust_decimal::prelude::ToPrimitive;5+use rust_decimal::Decimal;6+use serde::Serialize;7+8+#[derive(Debug, Clone, Serialize, Copy, PartialEq, Eq)]9+#[serde(untagged)]10+pub enum Number {11+ Int(i32),12+ Decimal(Decimal),13+}14+15+impl fmt::Display for Number {16+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {17+ match *self {18+ Number::Int(i) => fmt::Display::fmt(&i, f),19+ Number::Decimal(i) => fmt::Display::fmt(&i, f),20+ }21+ }22+}23+24+impl From<i32> for Number {25+ fn from(x: i32) -> Number {26+ Number::Int(x)27+ }28+}29+30+impl From<Decimal> for Number {31+ fn from(x: Decimal) -> Number {32+ Number::Decimal(x)33+ }34+}35+36+impl TryFrom<Number> for usize {37+ type Error = ();38+39+ fn try_from(numeric: Number) -> Result<Self, Self::Error> {40+ match numeric {41+ Number::Int(i) => usize::try_from(i).map_err(|_| ()),42+ Number::Decimal(d) => match d.to_u64() {43+ Some(u) => usize::try_from(u).map_err(|_| ()),44+ None => Err(()),45+ },46+ }47+ }48+}deleted:src/numeric.rs-48 removed
1-use std::convert::TryFrom;2-use std::fmt;3-4-use rust_decimal::prelude::ToPrimitive;5-use rust_decimal::Decimal;6-use serde::Serialize;7-8-#[derive(Debug, Clone, Serialize, Copy, PartialEq, Eq)]9-#[serde(untagged)]10-pub enum NumericType {11- Int(i32),12- Decimal(Decimal),13-}14-15-impl fmt::Display for NumericType {16- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {17- match *self {18- NumericType::Int(i) => fmt::Display::fmt(&i, f),19- NumericType::Decimal(i) => fmt::Display::fmt(&i, f),20- }21- }22-}23-24-impl From<i32> for NumericType {25- fn from(x: i32) -> NumericType {26- NumericType::Int(x)27- }28-}29-30-impl From<Decimal> for NumericType {31- fn from(x: Decimal) -> NumericType {32- NumericType::Decimal(x)33- }34-}35-36-impl TryFrom<NumericType> for usize {37- type Error = ();38-39- fn try_from(numeric: NumericType) -> Result<Self, Self::Error> {40- match numeric {41- NumericType::Int(i) => usize::try_from(i).map_err(|_| ()),42- NumericType::Decimal(d) => match d.to_u64() {43- Some(u) => usize::try_from(u).map_err(|_| ()),44- None => Err(()),45- },46- }47- }48-}deleted:src/relative_speed.rs-100 removed
1-use std::cmp::Ordering;2-3-use crate::benchmark::result::BenchmarkResult;4-use crate::units::Scalar;5-6-#[derive(Debug)]7-pub struct BenchmarkResultWithRelativeSpeed<'a> {8- pub result: &'a BenchmarkResult,9- pub relative_speed: Scalar,10- pub relative_speed_stddev: Option<Scalar>,11- pub is_fastest: bool,12-}13-14-pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {15- l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)16-}17-18-pub fn compute(results: &[BenchmarkResult]) -> Option<Vec<BenchmarkResultWithRelativeSpeed>> {19- let fastest: &BenchmarkResult = results20- .iter()21- .min_by(|&l, &r| compare_mean_time(l, r))22- .expect("at least one benchmark result");23-24- if fastest.mean == 0.0 {25- return None;26- }27-28- Some(29- results30- .iter()31- .map(|result| {32- let ratio = result.mean / fastest.mean;33-34- // https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas35- // Covariance asssumed to be 0, i.e. variables are assumed to be independent36- let ratio_stddev = match (result.stddev, fastest.stddev) {37- (Some(result_stddev), Some(fastest_stddev)) => Some(38- ratio39- * ((result_stddev / result.mean).powi(2)40- + (fastest_stddev / fastest.mean).powi(2))41- .sqrt(),42- ),43- _ => None,44- };45-46- BenchmarkResultWithRelativeSpeed {47- result,48- relative_speed: ratio,49- relative_speed_stddev: ratio_stddev,50- is_fastest: result == fastest,51- }52- })53- .collect(),54- )55-}56-57-#[cfg(test)]58-fn create_result(name: &str, mean: Scalar) -> BenchmarkResult {59- use std::collections::BTreeMap;60-61- BenchmarkResult {62- command: name.into(),63- mean,64- stddev: Some(1.0),65- median: mean,66- user: mean,67- system: 0.0,68- min: mean,69- max: mean,70- times: None,71- exit_codes: Vec::new(),72- parameters: BTreeMap::new(),73- }74-}75-76-#[test]77-fn test_compute_relative_speed() {78- use approx::assert_relative_eq;79-80- let results = vec![81- create_result("cmd1", 3.0),82- create_result("cmd2", 2.0),83- create_result("cmd3", 5.0),84- ];85-86- let annotated_results = compute(&results).unwrap();87-88- assert_relative_eq!(1.5, annotated_results[0].relative_speed);89- assert_relative_eq!(1.0, annotated_results[1].relative_speed);90- assert_relative_eq!(2.5, annotated_results[2].relative_speed);91-}92-93-#[test]94-fn test_compute_relative_speed_for_zero_times() {95- let results = vec![create_result("cmd1", 1.0), create_result("cmd2", 0.0)];96-97- let annotated_results = compute(&results);98-99- assert!(annotated_results.is_none());100-}