Add --ignore-failure option, closes #11
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent 50b88b5 · Browse files · 1 file·+42 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+42 added−9 removed
- other:+42 added−9 removed
Provenance
- Commit
7dae4cd- Tree
8bd0094- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
50b88b5
1 file changed+42 added−9 removed
modified:src/main.rs+42 added-9 removed
9 unmodified lines10 use std::time::Instant;11 use std::fmt;12 13 use indicatif::{ProgressBar, ProgressStyle};14-use ansi_term::Colour::{Cyan, Green, Yellow};14+use ansi_term::Colour::{Cyan, Green, Red, Yellow};15 use clap::{App, AppSettings, Arg};16 17 /// Type alias for unit of time18 type Second = f64;2 unmodified lines21 const MIN_EXECUTION_TIME: Second = 5e-3;22 23 /// Print error message to stderr and terminate24 pub fn error(message: &str) -> ! {25- eprintln!("{}", message);25+ eprintln!("{}: {}", Red.paint("Error"), message);26 std::process::exit(1);27 }28 29 struct CmdResult {13 unmodified lines43 }44 }45 46 /// Run the given shell command and measure the execution time47-fn time_shell_command(shell_cmd: &str) -> io::Result<CmdResult> {47+fn time_shell_command(shell_cmd: &str, ignore_failure: bool) -> io::Result<CmdResult> {48 let start = Instant::now();49 50 let status = Command::new("sh")51 .arg("-c")2 unmodified lines54 .stdout(Stdio::null())55 .stderr(Stdio::null())56 .status()?;57 58+ if !ignore_failure && !status.success() {59+ return Err(io::Error::new(60+ io::ErrorKind::Other,61+ "Command terminated with non-zero exit code. \62+ Use the '-i'/'--ignore-failure' option if you want to ignore this.",63+ ));64+ }65+66 let duration = start.elapsed();67 68 let execution_time_sec = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;69 28 unmodified lines98 "Command took less than {:.0} ms to complete. \99 Results might be inaccurate due to the intermediate shell call.",100 MIN_EXECUTION_TIME * 1e3101 ),94- &Warnings::NonZeroExitCode => write!(f, "Process exited with a non-zero exit code"),102+ &Warnings::NonZeroExitCode => write!(f, "Ignoring non-zero exit code."),103 }104 }105 }106 27 unmodified lines134 let bar = get_progress_bar(options.warmup_count, "Performing warmup runs");135 136 for _ in 1..options.warmup_count {137 bar.inc(1);130- let _ = time_shell_command(cmd);138+ let _ = time_shell_command(cmd, options.ignore_failure)?;139 }140 bar.finish_and_clear();141 }142 143 // Set up progress bar (and spinner for initial measurement)144 let bar = get_progress_bar(options.min_runs, "Initial time measurement");145 146 // Initial timing run139- let res = time_shell_command(cmd)?;147+ let res = time_shell_command(cmd, options.ignore_failure)?;148 149 let runs_in_min_time = (options.min_time_sec / res.execution_time_sec) as u64;150 151 let count = if runs_in_min_time >= options.min_runs {11 unmodified lines163 164 // Gather statistics165 for _ in 1..count {166 bar.inc(1);159- let res = time_shell_command(cmd)?;167+ let res = time_shell_command(cmd, options.ignore_failure)?;168 results.push(res);169 }170 bar.finish_and_clear();171 48 unmodified lines220 221 let mut times: Vec<Second> = vec![];222 for _ in 1..COUNT {223 bar.inc(1);216- let res = time_shell_command("")?;217- times.push(res.execution_time_sec);224+ let res = time_shell_command("", false);225+226+ match res {227+ Err(_) => {228+ return Err(io::Error::new(229+ io::ErrorKind::Other,230+ "Could not measure shell execution time. \231+ Make sure you can run 'sh -c \"\"'.",232+ ))233+ }234+ Ok(r) => {235+ times.push(r.execution_time_sec);236+ }237+ }238 }239 bar.finish_and_clear();240 241 let mean: f64 = mean(times.iter().cloned()); // TODO: get rid of .cloned()20 unmodified lines262 pub min_runs: u64,263 264 /// Minimum benchmarking time265 pub min_time_sec: Second,266+267+ /// Whether or not to ignore non-zero exit codes268+ pub ignore_failure: bool,269 }270 271 impl Default for HyperfineOptions {272 fn default() -> HyperfineOptions {273 HyperfineOptions {274 warmup_count: 0,275 min_runs: 10,276 min_time_sec: 3.0,277+ ignore_failure: false,278 }279 }280 }281 25 unmodified lines307 .takes_value(true)308 .value_name("NUM")309 .help("Perform at least NUM runs for each command"),310 )311+ .arg(312+ Arg::with_name("ignore-failure")313+ .long("ignore-failure")314+ .short("i")315+ .help("Ignore non-zero exit codes"),316+ )317 .get_matches();318 319 let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();320 321 // Process command line options322 let mut options = HyperfineOptions::default();323+324 options.warmup_count = matches325 .value_of("warmup")326 .and_then(&str_to_u64)327 .unwrap_or(0);1 unmodified line329 if let Some(min_runs) = matches.value_of("min-runs").and_then(&str_to_u64) {330 options.min_runs = cmp::max(1, min_runs);331 }332 333+ options.ignore_failure = matches.is_present("ignore-failure");334+335 let commands = matches.values_of("command").unwrap().collect();336 let res = run(&commands, &options);337 338 match res {4 unmodified lines