Skip to content
Myr

Add option to set the shell, closes #61

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Allow the user to set the shell used for executing benchmark commands. The option (-S/--shell) can be overridden, when there are duplicate options the last option's value will be used.

authored by Jason Peacock · committed by David Peter · parent 0b18446 · Browse files · 5 files+55 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+55 added−20 removed

  • other:+55 added−20 removed

5 files+55 added−20 removed

modified:src/hyperfine/app.rs+9 added
108 unmodified lines
109 the colors without any interactive output.",110 ),111 )112 .arg(113 Arg::with_name("shell")114 .long("shell")115 .short("S")116 .takes_value(true)117 .value_name("SHELL")118 .overrides_with("shell")119 .help("Set the shell to use (default: sh) for executing benchmarked commands."),120 )121 .arg(122 Arg::with_name("ignore-failure")123 .long("ignore-failure")124 .short("i")125 .help("Ignore non-zero exit codes."),
36 unmodified lines
modified:src/hyperfine/benchmark.rs+21 added-9 removed
37 unmodified lines
38}3940/// Run the given shell command and measure the execution time41pub fn time_shell_command(42 shell: &str,43 command: &Command,44 show_output: bool,45 failure_action: CmdFailureAction,46 shell_spawning_time: Option<TimingResult>,
4 unmodified lines
51 (Stdio::inherit(), Stdio::inherit())52 } else {53 (Stdio::null(), Stdio::null())54 };54 let result = execute_and_time(stdout, stderr, &command.get_shell_command())?;55 let result = execute_and_time(stdout, stderr, &command.get_shell_command(), shell)?;5657 let mut time_user = result.user_time;58 let mut time_system = result.system_time;59
26 unmodified lines
86}8788/// Measure the average shell spawning time89pub fn mean_shell_spawning_time(90 shell: &str,91 style: &OutputStyleOption,92 show_output: bool,93) -> io::Result<TimingResult> {94 const COUNT: u64 = 200;
5 unmodified lines
100101 for _ in 0..COUNT {102 // Just run the shell without any command103 let res = time_shell_command(104 shell,105 &Command::new(""),106 show_output,107 CmdFailureAction::RaiseError,108 None,109 );110111 match res {112 Err(_) => {113 let shell_cmd = if cfg!(windows) {114 format!("{} /C \"\"", shell)115 } else {116 format!("{} -c \"\"", shell)117 };118119 return Err(io::Error::new(111 io::ErrorKind::Other,112 "Could not measure shell execution time. \113 Make sure you can run 'sh -c \"\"'.",114 ))120 io::ErrorKind::Other,121 format!("Could not measure shell execution time. \122 Make sure you can run '{}'.", shell_cmd),123 ))124 }125 Ok((r, _)) => {126 times_real.push(r.time_real);127 times_user.push(r.time_user);
12 unmodified lines
140}141142/// Run the command specified by `--prepare`.143fn run_preparation_command(144 shell: &str,145 command: &Option<Command>,146 show_output: bool,147) -> io::Result<TimingResult> {148 if let &Some(ref cmd) = command {139 let res = time_shell_command(cmd, show_output, CmdFailureAction::RaiseError, None);149 let res = time_shell_command(shell, cmd, show_output, CmdFailureAction::RaiseError, None);150 if res.is_err() {151 return Err(io::Error::new(152 io::ErrorKind::Other,153 "The preparation command terminated with a non-zero exit code. \
34 unmodified lines
188 &options.output_style,189 );190191 for _ in 0..options.warmup_count {182 let _ = time_shell_command(cmd, options.show_output, options.failure_action, None)?;192 let _ = time_shell_command(&options.shell, cmd, options.show_output, options.failure_action, None)?;193 progress_bar.inc(1);194 }195 progress_bar.finish_and_clear();196 }
12 unmodified lines
209 .map(|preparation_command| match cmd.get_parameter() {210 Some((param, value)) => Command::new_parametrized(preparation_command, param, value),211 None => Command::new(preparation_command),212 });203 let prepare_res = run_preparation_command(&prepare_cmd, options.show_output)?;213 let prepare_res = run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?;214215 // Initial timing run216 let (res, success) = time_shell_command(217 &options.shell,218 cmd,219 options.show_output,220 options.failure_action,221 Some(shell_spawning_time),
28 unmodified lines
250 progress_bar.set_length(count_remaining);251252 // Gather statistics253 for _ in 0..count_remaining {243 run_preparation_command(&prepare_cmd, options.show_output)?;254 run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?;255256 let msg = {257 let mean = format_duration(mean(&times_real), None);258 format!("Current estimate: {}", mean.to_string().green())259 };260 progress_bar.set_message(&msg);261262 let (res, success) = time_shell_command(263 &options.shell,264 cmd,265 options.show_output,266 options.failure_action,267 Some(shell_spawning_time),
90 unmodified lines
modified:src/hyperfine/shell.rs+10 added-8 removed
17 unmodified lines
18}1920/// Execute the given command and return a timing summary21#[cfg(windows)]22pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str) -> io::Result<ExecuteResult> {23 let mut child = run_shell_command(stdout, stderr, command)?;22pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str, shell: &str) -> io::Result<ExecuteResult> {23 let mut child = run_shell_command(stdout, stderr, command, shell)?;24 let cpu_timer = get_cpu_timer(&child);25 let status = child.wait()?;2627 let (user_time, system_time) = cpu_timer.stop();
5 unmodified lines
33}3435/// Execute the given command and return a timing summary36#[cfg(not(windows))]37pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str) -> io::Result<ExecuteResult> {37pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str, shell: &str) -> io::Result<ExecuteResult> {38 let cpu_timer = get_cpu_timer();3940 let status = run_shell_command(stdout, stderr, command)?;40 let status = run_shell_command(stdout, stderr, command, shell)?;4142 let (user_time, system_time) = cpu_timer.stop();4344 Ok(ExecuteResult {
2 unmodified lines
47 status,48 })49}5051/// Run a standard shell command51/// Run a standard shell command using `sh -c`52#[cfg(not(windows))]53fn run_shell_command(54 stdout: Stdio,55 stderr: Stdio,56 command: &str,57 shell: &str,58) -> io::Result<std::process::ExitStatus> {58 Command::new("sh")59 Command::new(shell)60 .arg("-c")61 .arg(command)62 .stdin(Stdio::null())63 .stdout(stdout)64 .stderr(stderr)65 .status()66}6767/// Run a Windows shell command using cmd.exe68/// Run a Windows shell command using `cmd.exe /C`69#[cfg(windows)]70fn run_shell_command(71 stdout: Stdio,72 stderr: Stdio,73 command: &str,74 shell: &str,75) -> io::Result<std::process::Child> {74 Command::new("cmd.exe")76 Command::new(shell)77 .arg("/C")78 .arg(command)79 .stdin(Stdio::null())80 .stdout(stdout)
3 unmodified lines
modified:src/hyperfine/types.rs+10 added
1/// This module contains common internal types.2use std::fmt;34#[cfg(not(windows))]5pub const DEFAULT_SHELL: &str = "sh";67#[cfg(windows)]8pub const DEFAULT_SHELL: &str = "cmd.exe";910/// Type alias for unit of time11pub type Second = f64;1213/// A command that should be benchmarked.
102 unmodified lines
116117 /// What color mode to use for output118 pub output_style: OutputStyleOption,119120 /// The shell to use for executing commands.121 pub shell: String,122123 /// Forward benchmark's stdout to hyperfine's stdout124 pub show_output: bool,125}126
5 unmodified lines
132 min_time_sec: 3.0,133 failure_action: CmdFailureAction::RaiseError,134 preparation_command: None,135 output_style: OutputStyleOption::Full,136 shell: DEFAULT_SHELL.to_string(),137 show_output: false,138 }139 }140}
54 unmodified lines
modified:src/main.rs+5 added-3 removed
55 unmodified lines
56}5758/// Runs the benchmark for the given commands59fn run(commands: &Vec<Command>, options: &HyperfineOptions) -> io::Result<Vec<BenchmarkResult>> {60 let shell_spawning_time = mean_shell_spawning_time(&options.output_style, options.show_output)?;60 let shell_spawning_time = mean_shell_spawning_time(&options.shell, &options.output_style, options.show_output)?;6162 let mut timing_results = vec![];6364 // Run the benchmarks
57 unmodified lines
122123 options.warmup_count = matches124 .value_of("warmup")125 .and_then(&str_to_u64)126 .unwrap_or(0);126 .unwrap_or(options.warmup_count);127128 let mut min_runs = matches.value_of("min-runs").and_then(&str_to_u64);129 let mut max_runs = matches.value_of("max-runs").and_then(&str_to_u64);130
56 unmodified lines
187 OutputStyleOption::Basic | OutputStyleOption::NoColor => {188 colored::control::set_override(false)189 }190 _ => {}191 }191 };192193 options.shell = matches.value_of("shell").unwrap_or(&options.shell).to_string();194195 if matches.is_present("ignore-failure") {196 options.failure_action = CmdFailureAction::Ignore;197 }198
42 unmodified lines