Add --setup option, closes #8
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent b0bab45 · Browse files · 3 files·+47 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+47 added−14 removed
- other:+47 added−14 removed
Provenance
- Commit
a4df90e- Tree
c4fd416- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
b0bab45
3 files changed+47 added−14 removed
modified:src/hyperfine/benchmark.rs+11 added-1 removed
1 use std::io;2 use std::process::{Command, Stdio};3 use std::time::Instant;4 5-use ansi_term::Colour::{Cyan, Green, White, Yellow, Purple};5+use ansi_term::Colour::{Cyan, Green, Purple, White, Yellow};6 use statistical::{mean, standard_deviation};7 8 use hyperfine::internal::{get_progress_bar, max, min, HyperfineOptions, Second, Warnings,9 MIN_EXECUTION_TIME};122 unmodified lines132 133 // Set up progress bar (and spinner for initial measurement)134 let bar = get_progress_bar(options.min_runs, "Initial time measurement");135 136+ // Run setup / cleanup command137+ let run_setup_command = || {138+ if let Some(ref setup_command) = options.setup_command {139+ let _ = time_shell_command(setup_command, options.ignore_failure, None);140+ }141+ };142+ run_setup_command();143+144 // Initial timing run145 let res = time_shell_command(cmd, options.ignore_failure, Some(shell_spawning_time))?;146 147 // Determine number of benchmark runs16 unmodified lines164 bar.set_length(count_remaining);165 166 // Gather statistics167 for _ in 0..count_remaining {168+ run_setup_command();169+170 let msg = {171 let mean = format_duration(mean(&execution_times), Unit::Auto);172 format!("Current estimate: {}", Green.paint(mean))173 };64 unmodified linesmodified:src/hyperfine/internal.rs+4 added
19 unmodified lines20 pub min_time_sec: Second,21 22 /// Whether or not to ignore non-zero exit codes23 pub ignore_failure: bool,24+25+ /// Command to run before each benchmark run26+ pub setup_command: Option<String>,27 }28 29 impl Default for HyperfineOptions {30 fn default() -> HyperfineOptions {1 unmodified line32 warmup_count: 0,33 min_runs: 10,34 min_time_sec: 3.0,35 ignore_failure: false,36+ setup_command: None,37 }38 }39 }40 48 unmodified linesmodified:src/main.rs+32 added-13 removed
33 unmodified lines34 Ok(())35 }36 37 fn main() {38+ // Process command line options39+ let mut options = HyperfineOptions::default();40+41 let matches = App::new("hyperfine")42 .version(crate_version!())43 .setting(AppSettings::ColoredHelp)44 .setting(AppSettings::DeriveDisplayOrder)5 unmodified lines50 .multiple(true)51 .empty_values(false),52 )53 .arg(54+ Arg::with_name("ignore-failure")55+ .long("ignore-failure")56+ .short("i")57+ .help("Ignore non-zero exit codes"),58+ )59+ .arg(60+ Arg::with_name("setup")61+ .long("setup")62+ .short("S")63+ .takes_value(true)64+ .value_name("CMD")65+ .help(66+ "Execute CMD before each benchmark run. This is useful for \67+ clearing disk caches, for example.",68+ ),69+ )70+ .arg(71 Arg::with_name("warmup")72 .long("warmup")73 .short("w")74 .takes_value(true)75 .value_name("NUM")56- .help("Perform NUM warmup runs before the actual benchmark"),76+ .help(77+ "Perform NUM warmup runs before the actual benchmark. This can be used \78+ to fill (disk) caches for I/O-heavy programs.",79+ ),80 )81 .arg(82 Arg::with_name("min-runs")83 .long("min-runs")84 .short("m")85 .takes_value(true)86 .value_name("NUM")64- .help("Perform at least NUM runs for each command"),87+ .help(&format!(88+ "Perform at least NUM runs for each command (default: {}).",89+ options.min_runs90+ )),91 )66- .arg(67- Arg::with_name("ignore-failure")68- .long("ignore-failure")69- .short("i")70- .help("Ignore non-zero exit codes"),71- )92 .get_matches();93 74- let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();94+ options.ignore_failure = matches.is_present("ignore-failure");95 76- // Process command line options77- let mut options = HyperfineOptions::default();96+ options.setup_command = matches.value_of("setup").map(String::from);97 98+ let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();99+100 options.warmup_count = matches101 .value_of("warmup")102 .and_then(&str_to_u64)103 .unwrap_or(0);2 unmodified lines106 // we need at least two runs to compute a variance107 options.min_runs = cmp::max(2, min_runs);108 }109 89- options.ignore_failure = matches.is_present("ignore-failure");90-110 let commands = matches.values_of("command").unwrap().collect();111 let res = run(&commands, &options);112 113 match res {4 unmodified lines