Errors
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- if execution is faster than 1 ms - if command is empty
authored by Henri Menke · committed by David Peter · parent 5e69689 · Browse files · 1 file·+29 added−7 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+29 added−7 removed
- other:+29 added−7 removed
Provenance
- Commit
219bb1e- Tree
693a062- Author
Henri Menke <henri@icp.uni-stuttgart.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
5e69689
1 file changed+29 added−7 removed
modified:src/main.rs+29 added-7 removed
2 unmodified lines3 extern crate clap;4 extern crate indicatif;5 6 use std::cmp;7+use std::error::Error;8+use std::io;9 use std::process::{Command, Stdio};10 use std::time::Instant;11 12 use indicatif::{ProgressBar, ProgressStyle};13 use ansi_term::Colour::{Cyan, Green, Red};14 use clap::{App, AppSettings, Arg};15 16+/// Print error message to stderr and terminate17+pub fn error(message: &str) -> ! {18+ eprintln!("{}", message);19+ std::process::exit(1);20+}21+22 struct CmdResult {23 /// Execution time in seconds24 execution_time_sec: f64,25 10 unmodified lines36 }37 }38 39 /// Run the given shell command and measure the execution time32-fn time_shell_command(shell_cmd: &str) -> CmdResult {40+fn time_shell_command(shell_cmd: &str) -> io::Result<CmdResult> {41 let start = Instant::now();42 43 let status = Command::new("sh")44 .arg("-c")45 .arg(shell_cmd)46 .stdout(Stdio::null())47 .stderr(Stdio::null())40- .status()41- .expect("failed to execute process");48+ .status()?;49 50 let duration = start.elapsed();51 52 let execution_time_sec = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;53 47- CmdResult::new(execution_time_sec, status.success())54+ const MILLISECOND: f64 = 1e-3;55+ if execution_time_sec < MILLISECOND {56+ Err(io::Error::new(io::ErrorKind::Other, format!{57+ "Command took only {:.6} s to complete. Execution is probably dominated by shell overhead.",58+ execution_time_sec59+ }))60+ } else {61+ Ok(CmdResult::new(execution_time_sec, status.success()))62+ }63 }64 65 /// Return a pre-configured progress bar66 fn get_progress_bar(length: u64, msg: &str) -> ProgressBar {30 unmodified lines97 // Set up progress bar (and spinner for initial measurement)98 let bar = get_progress_bar(options.min_runs, "Initial time measurement");99 100 // Initial timing run86- let res = time_shell_command(cmd);101+ let res = match time_shell_command(cmd) {102+ Ok(s) => s,103+ Err(e) => error(e.description()),104+ };105 106 let runs_in_min_time = (options.min_time_sec / res.execution_time_sec) as u64;107 108 let count = if runs_in_min_time >= options.min_runs {11 unmodified lines120 121 // Gather statistics122 for _ in 1..count {123 bar.inc(1);106- let res = time_shell_command(cmd);124+ let res = match time_shell_command(cmd) {125+ Ok(s) => s,126+ Err(e) => error(e.description()),127+ };128 results.push(res);129 }130 bar.finish_and_clear();131 46 unmodified lines178 .arg(179 Arg::with_name("command")180 .help("Command to benchmark")181 .required(true)161- .multiple(true),182+ .multiple(true)183+ .empty_values(false),184 )185 .arg(186 Arg::with_name("warmup")187 .long("warmup")31 unmodified lines