Use statistical crate
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent c432fec · Browse files · 6 files·+142 added−53 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+142 added−53 removed
- other:+142 added−53 removed
Provenance
- Commit
05e5899- Tree
d52a03e- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
c432fec
6 files changed+142 added−53 removed
modified:src/hyperfine/benchmark.rs+48 added-38 removed
1 unmodified line2 use std::process::{Command, Stdio};3 use std::time::Instant;4 5 use ansi_term::Colour::{Cyan, Green, White, Yellow};6+use statistical::{mean, standard_deviation};7 8 use hyperfine::internal::{get_progress_bar, HyperfineOptions, Second, Warnings, MIN_EXECUTION_TIME};9 use hyperfine::format::{format_duration, format_duration_unit, Unit};9-use hyperfine::statistics::mean;10 11 /// Results from timing a single shell command12 pub struct TimingResult {13 /// Execution time in seconds14- pub execution_time_sec: Second,14+ pub execution_time: Second,15 16 /// True if the command finished with exit code zero17 pub success: bool,18 }19 20 impl TimingResult {21- fn new(execution_time_sec: Second, success: bool) -> TimingResult {21+ fn new(execution_time: Second, success: bool) -> TimingResult {22 TimingResult {23- execution_time_sec,23+ execution_time,24 success,25 }26 }27 }28 29 /// Run the given shell command and measure the execution time30-pub fn time_shell_command(shell_cmd: &str, ignore_failure: bool) -> io::Result<TimingResult> {30+pub fn time_shell_command(31+ shell_cmd: &str,32+ ignore_failure: bool,33+ shell_spawning_time: Option<Second>,34+) -> io::Result<TimingResult> {35 let start = Instant::now();36 37 let status = Command::new("sh")38 .arg("-c")12 unmodified lines51 }52 53 let duration = start.elapsed();54 51- let execution_time_sec = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;55+ let execution_time = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;56 53- Ok(TimingResult::new(execution_time_sec, status.success()))57+ // Correct for shell spawning time58+ let execution_time_corrected = if let Some(spawning_time) = shell_spawning_time {59+ if execution_time < spawning_time {60+ 0.061+ } else {62+ execution_time - spawning_time63+ }64+ } else {65+ execution_time66+ };67+68+ Ok(TimingResult::new(69+ execution_time_corrected,70+ status.success(),71+ ))72 }73 74 /// Measure the average shell spawning time75 pub fn mean_shell_spawning_time() -> io::Result<Second> {1 unmodified line77 let bar = get_progress_bar(COUNT, "Measuring shell spawning time");78 79 let mut times: Vec<Second> = vec![];80 for _ in 0..COUNT {81+ // Just run the shell without any command63- let res = time_shell_command("", false);82+ let res = time_shell_command("", false, None);83 84 match res {85 Err(_) => {86 return Err(io::Error::new(2 unmodified lines89 Make sure you can run 'sh -c \"\"'.",90 ))91 }92 Ok(r) => {74- times.push(r.execution_time_sec);93+ times.push(r.execution_time);94 }95 }96 bar.inc(1);97 }98 bar.finish_and_clear();99 81- let mean: f64 = mean(times.iter().cloned()); // TODO: get rid of .cloned()82- Ok(mean)100+ Ok(mean(×))101 }102 103 /// Run the benchmark for a single shell command104 pub fn run_benchmark(1 unmodified line106 cmd: &str,107 shell_spawning_time: Second,108 options: &HyperfineOptions,109 ) -> io::Result<()> {92- // Helper function to compute corrected execution times93- let get_execution_time = |r: &TimingResult| -> Second {94- if r.execution_time_sec < shell_spawning_time {95- 0.096- } else {97- r.execution_time_sec - shell_spawning_time98- }99- };100-110 println!(111 "{}{}: {}",112 White.bold().paint("Benchmark #"),113 White.bold().paint((num + 1).to_string()),114 Cyan.paint(cmd)115 );116 println!();117 109- let mut results = vec![];118+ let mut execution_times: Vec<Second> = vec![];119+ let mut all_succeeded = true;120 121 // Warmup phase122 if options.warmup_count > 0 {123 let bar = get_progress_bar(options.warmup_count, "Performing warmup runs");124 125 for _ in 0..options.warmup_count {116- let _ = time_shell_command(cmd, options.ignore_failure)?;126+ let _ = time_shell_command(cmd, options.ignore_failure, None)?;127 bar.inc(1);128 }129 bar.finish_and_clear();130 }1 unmodified line132 // Set up progress bar (and spinner for initial measurement)133 let bar = get_progress_bar(options.min_runs, "Initial time measurement");134 135 // Initial timing run126- let res = time_shell_command(cmd, options.ignore_failure)?;136+ let res = time_shell_command(cmd, options.ignore_failure, Some(shell_spawning_time))?;137 138 // Determine number of benchmark runs129- let runs_in_min_time = (options.min_time_sec / res.execution_time_sec) as u64;139+ let runs_in_min_time =140+ (options.min_time_sec / (res.execution_time + shell_spawning_time)) as u64;141 142 let count = if runs_in_min_time >= options.min_runs {143 runs_in_min_time144 } else {2 unmodified lines147 148 let count_remaining = count - 1;149 150 // Save the first result140- results.push(res);151+ execution_times.push(res.execution_time);152+ all_succeeded = all_succeeded && res.success;153 154 // Re-configure the progress bar155 bar.set_length(count_remaining);156 157 // Gather statistics158 for _ in 0..count_remaining {159 let msg = {148- let execution_times = results.iter().map(&get_execution_time);149- let mean = format_duration(mean(execution_times), Unit::Auto);160+ let mean = format_duration(mean(&execution_times), Unit::Auto);161 format!("Current estimate: {}", Green.paint(mean))162 };163 bar.set_message(&msg);164 154- let res = time_shell_command(cmd, options.ignore_failure)?;155- results.push(res);165+ let res = time_shell_command(cmd, options.ignore_failure, Some(shell_spawning_time))?;166+167+ execution_times.push(res.execution_time);168+ all_succeeded = all_succeeded && res.success;169+170 bar.inc(1);171 }172 bar.finish_and_clear();173 174 // Compute statistical quantities175 176 // Corrected execution times163- let execution_times = results.iter().map(&get_execution_time);177+ let mean = mean(&execution_times);178+ let stddev = standard_deviation(&execution_times, Some(mean));179 165- let t_mean = mean(execution_times.clone());166- let t2_mean = mean(execution_times.clone().map(|t| t.powi(2)));167-168- let stddev = (t2_mean - t_mean.powi(2)).sqrt();169-180 // Formatting and console output171- let (mean_str, unit_mean) = format_duration_unit(t_mean, Unit::Auto);181+ let (mean_str, unit_mean) = format_duration_unit(mean, Unit::Auto);182 let stddev_str = format_duration(stddev, unit_mean);183 let time_fmt = format!("{} ± {}", mean_str, stddev_str);184 185 println!(" Time: {}", Green.paint(time_fmt));1 unmodified line187 // Warnings188 let mut warnings = vec![];189 190 // Check execution time181- if execution_times.clone().any(|t| t < MIN_EXECUTION_TIME) {191+ if execution_times.iter().any(|&t| t < MIN_EXECUTION_TIME) {192 warnings.push(Warnings::FastExecutionTime);193 }194 195 // Check programm exit codes186- if results.iter().any(|r| !r.success) {196+ if !all_succeeded {197 warnings.push(Warnings::NonZeroExitCode);198 }199 200 if warnings.len() > 0 {10 unmodified linesmodified:src/hyperfine/mod.rs-1 removed
1 pub mod benchmark;2 pub mod format;3 pub mod internal;4-pub mod statistics;deleted:src/hyperfine/statistics.rs-13 removed
1-/// Calculate statistical average2-pub fn mean<I>(values: I) -> f643-where4- I: IntoIterator<Item = f64>,5-{6- let mut sum: f64 = 0.0;7- let mut len: u64 = 0;8- for v in values {9- sum += v;10- len += 1;11- }12- sum / (len as f64)13-}modified:src/main.rs+3 added-1 removed
1 extern crate ansi_term;2 #[macro_use]3 extern crate clap;4 extern crate indicatif;5+extern crate statistical;6 7 use std::cmp;8 use std::error::Error;9 use std::io;71 unmodified lines81 .and_then(&str_to_u64)82 .unwrap_or(0);83 84 if let Some(min_runs) = matches.value_of("min-runs").and_then(&str_to_u64) {85+ // we need at least two runs to compute a variance84- options.min_runs = cmp::max(1, min_runs);86+ options.min_runs = cmp::max(2, min_runs);87 }88 89 options.ignore_failure = matches.is_present("ignore-failure");90 8 unmodified linesmodified:Cargo.lock+90 added
86 unmodified lines87 dependencies = [88 "ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",89 "clap 2.29.1 (registry+https://github.com/rust-lang/crates.io-index)",90 "indicatif 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",91+ "statistical 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",92 ]93 94 [[package]]95 name = "indicatif"38 unmodified lines134 "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",135 ]136 137 [[package]]138+name = "num"139+version = "0.1.41"140+source = "registry+https://github.com/rust-lang/crates.io-index"141+dependencies = [142+ "num-bigint 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",143+ "num-complex 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",144+ "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",145+ "num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",146+ "num-rational 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",147+ "num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",148+]149+150+[[package]]151+name = "num-bigint"152+version = "0.1.41"153+source = "registry+https://github.com/rust-lang/crates.io-index"154+dependencies = [155+ "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",156+ "num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",157+ "rand 0.3.20 (registry+https://github.com/rust-lang/crates.io-index)",158+ "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)",159+]160+161+[[package]]162+name = "num-complex"163+version = "0.1.41"164+source = "registry+https://github.com/rust-lang/crates.io-index"165+dependencies = [166+ "num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",167+ "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)",168+]169+170+[[package]]171+name = "num-integer"172+version = "0.1.35"173+source = "registry+https://github.com/rust-lang/crates.io-index"174+dependencies = [175+ "num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",176+]177+178+[[package]]179+name = "num-iter"180+version = "0.1.34"181+source = "registry+https://github.com/rust-lang/crates.io-index"182+dependencies = [183+ "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",184+ "num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",185+]186+187+[[package]]188+name = "num-rational"189+version = "0.1.40"190+source = "registry+https://github.com/rust-lang/crates.io-index"191+dependencies = [192+ "num-bigint 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",193+ "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",194+ "num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",195+ "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)",196+]197+198+[[package]]199+name = "num-traits"200+version = "0.1.41"201+source = "registry+https://github.com/rust-lang/crates.io-index"202+203+[[package]]204 name = "owning_ref"205 version = "0.3.3"206 source = "registry+https://github.com/rust-lang/crates.io-index"207 dependencies = [59 unmodified lines267 version = "0.4.2"268 source = "registry+https://github.com/rust-lang/crates.io-index"269 270 [[package]]271+name = "rustc-serialize"272+version = "0.3.24"273+source = "registry+https://github.com/rust-lang/crates.io-index"274+275+[[package]]276 name = "smallvec"277 version = "0.6.0"278 source = "registry+https://github.com/rust-lang/crates.io-index"279 2 unmodified lines282 version = "1.0.0"283 source = "registry+https://github.com/rust-lang/crates.io-index"284 285 [[package]]286+name = "statistical"287+version = "0.1.1"288+source = "registry+https://github.com/rust-lang/crates.io-index"289+dependencies = [290+ "num 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",291+ "rand 0.3.20 (registry+https://github.com/rust-lang/crates.io-index)",292+]293+294+[[package]]295 name = "strsim"296 version = "0.6.0"297 source = "registry+https://github.com/rust-lang/crates.io-index"298 110 unmodified lines409 "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73"410 "checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d"411 "checksum libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "1e5d97d6708edaa407429faa671b942dc0f2727222fb6b6539bf1db936e4b121"412 "checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d"413+"checksum num 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cc4083e14b542ea3eb9b5f33ff48bd373a92d78687e74f4cc0a30caeb754f0ca"414+"checksum num-bigint 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "bdc1494b5912f088f260b775799468d9b9209ac60885d8186a547a0476289e23"415+"checksum num-complex 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "58de7b4bf7cf5dbecb635a5797d489864eadd03b107930cbccf9e0fd7428b47c"416+"checksum num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "d1452e8b06e448a07f0e6ebb0bb1d92b8890eea63288c0b627331d53514d0fba"417+"checksum num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "7485fcc84f85b4ecd0ea527b14189281cf27d60e583ae65ebc9c088b13dffe01"418+"checksum num-rational 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "0c7cb72a95250d8a370105c828f388932373e0e94414919891a0f945222310fe"419+"checksum num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cacfcab5eb48250ee7d0c7896b51a2c5eec99c1feea5f32025635f5ae4b00070"420 "checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"421 "checksum parking_lot 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3e7f7c9857874e54afeb950eebeae662b1e51a2493666d2ea4c0a5d91dcf0412"422 "checksum parking_lot_core 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "9f35048d735bb93dd115a0030498785971aab3234d311fbe273d020084d26bd8"423 "checksum rand 0.3.20 (registry+https://github.com/rust-lang/crates.io-index)" = "512870020642bb8c221bf68baa1b2573da814f6ccfe5c9699b1c303047abe9b1"424 "checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd"425 "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"426 "checksum regex 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "744554e01ccbd98fff8c457c3b092cd67af62a555a43bfe97ae8a0451f7799fa"427 "checksum regex-syntax 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8e931c58b93d86f080c734bfd2bce7dd0079ae2331235818133c8be7f422e20e"428+"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"429 "checksum smallvec 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44db0ecb22921ef790d17ae13a3f6d15784183ff5f2a01aa32098c7498d2b4b9"430 "checksum stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "15132e0e364248108c5e2c02e3ab539be8d6f5d52a01ca9bbf27ed657316f02b"431+"checksum statistical 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c139942f46d96c53b28420a2cdfb374629f122656bd9daef7fc221ed4d8ec228"432 "checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694"433 "checksum term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9e5b9a66db815dcfd2da92db471106457082577c3c278d4138ab3e3b4e189327"434 "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"435 "checksum termios 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d9cf598a6d7ce700a4e6a9199da127e6819a61e64b68609683cc9a01b5683a"11 unmodified linesmodified:Cargo.toml+1 added
10 unmodified lines1112131415161718192010 unmodified lines
[dependencies]ansi_term = "0.10"indicatif = "0.8"statistical = "0.1"
[dependencies.clap]version = "2"default-features = falsefeatures = ["suggestions", "color", "wrap_help"]