Files
hyperfine/src/cli.rs
T

341 lines
15 KiB
Rust
Raw Normal View History

2021-08-24 21:33:48 +02:00
use std::ffi::OsString;
2022-10-28 11:23:03 +02:00
use clap::{
builder::NonEmptyStringValueParser, crate_version, Arg, ArgAction, ArgMatches, Command,
};
2018-03-24 18:30:52 -04:00
2022-02-06 21:02:15 +01:00
pub fn get_cli_arguments<'a, I, T>(args: I) -> ArgMatches
2018-03-24 18:30:52 -04:00
where
I: IntoIterator<Item = T>,
2022-02-03 20:50:39 +01:00
T: Into<OsString> + Clone + 'a,
2018-03-24 18:30:52 -04:00
{
2022-02-22 14:04:07 +01:00
let command = build_command();
command.get_matches_from(args)
2018-03-24 18:30:52 -04:00
}
2022-02-22 14:04:07 +01:00
/// Build the clap command for parsing command line arguments
2022-10-28 09:50:35 +02:00
fn build_command() -> Command {
2022-02-22 14:04:07 +01:00
Command::new("hyperfine")
2018-03-24 18:30:52 -04:00
.version(crate_version!())
2022-02-22 14:04:07 +01:00
.next_line_help(true)
.hide_possible_values(true)
2018-03-24 18:30:52 -04:00
.about("A command-line benchmarking tool.")
2022-10-28 11:17:16 +02:00
.help_expected(true)
2023-03-21 20:51:19 +01:00
.max_term_width(80)
2018-03-24 18:30:52 -04:00
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("command")
.help("The command to benchmark. This can be the name of an executable, a command \
line like \"grep -i todo\" or a shell command like \"sleep 0.5 && echo test\". \
The latter is only available if the shell is not explicitly disabled via \
'--shell=none'. If multiple commands are given, hyperfine will show a \
comparison of the respective runtimes.")
2018-03-24 18:30:52 -04:00
.required(true)
2022-10-28 09:50:35 +02:00
.action(ArgAction::Append)
2022-10-27 20:42:33 +02:00
.value_parser(NonEmptyStringValueParser::new()),
2018-03-24 18:30:52 -04:00
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("warmup")
2018-03-24 18:30:52 -04:00
.long("warmup")
2022-02-03 20:50:39 +01:00
.short('w')
2018-03-24 18:30:52 -04:00
.value_name("NUM")
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
2018-03-24 18:30:52 -04:00
.help(
"Perform NUM warmup runs before the actual benchmark. This can be used \
to fill (disk) caches for I/O-heavy programs.",
),
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("min-runs")
2018-03-24 18:30:52 -04:00
.long("min-runs")
2022-02-03 20:50:39 +01:00
.short('m')
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
2018-03-24 18:30:52 -04:00
.value_name("NUM")
.help("Perform at least NUM runs for each command (default: 10)."),
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("max-runs")
.long("max-runs")
2022-02-03 20:50:39 +01:00
.short('M')
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
.value_name("NUM")
2019-10-13 16:24:29 +02:00
.help("Perform at most NUM runs for each command. By default, there is no limit."),
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("runs")
.long("runs")
2023-03-15 09:49:43 +01:00
.conflicts_with_all(["max-runs", "min-runs"])
2022-02-03 20:50:39 +01:00
.short('r')
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
.value_name("NUM")
2019-10-13 16:24:29 +02:00
.help("Perform exactly NUM runs for each command. If this option is not specified, \
hyperfine automatically determines the number of runs."),
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("setup")
.long("setup")
2022-02-03 20:50:39 +01:00
.short('s')
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
.value_name("CMD")
.help(
"Execute CMD before each set of timing runs. This is useful for \
compiling your software with the provided parameters, or to do any \
2022-02-06 22:49:41 +01:00
other work that should happen once before a series of benchmark runs, \
not every time as would happen with the --prepare option."
),
)
2018-03-24 18:30:52 -04:00
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("prepare")
2018-03-24 18:30:52 -04:00
.long("prepare")
2022-02-03 20:50:39 +01:00
.short('p')
2022-10-27 22:09:41 +02:00
.action(ArgAction::Append)
2022-10-28 09:50:35 +02:00
.num_args(1)
2018-03-24 18:30:52 -04:00
.value_name("CMD")
.help(
2019-10-13 18:10:53 +05:45
"Execute CMD before each timing run. This is useful for \
2019-10-13 14:47:18 +02:00
clearing disk caches, for example.\nThe --prepare option can \
2019-10-13 18:10:53 +05:45
be specified once for all commands or multiple times, once for \
each command. In the latter case, each preparation command will \
be run prior to the corresponding benchmark command.",
2018-03-24 18:30:52 -04:00
),
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("cleanup")
.long("cleanup")
2022-02-03 20:50:39 +01:00
.short('c')
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
.value_name("CMD")
.help(
2019-01-14 08:33:38 -08:00
"Execute CMD after the completion of all benchmarking \
runs for each individual command to be benchmarked. \
This is useful if the commands to be benchmarked produce \
artifacts that need to be cleaned up."
),
)
2018-03-24 18:30:52 -04:00
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("parameter-scan")
2018-03-24 18:30:52 -04:00
.long("parameter-scan")
2022-02-03 20:50:39 +01:00
.short('P')
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
.allow_hyphen_values(true)
2023-03-15 09:49:43 +01:00
.value_names(["VAR", "MIN", "MAX"])
2018-03-24 18:30:52 -04:00
.help(
"Perform benchmark runs for each value in the range MIN..MAX. Replaces the \
2019-10-13 16:24:29 +02:00
string '{VAR}' in each command by the current parameter value.\n\n \
Example: hyperfine -P threads 1 8 'make -j {threads}'\n\n\
2021-10-10 19:26:13 +00:00
This performs benchmarks for 'make -j 1', 'make -j 2', …, 'make -j 8'.\n\n\
To have the value increase following different patterns, use shell arithmetics.\n\n \
Example: hyperfine -P size 0 3 'sleep $((2**{size}))'\n\n\
This performs benchmarks with power of 2 increases: 'sleep 1', 'sleep 2', 'sleep 4', …\n\
The exact syntax may vary depending on your shell and OS."
),
2018-03-24 18:30:52 -04:00
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("parameter-step-size")
.long("parameter-step-size")
2022-02-03 20:50:39 +01:00
.short('D')
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
2023-03-15 09:49:43 +01:00
.value_names(["DELTA"])
.requires("parameter-scan")
.help(
2019-09-13 00:54:50 +01:00
"This argument requires --parameter-scan to be specified as well. \
2019-10-13 16:24:29 +02:00
Traverse the range MIN..MAX in steps of DELTA.\n\n \
Example: hyperfine -P delay 0.3 0.7 -D 0.2 'sleep {delay}'\n\n\
This performs benchmarks for 'sleep 0.3', 'sleep 0.5' and 'sleep 0.7'.",
),
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("parameter-list")
.long("parameter-list")
2022-02-03 20:50:39 +01:00
.short('L')
2022-10-27 22:09:41 +02:00
.action(ArgAction::Append)
.allow_hyphen_values(true)
2023-03-15 09:49:43 +01:00
.value_names(["VAR", "VALUES"])
.conflicts_with_all(["parameter-scan", "parameter-step-size"])
.help(
2020-05-22 21:35:37 +02:00
"Perform benchmark runs for each value in the comma-separated list VALUES. \
Replaces the string '{VAR}' in each command by the current parameter value\
2019-10-20 10:02:05 +02:00
.\n\nExample: hyperfine -L compiler gcc,clang '{compiler} -O2 main.cpp'\n\n\
2020-10-17 00:07:06 +02:00
This performs benchmarks for 'gcc -O2 main.cpp' and 'clang -O2 main.cpp'.\n\n\
The option can be specified multiple times to run benchmarks for all \
possible parameter combinations.\n"
),
)
2018-03-24 18:30:52 -04:00
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("style")
2018-03-24 18:30:52 -04:00
.long("style")
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
2018-03-24 18:30:52 -04:00
.value_name("TYPE")
2022-10-27 20:42:33 +02:00
.value_parser(["auto", "basic", "full", "nocolor", "color", "none"])
2018-03-24 18:30:52 -04:00
.help(
"Set output style type (default: auto). Set this to 'basic' to disable output \
coloring and interactive elements. Set it to 'full' to enable all effects \
even if no interactive terminal was detected. Set this to 'nocolor' to \
2019-01-02 17:13:05 +01:00
keep the interactive output without any colors. Set this to 'color' to keep \
the colors without any interactive output. Set this to 'none' to disable all \
the output of the tool.",
2018-03-24 18:30:52 -04:00
),
)
2018-10-30 19:30:17 -07:00
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("shell")
2018-10-30 19:30:17 -07:00
.long("shell")
2022-02-03 20:50:39 +01:00
.short('S')
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
2018-10-30 19:30:17 -07:00
.value_name("SHELL")
.overrides_with("shell")
.help("Set the shell to use for executing benchmarked commands. This can be the \
name or the path to the shell executable, or a full command line \
like \"bash --norc\". It can also be set to \"default\" to explicitly select \
2022-03-05 12:47:26 +01:00
the default shell on this platform. Finally, this can also be set to \
\"none\" to disable the shell. In this case, commands will be executed \
directly. They can still have arguments, but more complex things like \
\"sleep 0.1; sleep 0.2\" are not possible without a shell.")
)
.arg(
Arg::new("no-shell")
.short('N')
2022-10-27 22:09:41 +02:00
.action(ArgAction::SetTrue)
2023-03-15 09:49:43 +01:00
.conflicts_with_all(["shell", "debug-mode"])
.help("An alias for '--shell=none'.")
2018-10-30 19:30:17 -07:00
)
2018-03-24 18:30:52 -04:00
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("ignore-failure")
2018-03-24 18:30:52 -04:00
.long("ignore-failure")
2022-10-27 22:09:41 +02:00
.action(ArgAction::SetTrue)
2022-02-03 20:50:39 +01:00
.short('i')
2019-10-13 16:24:29 +02:00
.help("Ignore non-zero exit codes of the benchmarked programs."),
2018-03-24 18:30:52 -04:00
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("time-unit")
.long("time-unit")
2022-02-03 20:50:39 +01:00
.short('u')
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
.value_name("UNIT")
2022-10-27 20:42:33 +02:00
.value_parser(["millisecond", "second"])
2022-06-29 15:43:23 +02:00
.help("Set the time unit to be used. Possible values: millisecond, second. \
If the option is not given, the time unit is determined automatically. \
2022-06-16 08:54:36 +02:00
This option affects the standard output as well as all export formats except for CSV and JSON."),
)
2019-01-29 18:02:19 -05:00
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("export-asciidoc")
2019-01-29 18:02:19 -05:00
.long("export-asciidoc")
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
2019-01-29 18:02:19 -05:00
.value_name("FILE")
2022-06-29 15:43:23 +02:00
.help("Export the timing summary statistics as an AsciiDoc table to the given FILE. \
2022-07-01 13:57:42 +02:00
The output time unit can be changed using the --time-unit option."),
2019-01-29 18:02:19 -05:00
)
2018-03-24 18:30:52 -04:00
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("export-csv")
2018-03-24 18:30:52 -04:00
.long("export-csv")
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
2018-03-24 18:30:52 -04:00
.value_name("FILE")
2019-10-13 16:24:29 +02:00
.help("Export the timing summary statistics as CSV to the given FILE. If you need \
2022-06-29 15:43:23 +02:00
the timing results for each individual run, use the JSON export format. \
2022-07-01 13:57:42 +02:00
The output time unit is always seconds."),
2018-03-24 18:30:52 -04:00
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("export-json")
2018-03-24 18:30:52 -04:00
.long("export-json")
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
2018-03-24 18:30:52 -04:00
.value_name("FILE")
2022-06-29 15:43:23 +02:00
.help("Export the timing summary statistics and timings of individual runs as JSON to the given FILE. \
2022-06-17 15:02:41 +02:00
The output time unit is always seconds"),
2018-03-24 18:30:52 -04:00
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("export-markdown")
2018-03-24 18:30:52 -04:00
.long("export-markdown")
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
2018-03-24 18:30:52 -04:00
.value_name("FILE")
2022-06-29 15:43:23 +02:00
.help("Export the timing summary statistics as a Markdown table to the given FILE. \
2022-07-01 13:57:42 +02:00
The output time unit can be changed using the --time-unit option."),
2018-03-24 18:30:52 -04:00
)
2022-03-21 21:45:24 +01:00
.arg(
Arg::new("export-orgmode")
.long("export-orgmode")
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
2022-03-21 21:45:24 +01:00
.value_name("FILE")
2023-05-18 16:22:22 +03:00
.help("Export the timing summary statistics as an Emacs org-mode table to the given FILE. \
2022-07-01 13:57:42 +02:00
The output time unit can be changed using the --time-unit option."),
2022-03-21 21:45:24 +01:00
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("show-output")
2018-05-21 21:04:30 +02:00
.long("show-output")
2022-10-27 20:42:33 +02:00
.action(ArgAction::SetTrue)
.conflicts_with("style")
2018-05-22 22:57:51 +02:00
.help(
"Print the stdout and stderr of the benchmark instead of suppressing it. \
This will increase the time it takes for benchmarks to run, \
so it should only be used for debugging purposes or \
when trying to benchmark output speed.",
),
)
2022-05-14 13:21:41 -04:00
.arg(
Arg::new("output")
.long("output")
.conflicts_with("show-output")
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
2022-05-14 13:21:41 -04:00
.value_name("WHERE")
.help(
2023-03-15 09:08:59 +01:00
"Control where the output of the benchmark is redirected. Note \
that some programs like 'grep' detect when standard output is \
/dev/null and apply certain optimizations. To avoid that, consider \
using '--output=pipe'.\n\
\n\
<WHERE> can be:\n\
\n \
null: Redirect output to /dev/null (the default).\n\
\n \
pipe: Feed the output through a pipe before discarding it.\n\
\n \
inherit: Don't redirect the output at all (same as '--show-output').\n\
\n \
<FILE>: Write the output to the given file.",
2022-05-14 13:21:41 -04:00
),
)
2023-03-15 09:08:59 +01:00
.arg(
Arg::new("input")
.long("input")
2023-03-15 09:23:20 +01:00
.action(ArgAction::Set)
2023-03-15 10:05:57 +01:00
.num_args(1)
2023-03-15 09:08:59 +01:00
.value_name("WHERE")
.help("Control where the input of the benchmark comes from.\n\
\n\
<WHERE> can be:\n\
\n \
null: Read from /dev/null (the default).\n\
\n \
<FILE>: Read the input from the given file."),
)
.arg(
2022-02-03 20:50:39 +01:00
Arg::new("command-name")
2020-10-15 22:11:38 +02:00
.long("command-name")
2022-02-03 20:50:39 +01:00
.short('n')
2022-10-27 22:09:41 +02:00
.action(ArgAction::Append)
2022-10-28 09:50:35 +02:00
.num_args(1)
.value_name("NAME")
2022-02-03 20:50:39 +01:00
.help("Give a meaningful name to a command. This can be specified multiple times \
if several commands are benchmarked."),
)
// This option is hidden for now, as it is not yet clear yet if we want to 'stabilize' this,
// see discussion in https://github.com/sharkdp/hyperfine/issues/527
.arg(
Arg::new("min-benchmarking-time")
.long("min-benchmarking-time")
2022-10-27 22:09:41 +02:00
.action(ArgAction::Set)
.hide(true)
.help("Set the minimum time (in seconds) to run benchmarks. Note that the number of \
benchmark runs is additionally influenced by the `--min-runs`, `--max-runs`, and \
`--runs` option.")
)
2022-02-22 08:36:56 +01:00
.arg(
Arg::new("debug-mode")
.long("debug-mode")
2022-10-27 22:09:41 +02:00
.action(ArgAction::SetTrue)
2022-02-22 08:36:56 +01:00
.hide(true)
2022-07-01 13:57:42 +02:00
.help("Enable debug mode which does not actually run commands, but returns fake times when the command is 'sleep <time>'.")
2022-02-22 08:36:56 +01:00
)
2022-02-03 20:50:39 +01:00
}
#[test]
fn verify_app() {
2022-02-22 14:04:07 +01:00
build_command().debug_assert();
2018-03-24 18:30:52 -04:00
}