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
Provenance
- Commit
ab86cb9- Tree
a505445- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
ef86c83
7 files changed+93 added−51 removed
modified:doc/hyperfine.1+13 added-7 removed
1 unmodified line2 .SH NAME3 hyperfine \- 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\-runs160 unmodified lines171 .IP172 .RS173 Example: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 .IP179 This performs benchmarks with power of 2 increases: 'sleep 1', 'sleep31 unmodified lines211 .IP212 The option can be specified multiple times to run benchmarks for all213 possible parameter combinations.214 .HP215-\fB\-S\fR, \fB\-\-shell\fR \fISHELL\fP215+\fB\-\-shell\fR \fISHELL\fP216 .IP217-Set the shell to use for executing benchmarked commands. This can be217+Set the shell to use for executing commands (default: none). This can be218 the name or the path to the shell executable, or a full command line219 like "bash \fB\-\-norc\fR". It can also be set to "default" to explicitly220-select the default shell on this platform. Finally, this can also be220+select the platform shell (sh on Unix, cmd.exe on Windows). Finally, this can also be221 set to "none" to disable the shell. In this case, commands will be222 executed directly. They can still have arguments, but more complex223 things like "sleep 0.1; sleep 0.2" are not possible without a shell.224+This option also applies to setup, prepare, conclude, and cleanup commands.225 .HP226+\fB\-S\fR227+.IP228+An alias for '\-\-shell=default' (sh on Unix, cmd.exe on Windows).229+.HP230 \fB\-N\fR231 .IP232 An alias for '\-\-shell=none'.233 .HP101 unmodified lines335 .IP336 This option can be specified once for all commands or multiple times,337 once for each command. Note: If you want to log the output of each and338 every iteration, you can use a shell redirection and the $HYPERFINE_ITERATION334-environment variable: 'my-command > output-${HYPERFINE_ITERATION}.log'339+environment variable (requires \fB\-S\fR / \fB\-\-shell=default\fR):340+'my-command > output-${HYPERFINE_ITERATION}.log'341 .HP342 \fB\-\-input\fR \fIWHERE\fP343 .IP344 Control where the input of the benchmark comes from.37 unmodified lines382 Perform a benchmark of 'grep' with a warm disk cache by executing 3 runs up front that are not part383 of 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 .LP390 Export the results of a parameter scan benchmark to a markdown table: 23 unmodified linesmodified:src/cli.rs+15 added-8 removed
34 unmodified lines35 .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 lines180 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 lines219 )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 lines389 <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 linesmodified:src/options.rs+15 added-17 removed
27 unmodified lines28 /// Custom shell command specified via --shell29 Custom(Vec<String>),30 }31 32-impl Default for Shell {33- fn default() -> Self {34- Shell::Default(DEFAULT_SHELL)35- }36-}37-32 impl 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 lines38 }39 }40 41 impl 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+ }46+47 /// 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 lines179 Ok(streams)180 }181 }182 184-#[derive(Debug, PartialEq)]183+#[derive(Debug, Default, PartialEq)]184 pub enum ExecutorKind {185+ #[default]186 Raw,187 Shell(Shell),188 Mock(Option<String>),189 }190 191-impl Default for ExecutorKind {192- fn default() -> Self {193- ExecutorKind::Shell(Shell::default())194- }195-}196-191 /// The main settings for a hyperfine benchmark session192 pub struct Options {193 /// Upper and lower bound for the number of benchmark runs194 pub run_bounds: RunBounds,199 unmodified lines394 };395 396 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 lines529 }530 }531 532 #[test]535-fn test_default_shell() {536- let shell = Shell::default();533+fn test_platform_default_shell() {534+ let shell = Shell::platform_default();535 536 let s = format!("{shell}");537 assert_eq!(&s, DEFAULT_SHELL);538 36 unmodified linesmodified:tests/execution_order_tests.rs+3 added-1 removed
15 unmodified lines16 impl 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");22 23 ExecutionOrderTest {22- cmd: hyperfine(),24+ cmd,25 expected_content: String::new(),26 logfile_path,27 tempdir,28 }517 unmodified linesmodified:tests/integration_tests.rs+15 added
257 unmodified lines258 259 #[test]260 fn 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 lines298 299 #[test]300 fn 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 lines312 313 #[test]314 fn 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 lines326 327 #[test]328 fn 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 lines339 340 #[test]341 fn 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 lines373 #[test]374 fn 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();383 384 // 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 lines398 #[test]399 fn 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();408 409 hyperfine()410+ .arg("--shell=default")411 .arg("--runs=1")412 .arg("--ignore-failure=1,2,3")413 .arg("exit 2")414 .assert()415 .success();416 417 hyperfine()418+ .arg("--shell=default")419 .arg("--runs=1")420 .arg("--ignore-failure=1,2,3")421 .arg("exit 3")422 .assert()423 .success();424 425 // 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 lines439 #[test]440 fn 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 lines1240 #[cfg(windows)]1241 #[test]1242 fn windows_quote_args() {1243 hyperfine()1244+ .arg("--shell=default")1245 .arg("more \"example_input_file.txt\"")1246 .assert()1247 .success();1248 }1 unmodified line1250 #[cfg(windows)]1251 #[test]1252 fn windows_quote_before_quote_args() {1253 hyperfine()1254+ .arg("--shell=default")1255 .arg("dir \"..\\src\\\" \"..\\tests\\\"")1256 .assert()1257 .success();1258 }9 unmodified lines1268 };1269 let command = |phase| format!(r#"echo {phase}:{iteration} >> "iteration output.txt""#);1270 1271 hyperfine()1272+ .arg("--shell=default")1273 .current_dir(tempdir.path())1274 .arg("--runs=2")1275 .arg("--warmup=1")1276 .arg("--prepare")68 unmodified linesmodified:CHANGELOG.md+7 added
1+# Unreleased2+3+## Breaking changes4+5+- 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>`.7+8 # v1.21.09 10 ## Features11 452 unmodified linesmodified:README.md+25 added-18 removed
24 unmodified lines25 ## Usage26 27 ### Basic benchmarks28 29-To run a benchmark, you can simply call `hyperfine <command>...`. The argument(s) can be any30-shell command. For example:29+To run a benchmark, you can simply call `hyperfine <command>...`. Each argument is an executable30+and its arguments. Commands run directly by default; use `-S` for shell syntax such as31+pipes, redirections, and wildcards. For example:32 ```sh33 hyperfine 'sleep 0.3'34 ```35 17 unmodified lines53 54 If you want to run the benchmark on a warm cache, you can use the `-w`/`--warmup` option to55 perform a certain number of program executions before the actual benchmark:56 ```sh56-hyperfine --warmup 3 'grep -R TODO *'57+hyperfine -S --warmup 3 'grep -R TODO *'58 ```59 60 Conversely, if you want to run the benchmark for a cold cache, you can use the `-p`/`--prepare`61 option to run a special command before *each* timing run. For example, to clear Linux filesystem caches,3 unmodified lines65 ```66 To use this specific command with hyperfine, call `sudo -v` to temporarily gain sudo permissions67 and then call:68 ```sh68-hyperfine --prepare 'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches' 'grep -R TODO *'69+hyperfine -S --prepare 'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches' 'grep -R TODO *'70 ```71 72 ### Parameterized benchmarks73 27 unmodified lines101 102 If you need a unique value for each individual run of a benchmark command, hyperfine also exposes103 the zero-based `$HYPERFINE_ITERATION` environment variable inside the benchmarked command itself:104 ```sh104-hyperfine 'my-command > output-${HYPERFINE_ITERATION}.log'105+hyperfine -S 'my-command > output-${HYPERFINE_ITERATION}.log'106 ```107 108 ### Intermediate shell109 109-By default, commands are executed using `sh` on Unix (resolved through `PATH`) or `cmd.exe` on Windows.110-If you want to use a different shell, you can use the `-S, --shell <SHELL>` option:110+By default, commands are executed directly, without an intermediate shell (`--shell=none`).111+Arguments are split using shell-like quoting, so quoted arguments containing spaces are supported.112+Shell syntax such as pipes, redirections, environment-variable expansion, `*`, and `~` is not interpreted.113+This avoids shell startup overhead and the noise from correcting for it, especially for fast commands114+(< 5 ms).115+116+To enable shell syntax, use `-S` (an alias for `--shell=default`). This selects `sh`117+on Unix (resolved through `PATH`) or `cmd.exe` on Windows:118 ```sh119+hyperfine -S 'sleep 0.1 && echo done'120+```121+122+You can also select a specific shell with `--shell <SHELL>`:123+```sh124 hyperfine --shell zsh 'for i in {1..10000}; do echo test; done'125 ```126 115-Note that hyperfine always *corrects for the shell spawning time*. To do this, it performs a calibration116-procedure where it runs the shell with an empty command (multiple times), to measure the startup time117-of the shell. It will then subtract this time from the total to show the actual time used by the command118-in question.127+The shell setting applies to all commands, including `--setup`, `--prepare`, `--conclude`, and `--cleanup`.128+If any of these commands need shell syntax, enable a shell explicitly.129 120-If you want to run a benchmark *without an intermediate shell*, you can use the `-N` or `--shell=none`121-option. This is helpful for very fast commands (< 5 ms) where the shell startup overhead correction would122-produce a significant amount of noise. Note that you cannot use shell syntax like `*` or `~` in this case.123-```124-hyperfine -N 'grep -R TODO /home/user'125-```130+When a shell is enabled, hyperfine *corrects for the shell spawning time*. It runs the shell with an131+empty command multiple times to measure its startup time, then subtracts this time from each132+measurement.133 134 135 ### Shell functions136 7 unmodified lines144 145 Otherwise, inline the function into the benchmarked command:146 147 ```sh141-hyperfine 'my_function() { sleep 1; }; my_function'148+hyperfine -S 'my_function() { sleep 1; }; my_function'149 ```150 151 ### Exporting results152 231 unmodified lines