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
Provenance
- Commit
01581f6- Tree
817452d- Author
Jason Peacock <jason@jasonpeacock.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
0b18446
5 files changed+55 added−20 removed
modified:src/hyperfine/app.rs+9 added
108 unmodified lines109 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 linesmodified:src/hyperfine/benchmark.rs+21 added-9 removed
37 unmodified lines38 }39 40 /// Run the given shell command and measure the execution time41 pub 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 lines51 (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)?;56 57 let mut time_user = result.user_time;58 let mut time_system = result.system_time;59 26 unmodified lines86 }87 88 /// Measure the average shell spawning time89 pub 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 lines100 101 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 );110 111 match res {112 Err(_) => {113+ let shell_cmd = if cfg!(windows) {114+ format!("{} /C \"\"", shell)115+ } else {116+ format!("{} -c \"\"", shell)117+ };118+119 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 lines140 }141 142 /// Run the command specified by `--prepare`.143 fn 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 lines188 &options.output_style,189 );190 191 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 lines209 .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)?;214 215 // 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 lines250 progress_bar.set_length(count_remaining);251 252 // 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)?;255 256 let msg = {257 let mean = format_duration(mean(×_real), None);258 format!("Current estimate: {}", mean.to_string().green())259 };260 progress_bar.set_message(&msg);261 262 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 linesmodified:src/hyperfine/shell.rs+10 added-8 removed
17 unmodified lines18 }19 20 /// Execute the given command and return a timing summary21 #[cfg(windows)]22-pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str) -> io::Result<ExecuteResult> {23- let mut child = run_shell_command(stdout, stderr, command)?;22+pub 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()?;26 27 let (user_time, system_time) = cpu_timer.stop();5 unmodified lines33 }34 35 /// Execute the given command and return a timing summary36 #[cfg(not(windows))]37-pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str) -> io::Result<ExecuteResult> {37+pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str, shell: &str) -> io::Result<ExecuteResult> {38 let cpu_timer = get_cpu_timer();39 40- let status = run_shell_command(stdout, stderr, command)?;40+ let status = run_shell_command(stdout, stderr, command, shell)?;41 42 let (user_time, system_time) = cpu_timer.stop();43 44 Ok(ExecuteResult {2 unmodified lines47 status,48 })49 }50 51-/// Run a standard shell command51+/// Run a standard shell command using `sh -c`52 #[cfg(not(windows))]53 fn 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 }67 67-/// Run a Windows shell command using cmd.exe68+/// Run a Windows shell command using `cmd.exe /C`69 #[cfg(windows)]70 fn 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 linesmodified:src/hyperfine/types.rs+10 added
1 /// This module contains common internal types.2 use std::fmt;3 4+#[cfg(not(windows))]5+pub const DEFAULT_SHELL: &str = "sh";6+7+#[cfg(windows)]8+pub const DEFAULT_SHELL: &str = "cmd.exe";9+10 /// Type alias for unit of time11 pub type Second = f64;12 13 /// A command that should be benchmarked.102 unmodified lines116 117 /// What color mode to use for output118 pub output_style: OutputStyleOption,119 120+ /// The shell to use for executing commands.121+ pub shell: String,122+123 /// Forward benchmark's stdout to hyperfine's stdout124 pub show_output: bool,125 }126 5 unmodified lines132 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 linesmodified:src/main.rs+5 added-3 removed
55 unmodified lines56 }57 58 /// Runs the benchmark for the given commands59 fn 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)?;61 62 let mut timing_results = vec![];63 64 // Run the benchmarks57 unmodified lines122 123 options.warmup_count = matches124 .value_of("warmup")125 .and_then(&str_to_u64)126- .unwrap_or(0);126+ .unwrap_or(options.warmup_count);127 128 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 lines187 OutputStyleOption::Basic | OutputStyleOption::NoColor => {188 colored::control::set_override(false)189 }190 _ => {}191- }191+ };192 193+ options.shell = matches.value_of("shell").unwrap_or(&options.shell).to_string();194+195 if matches.is_present("ignore-failure") {196 options.failure_action = CmdFailureAction::Ignore;197 }198 42 unmodified lines