Skip to content
Myr

Make --shell=none the default (#982)

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

authored by David Peter · committed by GitHub · parent ef86c83 · Browse files · 7 files+93 added−51 removed
This commit

State

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

Diff stats

7 files+93 added−51 removed

  • tests:+18 added−1 removed
  • other:+75 added−50 removed

7 files+93 added−51 removed

modified:doc/hyperfine.1+13 added-7 removed
1 unmodified line
2.SH NAME3hyperfine \- command\-line benchmarking tool4.SH SYNOPSIS5.B hyperfine6.RB [ \-hVN ]6.RB [ \-hVNS ]7.RB [ \-\-ignore\-failure[=MODE] ]8.RB [ \-\-warmup9.IR NUM ]10.RB [ \-\-min\-runs
160 unmodified lines
171.IP172.RS173Example:174.RS175\fBhyperfine\fR \fB\-P\fR size 0 3 'sleep $((2**{size}))'175\fBhyperfine\fR \fB\-\-shell=bash\fR \fB\-P\fR size 0 3 'sleep $((2**{size}))'176.RE177.RE178.IP179This performs benchmarks with power of 2 increases: 'sleep 1', 'sleep
31 unmodified lines
211.IP212The option can be specified multiple times to run benchmarks for all213possible parameter combinations.214.HP215\fB\-S\fR, \fB\-\-shell\fR \fISHELL\fP215\fB\-\-shell\fR \fISHELL\fP216.IP217Set the shell to use for executing benchmarked commands. This can be217Set the shell to use for executing commands (default: none). This can be218the name or the path to the shell executable, or a full command line219like "bash \fB\-\-norc\fR". It can also be set to "default" to explicitly220select the default shell on this platform. Finally, this can also be220select the platform shell (sh on Unix, cmd.exe on Windows). Finally, this can also be221set to "none" to disable the shell. In this case, commands will be222executed directly. They can still have arguments, but more complex223things like "sleep 0.1; sleep 0.2" are not possible without a shell.224This option also applies to setup, prepare, conclude, and cleanup commands.225.HP226\fB\-S\fR227.IP228An alias for '\-\-shell=default' (sh on Unix, cmd.exe on Windows).229.HP230\fB\-N\fR231.IP232An alias for '\-\-shell=none'.233.HP
101 unmodified lines
335.IP336This option can be specified once for all commands or multiple times,337once for each command. Note: If you want to log the output of each and338every iteration, you can use a shell redirection and the $HYPERFINE_ITERATION334environment variable: 'my-command > output-${HYPERFINE_ITERATION}.log'339environment variable (requires \fB\-S\fR / \fB\-\-shell=default\fR):340'my-command > output-${HYPERFINE_ITERATION}.log'341.HP342\fB\-\-input\fR \fIWHERE\fP343.IP344Control where the input of the benchmark comes from.
37 unmodified lines
382Perform a benchmark of 'grep' with a warm disk cache by executing 3 runs up front that are not part383of the measurement: 384.RS385.nf380\fBhyperfine\fR \fB\-\-warmup\fR 3 'grep -R TODO *'386\fBhyperfine\fR \fB\-S\fR \fB\-\-warmup\fR 3 'grep -R TODO *'387.fi388.RE389.LP390Export the results of a parameter scan benchmark to a markdown table:
23 unmodified lines
modified:src/cli.rs+15 added-8 removed
34 unmodified lines
35 .arg(36 Arg::new("command")37 .help("The command to benchmark. This can be the name of an executable, a command \38 line like \"grep -i todo\" or a shell command like \"sleep 0.5 && echo test\". \39 The latter is only available if the shell is not explicitly disabled via \40 '--shell=none'. If multiple commands are given, hyperfine will show a \39 The latter requires a shell, which can be enabled via '-S' or '--shell=...'. \40 If multiple commands are given, hyperfine will show a \41 comparison of the respective runtimes.")42 .required(true)43 .action(ArgAction::Append)44 .value_hint(ValueHint::CommandString)
135 unmodified lines
180 string '{VAR}' in each command by the current parameter value.\n\n \181 Example: hyperfine --prepare 'make clean' -P threads 1 8 'make -j {threads}'\n\n\182 This performs benchmarks for 'make -j 1', 'make -j 2', …, 'make -j 8'.\n\n\183 To have the value increase following different patterns, use shell arithmetic.\n\n \184 Example: hyperfine -P size 0 3 'sleep $((2**{size}))'\n\n\184 Example: hyperfine --shell=bash -P size 0 3 'sleep $((2**{size}))'\n\n\185 This performs benchmarks with power of 2 increases: 'sleep 1', 'sleep 2', 'sleep 4', …\n\186 The exact syntax may vary depending on your shell and OS."187 ),188 )
30 unmodified lines
219 )220 .arg(221 Arg::new("shell")222 .long("shell")223 .short('S')223 .action(ArgAction::Set)224 .value_name("SHELL")225 .overrides_with("shell")226 .value_hint(ValueHint::CommandString)228 .help("Set the shell to use for executing benchmarked commands. This can be the \227 .help("Set the shell to use for executing commands (default: none). This can be the \228 name or the path to the shell executable, or a full command line \229 like \"bash --norc\". It can also be set to \"default\" to explicitly select \231 the default shell on this platform. Finally, this can also be set to \230 the platform shell (sh on Unix, cmd.exe on Windows). It can also be set to \231 \"none\" to disable the shell. In this case, commands will be executed \232 directly. They can still have arguments, but more complex things like \234 \"sleep 0.1; sleep 0.2\" are not possible without a shell.")233 \"sleep 0.1; sleep 0.2\" are not possible without a shell. This option also \234 applies to setup, prepare, conclude, and cleanup commands.")235 )236 .arg(237 Arg::new("default-shell")238 .short('S')239 .action(ArgAction::SetTrue)240 .conflicts_with_all(["shell", "no-shell", "debug-mode"])241 .help("An alias for '--shell=default' (sh on Unix, cmd.exe on Windows).")242 )243 .arg(244 Arg::new("no-shell")245 .short('N')246 .action(ArgAction::SetTrue)247 .conflicts_with_all(["shell", "debug-mode"])
141 unmodified lines
389 <FILE>: Write the output to the given file.\n\n\390 This option can be specified once for all commands or multiple times, once for \391 each command. Note: If you want to log the output of each and every iteration, \392 you can use a shell redirection and the '$HYPERFINE_ITERATION' environment variable:\n \386 hyperfine 'my-command > output-${HYPERFINE_ITERATION}.log'\n\n",393 hyperfine -S 'my-command > output-${HYPERFINE_ITERATION}.log'\n\n",394 ),395 )396 .arg(397 Arg::new("input")
46 unmodified lines
modified:src/options.rs+15 added-17 removed
27 unmodified lines
28 /// Custom shell command specified via --shell29 Custom(Vec<String>),30}3132impl Default for Shell {33 fn default() -> Self {34 Shell::Default(DEFAULT_SHELL)35 }36}3732impl fmt::Display for Shell {33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {34 match self {35 Shell::Default(cmd) => write!(f, "{cmd}"),
2 unmodified lines
38 }39}4041impl Shell {42 /// Select the platform shell (sh on Unix, cmd.exe on Windows).43 pub fn platform_default() -> Self {44 Shell::Default(DEFAULT_SHELL)45 }4647 /// Parse given string as shell command line48 pub fn parse_from_str<'a>(s: &str) -> Result<Self, OptionsError<'a>> {49 let v = shell_words::split(s).map_err(OptionsError::ShellParseError)?;50 if v.is_empty() || v[0].is_empty() {
128 unmodified lines
179 Ok(streams)180 }181}182184#[derive(Debug, PartialEq)]183#[derive(Debug, Default, PartialEq)]184pub enum ExecutorKind {185 #[default]186 Raw,187 Shell(Shell),188 Mock(Option<String>),189}190191impl Default for ExecutorKind {192 fn default() -> Self {193 ExecutorKind::Shell(Shell::default())194 }195}196191/// The main settings for a hyperfine benchmark session192pub struct Options {193 /// Upper and lower bound for the number of benchmark runs194 pub run_bounds: RunBounds,
199 unmodified lines
394 };395396 options.executor_kind = if matches.get_flag("no-shell") {397 ExecutorKind::Raw398 } else if matches.get_flag("default-shell") {399 ExecutorKind::Shell(Shell::platform_default())400 } else {401 match (402 matches.get_flag("debug-mode"),403 matches.get_one::<String>("shell"),404 ) {409 (false, Some(shell)) if shell == "default" => ExecutorKind::Shell(Shell::default()),405 (false, Some(shell)) if shell == "default" => {406 ExecutorKind::Shell(Shell::platform_default())407 }408 (false, Some(shell)) if shell == "none" => ExecutorKind::Raw,409 (false, Some(shell)) => ExecutorKind::Shell(Shell::parse_from_str(shell)?),412 (false, None) => ExecutorKind::Shell(Shell::default()),410 (false, None) => ExecutorKind::default(),411 (true, Some(shell)) => ExecutorKind::Mock(Some(shell.into())),412 (true, None) => ExecutorKind::Mock(None),413 }414 };
114 unmodified lines
529 }530}531532#[test]535fn test_default_shell() {536 let shell = Shell::default();533fn test_platform_default_shell() {534 let shell = Shell::platform_default();535536 let s = format!("{shell}");537 assert_eq!(&s, DEFAULT_SHELL);538
36 unmodified lines
modified:tests/execution_order_tests.rs+3 added-1 removed
15 unmodified lines
16impl ExecutionOrderTest {17 fn new() -> Self {18 let tempdir = tempdir().unwrap();19 let logfile_path = tempdir.path().join("output.log");20 let mut cmd = hyperfine();21 cmd.arg("-S");2223 ExecutionOrderTest {22 cmd: hyperfine(),24 cmd,25 expected_content: String::new(),26 logfile_path,27 tempdir,28 }
517 unmodified lines
modified:tests/integration_tests.rs+15 added
257 unmodified lines
258259#[test]260fn fails_for_unknown_command() {261 hyperfine()262 .arg("--shell=default")263 .arg("--runs=1")264 .arg("some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")265 .assert()266 .failure()
31 unmodified lines
298299#[test]300fn fails_for_unknown_setup_command() {301 hyperfine()302 .arg("--shell=default")303 .arg("--runs=1")304 .arg("--setup=some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")305 .arg("echo test")306 .assert()
5 unmodified lines
312313#[test]314fn fails_for_unknown_cleanup_command() {315 hyperfine()316 .arg("--shell=default")317 .arg("--runs=1")318 .arg("--cleanup=some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")319 .arg("echo test")320 .assert()
5 unmodified lines
326327#[test]328fn fails_for_unknown_prepare_command() {329 hyperfine()330 .arg("--shell=default")331 .arg("--prepare=some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")332 .arg("echo test")333 .assert()334 .failure()
4 unmodified lines
339340#[test]341fn fails_for_unknown_conclude_command() {342 hyperfine()343 .arg("--shell=default")344 .arg("--conclude=some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")345 .arg("echo test")346 .assert()347 .failure()
25 unmodified lines
373#[test]374fn can_ignore_specific_exit_codes() {375 // Test that specifying exit code 1 ignores it376 hyperfine()377 .arg("--shell=default")378 .arg("--runs=1")379 .arg("--ignore-failure=1")380 .arg("exit 1")381 .assert()382 .success();383384 // Test that other exit codes still fail385 hyperfine()386 .arg("--shell=default")387 .arg("--runs=1")388 .arg("--ignore-failure=1")389 .arg("exit 2")390 .assert()
7 unmodified lines
398#[test]399fn can_ignore_multiple_exit_codes() {400 // Test that all specified exit codes are ignored401 hyperfine()402 .arg("--shell=default")403 .arg("--runs=1")404 .arg("--ignore-failure=1,2,3")405 .arg("exit 1")406 .assert()407 .success();408409 hyperfine()410 .arg("--shell=default")411 .arg("--runs=1")412 .arg("--ignore-failure=1,2,3")413 .arg("exit 2")414 .assert()415 .success();416417 hyperfine()418 .arg("--shell=default")419 .arg("--runs=1")420 .arg("--ignore-failure=1,2,3")421 .arg("exit 3")422 .assert()423 .success();424425 // Test that other exit codes still fail426 hyperfine()427 .arg("--shell=default")428 .arg("--runs=1")429 .arg("--ignore-failure=1,2,3")430 .arg("exit 4")431 .assert()
7 unmodified lines
439#[test]440fn ignore_failure_with_all_non_zero() {441 // Test explicit "all-non-zero" mode442 hyperfine()443 .arg("--shell=default")444 .arg("--runs=1")445 .arg("--ignore-failure=all-non-zero")446 .arg("exit 5")447 .assert()
792 unmodified lines
1240#[cfg(windows)]1241#[test]1242fn windows_quote_args() {1243 hyperfine()1244 .arg("--shell=default")1245 .arg("more \"example_input_file.txt\"")1246 .assert()1247 .success();1248}
1 unmodified line
1250#[cfg(windows)]1251#[test]1252fn windows_quote_before_quote_args() {1253 hyperfine()1254 .arg("--shell=default")1255 .arg("dir \"..\\src\\\" \"..\\tests\\\"")1256 .assert()1257 .success();1258}
9 unmodified lines
1268 };1269 let command = |phase| format!(r#"echo {phase}:{iteration} >> "iteration output.txt""#);12701271 hyperfine()1272 .arg("--shell=default")1273 .current_dir(tempdir.path())1274 .arg("--runs=2")1275 .arg("--warmup=1")1276 .arg("--prepare")
68 unmodified lines
modified:CHANGELOG.md+7 added
1# Unreleased23## Breaking changes45- Execute commands directly by default (`--shell=none`). Use `-S` (an alias for `--shell=default`) to restore the previous behavior (`sh` on Unix, `cmd.exe` on Windows), or select a shell with `--shell <SHELL>`.6- Repurpose `-S` as an alias for `--shell=default`. It no longer accepts a shell argument; replace `-S <SHELL>` with `--shell <SHELL>`.78# v1.21.0910## Features11
452 unmodified lines
modified:README.md+25 added-18 removed
24 unmodified lines
25## Usage2627### Basic benchmarks2829To run a benchmark, you can simply call `hyperfine <command>...`. The argument(s) can be any30shell command. For example:29To run a benchmark, you can simply call `hyperfine <command>...`. Each argument is an executable30and its arguments. Commands run directly by default; use `-S` for shell syntax such as31pipes, redirections, and wildcards. For example:32```sh33hyperfine 'sleep 0.3'34```35
17 unmodified lines
5354If you want to run the benchmark on a warm cache, you can use the `-w`/`--warmup` option to55perform a certain number of program executions before the actual benchmark:56```sh56hyperfine --warmup 3 'grep -R TODO *'57hyperfine -S --warmup 3 'grep -R TODO *'58```5960Conversely, if you want to run the benchmark for a cold cache, you can use the `-p`/`--prepare`61option to run a special command before *each* timing run. For example, to clear Linux filesystem caches,
3 unmodified lines
65```66To use this specific command with hyperfine, call `sudo -v` to temporarily gain sudo permissions67and then call:68```sh68hyperfine --prepare 'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches' 'grep -R TODO *'69hyperfine -S --prepare 'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches' 'grep -R TODO *'70```7172### Parameterized benchmarks73
27 unmodified lines
101102If you need a unique value for each individual run of a benchmark command, hyperfine also exposes103the zero-based `$HYPERFINE_ITERATION` environment variable inside the benchmarked command itself:104```sh104hyperfine 'my-command > output-${HYPERFINE_ITERATION}.log'105hyperfine -S 'my-command > output-${HYPERFINE_ITERATION}.log'106```107108### Intermediate shell109109By default, commands are executed using `sh` on Unix (resolved through `PATH`) or `cmd.exe` on Windows.110If you want to use a different shell, you can use the `-S, --shell <SHELL>` option:110By default, commands are executed directly, without an intermediate shell (`--shell=none`).111Arguments are split using shell-like quoting, so quoted arguments containing spaces are supported.112Shell syntax such as pipes, redirections, environment-variable expansion, `*`, and `~` is not interpreted.113This avoids shell startup overhead and the noise from correcting for it, especially for fast commands114(< 5 ms).115116To enable shell syntax, use `-S` (an alias for `--shell=default`). This selects `sh`117on Unix (resolved through `PATH`) or `cmd.exe` on Windows:118```sh119hyperfine -S 'sleep 0.1 && echo done'120```121122You can also select a specific shell with `--shell <SHELL>`:123```sh124hyperfine --shell zsh 'for i in {1..10000}; do echo test; done'125```126115Note that hyperfine always *corrects for the shell spawning time*. To do this, it performs a calibration116procedure where it runs the shell with an empty command (multiple times), to measure the startup time117of the shell. It will then subtract this time from the total to show the actual time used by the command118in question.127The shell setting applies to all commands, including `--setup`, `--prepare`, `--conclude`, and `--cleanup`.128If any of these commands need shell syntax, enable a shell explicitly.129120If you want to run a benchmark *without an intermediate shell*, you can use the `-N` or `--shell=none`121option. This is helpful for very fast commands (< 5 ms) where the shell startup overhead correction would122produce a significant amount of noise. Note that you cannot use shell syntax like `*` or `~` in this case.123```124hyperfine -N 'grep -R TODO /home/user'125```130When a shell is enabled, hyperfine *corrects for the shell spawning time*. It runs the shell with an131empty command multiple times to measure its startup time, then subtracts this time from each132measurement.133134135### Shell functions136
7 unmodified lines
144145Otherwise, inline the function into the benchmarked command:146147```sh141hyperfine 'my_function() { sleep 1; }; my_function'148hyperfine -S 'my_function() { sleep 1; }; my_function'149```150151### Exporting results152
231 unmodified lines