Skip to content
Myr

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

1 file+29 added−7 removed

modified:src/main.rs+29 added-7 removed
2 unmodified lines
3extern crate clap;4extern crate indicatif;56use std::cmp;7use std::error::Error;8use std::io;9use std::process::{Command, Stdio};10use std::time::Instant;1112use indicatif::{ProgressBar, ProgressStyle};13use ansi_term::Colour::{Cyan, Green, Red};14use clap::{App, AppSettings, Arg};1516/// Print error message to stderr and terminate17pub fn error(message: &str) -> ! {18 eprintln!("{}", message);19 std::process::exit(1);20}2122struct CmdResult {23 /// Execution time in seconds24 execution_time_sec: f64,25
10 unmodified lines
36 }37}3839/// Run the given shell command and measure the execution time32fn time_shell_command(shell_cmd: &str) -> CmdResult {40fn time_shell_command(shell_cmd: &str) -> io::Result<CmdResult> {41 let start = Instant::now();4243 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()?;4950 let duration = start.elapsed();5152 let execution_time_sec = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;5347 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}6465/// Return a pre-configured progress bar66fn get_progress_bar(length: u64, msg: &str) -> ProgressBar {
30 unmodified lines
97 // Set up progress bar (and spinner for initial measurement)98 let bar = get_progress_bar(options.min_runs, "Initial time measurement");99100 // 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 };105106 let runs_in_min_time = (options.min_time_sec / res.execution_time_sec) as u64;107108 let count = if runs_in_min_time >= options.min_runs {
11 unmodified lines
120121 // 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 lines
178 .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