Skip to content
Myr

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

6 files+142 added−53 removed

modified:src/hyperfine/benchmark.rs+48 added-38 removed
1 unmodified line
2use std::process::{Command, Stdio};3use std::time::Instant;45use ansi_term::Colour::{Cyan, Green, White, Yellow};6use statistical::{mean, standard_deviation};78use hyperfine::internal::{get_progress_bar, HyperfineOptions, Second, Warnings, MIN_EXECUTION_TIME};9use hyperfine::format::{format_duration, format_duration_unit, Unit};9use hyperfine::statistics::mean;1011/// Results from timing a single shell command12pub struct TimingResult {13 /// Execution time in seconds14 pub execution_time_sec: Second,14 pub execution_time: Second,1516 /// True if the command finished with exit code zero17 pub success: bool,18}1920impl 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}2829/// Run the given shell command and measure the execution time30pub fn time_shell_command(shell_cmd: &str, ignore_failure: bool) -> io::Result<TimingResult> {30pub 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();3637 let status = Command::new("sh")38 .arg("-c")
12 unmodified lines
51 }5253 let duration = start.elapsed();5451 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;5653 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 };6768 Ok(TimingResult::new(69 execution_time_corrected,70 status.success(),71 ))72}7374/// Measure the average shell spawning time75pub fn mean_shell_spawning_time() -> io::Result<Second> {
1 unmodified line
77 let bar = get_progress_bar(COUNT, "Measuring shell spawning time");7879 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);8384 match res {85 Err(_) => {86 return Err(io::Error::new(
2 unmodified lines
89 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();9981 let mean: f64 = mean(times.iter().cloned()); // TODO: get rid of .cloned()82 Ok(mean)100 Ok(mean(&times))101}102103/// Run the benchmark for a single shell command104pub fn run_benchmark(
1 unmodified line
106 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 };100110 println!(111 "{}{}: {}",112 White.bold().paint("Benchmark #"),113 White.bold().paint((num + 1).to_string()),114 Cyan.paint(cmd)115 );116 println!();117109 let mut results = vec![];118 let mut execution_times: Vec<Second> = vec![];119 let mut all_succeeded = true;120121 // Warmup phase122 if options.warmup_count > 0 {123 let bar = get_progress_bar(options.warmup_count, "Performing warmup runs");124125 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 line
132 // Set up progress bar (and spinner for initial measurement)133 let bar = get_progress_bar(options.min_runs, "Initial time measurement");134135 // 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))?;137138 // 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;141142 let count = if runs_in_min_time >= options.min_runs {143 runs_in_min_time144 } else {
2 unmodified lines
147148 let count_remaining = count - 1;149150 // Save the first result140 results.push(res);151 execution_times.push(res.execution_time);152 all_succeeded = all_succeeded && res.success;153154 // Re-configure the progress bar155 bar.set_length(count_remaining);156157 // 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);164154 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))?;166167 execution_times.push(res.execution_time);168 all_succeeded = all_succeeded && res.success;169170 bar.inc(1);171 }172 bar.finish_and_clear();173174 // Compute statistical quantities175176 // 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));179165 let t_mean = mean(execution_times.clone());166 let t2_mean = mean(execution_times.clone().map(|t| t.powi(2)));167168 let stddev = (t2_mean - t_mean.powi(2)).sqrt();169180 // 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);184185 println!(" Time: {}", Green.paint(time_fmt));
1 unmodified line
187 // Warnings188 let mut warnings = vec![];189190 // 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 }194195 // Check programm exit codes186 if results.iter().any(|r| !r.success) {196 if !all_succeeded {197 warnings.push(Warnings::NonZeroExitCode);198 }199200 if warnings.len() > 0 {
10 unmodified lines
modified:src/hyperfine/mod.rs-1 removed
1pub mod benchmark;2pub mod format;3pub mod internal;4pub mod statistics;
deleted:src/hyperfine/statistics.rs-13 removed
1/// Calculate statistical average2pub fn mean<I>(values: I) -> f643where4 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
1extern crate ansi_term;2#[macro_use]3extern crate clap;4extern crate indicatif;5extern crate statistical;67use std::cmp;8use std::error::Error;9use std::io;
71 unmodified lines
81 .and_then(&str_to_u64)82 .unwrap_or(0);8384 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 }8889 options.ignore_failure = matches.is_present("ignore-failure");90
8 unmodified lines
modified:Cargo.lock+90 added
86 unmodified lines
87dependencies = [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]9394[[package]]95name = "indicatif"
38 unmodified lines
134 "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",135]136137[[package]]138name = "num"139version = "0.1.41"140source = "registry+https://github.com/rust-lang/crates.io-index"141dependencies = [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]149150[[package]]151name = "num-bigint"152version = "0.1.41"153source = "registry+https://github.com/rust-lang/crates.io-index"154dependencies = [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]160161[[package]]162name = "num-complex"163version = "0.1.41"164source = "registry+https://github.com/rust-lang/crates.io-index"165dependencies = [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]169170[[package]]171name = "num-integer"172version = "0.1.35"173source = "registry+https://github.com/rust-lang/crates.io-index"174dependencies = [175 "num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",176]177178[[package]]179name = "num-iter"180version = "0.1.34"181source = "registry+https://github.com/rust-lang/crates.io-index"182dependencies = [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]186187[[package]]188name = "num-rational"189version = "0.1.40"190source = "registry+https://github.com/rust-lang/crates.io-index"191dependencies = [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]197198[[package]]199name = "num-traits"200version = "0.1.41"201source = "registry+https://github.com/rust-lang/crates.io-index"202203[[package]]204name = "owning_ref"205version = "0.3.3"206source = "registry+https://github.com/rust-lang/crates.io-index"207dependencies = [
59 unmodified lines
267version = "0.4.2"268source = "registry+https://github.com/rust-lang/crates.io-index"269270[[package]]271name = "rustc-serialize"272version = "0.3.24"273source = "registry+https://github.com/rust-lang/crates.io-index"274275[[package]]276name = "smallvec"277version = "0.6.0"278source = "registry+https://github.com/rust-lang/crates.io-index"279
2 unmodified lines
282version = "1.0.0"283source = "registry+https://github.com/rust-lang/crates.io-index"284285[[package]]286name = "statistical"287version = "0.1.1"288source = "registry+https://github.com/rust-lang/crates.io-index"289dependencies = [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]293294[[package]]295name = "strsim"296version = "0.6.0"297source = "registry+https://github.com/rust-lang/crates.io-index"298
110 unmodified lines
409"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 lines
modified:Cargo.toml+1 added