From 2784b7fc1bb9c4a319e257c861411160358e88f8 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sat, 20 Jan 2018 15:31:50 +0100 Subject: [PATCH] Detect failure of preparation command --- src/hyperfine/benchmark.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/hyperfine/benchmark.rs b/src/hyperfine/benchmark.rs index 33d7e07..358130c 100644 --- a/src/hyperfine/benchmark.rs +++ b/src/hyperfine/benchmark.rs @@ -123,6 +123,21 @@ pub fn mean_shell_spawning_time() -> io::Result { }) } +/// Run the command specified by `--prepare`. +fn run_preparation_command(command: &Option) -> io::Result<()> { + if let &Some(ref preparation_command) = command { + let res = time_shell_command(preparation_command, CmdFailureAction::RaiseError, None); + if let Err(_) = res { + return Err(io::Error::new( + io::ErrorKind::Other, + "The preparation command terminated with a non-zero exit code. \ + Append ' || true' to the command if you are sure that this can be ignored.", + )); + } + } + Ok(()) +} + /// Run the benchmark for a single shell command pub fn run_benchmark( num: usize, @@ -158,12 +173,7 @@ pub fn run_benchmark( let progress_bar = get_progress_bar(options.min_runs, "Initial time measurement"); // Run init / cleanup command - let run_preparation_command = || { - if let Some(ref preparation_command) = options.preparation_command { - let _ = time_shell_command(preparation_command, options.failure_action, None); - } - }; - run_preparation_command(); + run_preparation_command(&options.preparation_command)?; // Initial timing run let (res, success) = @@ -193,7 +203,7 @@ pub fn run_benchmark( // Gather statistics for _ in 0..count_remaining { - run_preparation_command(); + run_preparation_command(&options.preparation_command)?; let msg = { let mean = format_duration(mean(×_real), None);