Skip to content
Myr

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

1 file+42 added−9 removed

modified:src/main.rs+42 added-9 removed
9 unmodified lines
10use std::time::Instant;11use std::fmt;1213use indicatif::{ProgressBar, ProgressStyle};14use ansi_term::Colour::{Cyan, Green, Yellow};14use ansi_term::Colour::{Cyan, Green, Red, Yellow};15use clap::{App, AppSettings, Arg};1617/// Type alias for unit of time18type Second = f64;
2 unmodified lines
21const MIN_EXECUTION_TIME: Second = 5e-3;2223/// Print error message to stderr and terminate24pub fn error(message: &str) -> ! {25 eprintln!("{}", message);25 eprintln!("{}: {}", Red.paint("Error"), message);26 std::process::exit(1);27}2829struct CmdResult {
13 unmodified lines
43 }44}4546/// Run the given shell command and measure the execution time47fn time_shell_command(shell_cmd: &str) -> io::Result<CmdResult> {47fn time_shell_command(shell_cmd: &str, ignore_failure: bool) -> io::Result<CmdResult> {48 let start = Instant::now();4950 let status = Command::new("sh")51 .arg("-c")
2 unmodified lines
54 .stdout(Stdio::null())55 .stderr(Stdio::null())56 .status()?;5758 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 }6566 let duration = start.elapsed();6768 let execution_time_sec = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;69
28 unmodified lines
98 "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 lines
134 let bar = get_progress_bar(options.warmup_count, "Performing warmup runs");135136 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 }142143 // Set up progress bar (and spinner for initial measurement)144 let bar = get_progress_bar(options.min_runs, "Initial time measurement");145146 // Initial timing run139 let res = time_shell_command(cmd)?;147 let res = time_shell_command(cmd, options.ignore_failure)?;148149 let runs_in_min_time = (options.min_time_sec / res.execution_time_sec) as u64;150151 let count = if runs_in_min_time >= options.min_runs {
11 unmodified lines
163164 // 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 lines
220221 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);225226 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();240241 let mean: f64 = mean(times.iter().cloned()); // TODO: get rid of .cloned()
20 unmodified lines
262 pub min_runs: u64,263264 /// Minimum benchmarking time265 pub min_time_sec: Second,266267 /// Whether or not to ignore non-zero exit codes268 pub ignore_failure: bool,269}270271impl 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 lines
307 .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();318319 let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();320321 // Process command line options322 let mut options = HyperfineOptions::default();323324 options.warmup_count = matches325 .value_of("warmup")326 .and_then(&str_to_u64)327 .unwrap_or(0);
1 unmodified line
329 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 }332333 options.ignore_failure = matches.is_present("ignore-failure");334335 let commands = matches.values_of("command").unwrap().collect();336 let res = run(&commands, &options);337338 match res {
4 unmodified lines