cli.rs
This file
Path
src/cli.rs
358 lines · 15.8 KiB · Rust
Last changed by
Add support for microseconds, closes #684David Peterbefore adoption4564977
History of this fileTake it
1use std::ffi::OsString;23use clap::{4 builder::NonEmptyStringValueParser, crate_version, Arg, ArgAction, ArgMatches, Command,5};67pub fn get_cli_arguments<'a, I, T>(args: I) -> ArgMatches8where9 I: IntoIterator<Item = T>,10 T: Into<OsString> + Clone + 'a,11{12 let command = build_command();13 command.get_matches_from(args)14}1516/// Build the clap command for parsing command line arguments17fn build_command() -> Command {18 Command::new("hyperfine")19 .version(crate_version!())20 .next_line_help(true)21 .hide_possible_values(true)22 .about("A command-line benchmarking tool.")23 .help_expected(true)24 .max_term_width(80)25 .arg(26 Arg::new("command")27 .help("The command to benchmark. This can be the name of an executable, a command \28 line like \"grep -i todo\" or a shell command like \"sleep 0.5 && echo test\". \29 The latter is only available if the shell is not explicitly disabled via \30 '--shell=none'. If multiple commands are given, hyperfine will show a \31 comparison of the respective runtimes.")32 .required(true)33 .action(ArgAction::Append)34 .value_parser(NonEmptyStringValueParser::new()),35 )36 .arg(37 Arg::new("warmup")38 .long("warmup")39 .short('w')40 .value_name("NUM")41 .action(ArgAction::Set)42 .help(43 "Perform NUM warmup runs before the actual benchmark. This can be used \44 to fill (disk) caches for I/O-heavy programs.",45 ),46 )47 .arg(48 Arg::new("min-runs")49 .long("min-runs")50 .short('m')51 .action(ArgAction::Set)52 .value_name("NUM")53 .help("Perform at least NUM runs for each command (default: 10)."),54 )55 .arg(56 Arg::new("max-runs")57 .long("max-runs")58 .short('M')59 .action(ArgAction::Set)60 .value_name("NUM")61 .help("Perform at most NUM runs for each command. By default, there is no limit."),62 )63 .arg(64 Arg::new("runs")65 .long("runs")66 .conflicts_with_all(["max-runs", "min-runs"])67 .short('r')68 .action(ArgAction::Set)69 .value_name("NUM")70 .help("Perform exactly NUM runs for each command. If this option is not specified, \71 hyperfine automatically determines the number of runs."),72 )73 .arg(74 Arg::new("setup")75 .long("setup")76 .short('s')77 .action(ArgAction::Set)78 .value_name("CMD")79 .help(80 "Execute CMD before each set of timing runs. This is useful for \81 compiling your software with the provided parameters, or to do any \82 other work that should happen once before a series of benchmark runs, \83 not every time as would happen with the --prepare option."84 ),85 )86 .arg(87 Arg::new("prepare")88 .long("prepare")89 .short('p')90 .action(ArgAction::Append)91 .num_args(1)92 .value_name("CMD")93 .help(94 "Execute CMD before each timing run. This is useful for \95 clearing disk caches, for example.\nThe --prepare option can \96 be specified once for all commands or multiple times, once for \97 each command. In the latter case, each preparation command will \98 be run prior to the corresponding benchmark command.",99 ),100 )101 .arg(102 Arg::new("cleanup")103 .long("cleanup")104 .short('c')105 .action(ArgAction::Set)106 .value_name("CMD")107 .help(108 "Execute CMD after the completion of all benchmarking \109 runs for each individual command to be benchmarked. \110 This is useful if the commands to be benchmarked produce \111 artifacts that need to be cleaned up."112 ),113 )114 .arg(115 Arg::new("parameter-scan")116 .long("parameter-scan")117 .short('P')118 .action(ArgAction::Set)119 .allow_hyphen_values(true)120 .value_names(["VAR", "MIN", "MAX"])121 .help(122 "Perform benchmark runs for each value in the range MIN..MAX. Replaces the \123 string '{VAR}' in each command by the current parameter value.\n\n \124 Example: hyperfine -P threads 1 8 'make -j {threads}'\n\n\125 This performs benchmarks for 'make -j 1', 'make -j 2', …, 'make -j 8'.\n\n\126 To have the value increase following different patterns, use shell arithmetics.\n\n \127 Example: hyperfine -P size 0 3 'sleep $((2**{size}))'\n\n\128 This performs benchmarks with power of 2 increases: 'sleep 1', 'sleep 2', 'sleep 4', …\n\129 The exact syntax may vary depending on your shell and OS."130 ),131 )132 .arg(133 Arg::new("parameter-step-size")134 .long("parameter-step-size")135 .short('D')136 .action(ArgAction::Set)137 .value_names(["DELTA"])138 .requires("parameter-scan")139 .help(140 "This argument requires --parameter-scan to be specified as well. \141 Traverse the range MIN..MAX in steps of DELTA.\n\n \142 Example: hyperfine -P delay 0.3 0.7 -D 0.2 'sleep {delay}'\n\n\143 This performs benchmarks for 'sleep 0.3', 'sleep 0.5' and 'sleep 0.7'.",144 ),145 )146 .arg(147 Arg::new("parameter-list")148 .long("parameter-list")149 .short('L')150 .action(ArgAction::Append)151 .allow_hyphen_values(true)152 .value_names(["VAR", "VALUES"])153 .conflicts_with_all(["parameter-scan", "parameter-step-size"])154 .help(155 "Perform benchmark runs for each value in the comma-separated list VALUES. \156 Replaces the string '{VAR}' in each command by the current parameter value\157 .\n\nExample: hyperfine -L compiler gcc,clang '{compiler} -O2 main.cpp'\n\n\158 This performs benchmarks for 'gcc -O2 main.cpp' and 'clang -O2 main.cpp'.\n\n\159 The option can be specified multiple times to run benchmarks for all \160 possible parameter combinations.\n"161 ),162 )163 .arg(164 Arg::new("shell")165 .long("shell")166 .short('S')167 .action(ArgAction::Set)168 .value_name("SHELL")169 .overrides_with("shell")170 .help("Set the shell to use for executing benchmarked commands. This can be the \171 name or the path to the shell executable, or a full command line \172 like \"bash --norc\". It can also be set to \"default\" to explicitly select \173 the default shell on this platform. Finally, this can also be set to \174 \"none\" to disable the shell. In this case, commands will be executed \175 directly. They can still have arguments, but more complex things like \176 \"sleep 0.1; sleep 0.2\" are not possible without a shell.")177 )178 .arg(179 Arg::new("no-shell")180 .short('N')181 .action(ArgAction::SetTrue)182 .conflicts_with_all(["shell", "debug-mode"])183 .help("An alias for '--shell=none'.")184 )185 .arg(186 Arg::new("ignore-failure")187 .long("ignore-failure")188 .action(ArgAction::SetTrue)189 .short('i')190 .help("Ignore non-zero exit codes of the benchmarked programs."),191 )192 .arg(193 Arg::new("style")194 .long("style")195 .action(ArgAction::Set)196 .value_name("TYPE")197 .value_parser(["auto", "basic", "full", "nocolor", "color", "none"])198 .help(199 "Set output style type (default: auto). Set this to 'basic' to disable output \200 coloring and interactive elements. Set it to 'full' to enable all effects \201 even if no interactive terminal was detected. Set this to 'nocolor' to \202 keep the interactive output without any colors. Set this to 'color' to keep \203 the colors without any interactive output. Set this to 'none' to disable all \204 the output of the tool.",205 ),206 )207 .arg(208 Arg::new("sort")209 .long("sort")210 .action(ArgAction::Set)211 .value_name("METHOD")212 .value_parser(["auto", "command", "mean-time"])213 .default_value("auto")214 .hide_default_value(true)215 .help(216 "Specify the sort order of the speed comparison summary and the exported tables for \217 markup formats (Markdown, AsciiDoc, org-mode):\n \218 * 'auto' (default): the speed comparison will be ordered by time and\n \219 the markup tables will be ordered by command (input order).\n \220 * 'command': order benchmarks in the way they were specified\n \221 * 'mean-time': order benchmarks by mean runtime\n"222 ),223 )224 .arg(225 Arg::new("time-unit")226 .long("time-unit")227 .short('u')228 .action(ArgAction::Set)229 .value_name("UNIT")230 .value_parser(["microsecond", "millisecond", "second"])231 .help("Set the time unit to be used. Possible values: microsecond, millisecond, second. \232 If the option is not given, the time unit is determined automatically. \233 This option affects the standard output as well as all export formats except for CSV and JSON."),234 )235 .arg(236 Arg::new("export-asciidoc")237 .long("export-asciidoc")238 .action(ArgAction::Set)239 .value_name("FILE")240 .help("Export the timing summary statistics as an AsciiDoc table to the given FILE. \241 The output time unit can be changed using the --time-unit option."),242 )243 .arg(244 Arg::new("export-csv")245 .long("export-csv")246 .action(ArgAction::Set)247 .value_name("FILE")248 .help("Export the timing summary statistics as CSV to the given FILE. If you need \249 the timing results for each individual run, use the JSON export format. \250 The output time unit is always seconds."),251 )252 .arg(253 Arg::new("export-json")254 .long("export-json")255 .action(ArgAction::Set)256 .value_name("FILE")257 .help("Export the timing summary statistics and timings of individual runs as JSON to the given FILE. \258 The output time unit is always seconds"),259 )260 .arg(261 Arg::new("export-markdown")262 .long("export-markdown")263 .action(ArgAction::Set)264 .value_name("FILE")265 .help("Export the timing summary statistics as a Markdown table to the given FILE. \266 The output time unit can be changed using the --time-unit option."),267 )268 .arg(269 Arg::new("export-orgmode")270 .long("export-orgmode")271 .action(ArgAction::Set)272 .value_name("FILE")273 .help("Export the timing summary statistics as an Emacs org-mode table to the given FILE. \274 The output time unit can be changed using the --time-unit option."),275 )276 .arg(277 Arg::new("show-output")278 .long("show-output")279 .action(ArgAction::SetTrue)280 .conflicts_with("style")281 .help(282 "Print the stdout and stderr of the benchmark instead of suppressing it. \283 This will increase the time it takes for benchmarks to run, \284 so it should only be used for debugging purposes or \285 when trying to benchmark output speed.",286 ),287 )288 .arg(289 Arg::new("output")290 .long("output")291 .conflicts_with("show-output")292 .action(ArgAction::Set)293 .value_name("WHERE")294 .help(295 "Control where the output of the benchmark is redirected. Note \296 that some programs like 'grep' detect when standard output is \297 /dev/null and apply certain optimizations. To avoid that, consider \298 using '--output=pipe'.\n\299 \n\300 <WHERE> can be:\n\301 \n \302 null: Redirect output to /dev/null (the default).\n\303 \n \304 pipe: Feed the output through a pipe before discarding it.\n\305 \n \306 inherit: Don't redirect the output at all (same as '--show-output').\n\307 \n \308 <FILE>: Write the output to the given file.",309 ),310 )311 .arg(312 Arg::new("input")313 .long("input")314 .action(ArgAction::Set)315 .num_args(1)316 .value_name("WHERE")317 .help("Control where the input of the benchmark comes from.\n\318 \n\319 <WHERE> can be:\n\320 \n \321 null: Read from /dev/null (the default).\n\322 \n \323 <FILE>: Read the input from the given file."),324 )325 .arg(326 Arg::new("command-name")327 .long("command-name")328 .short('n')329 .action(ArgAction::Append)330 .num_args(1)331 .value_name("NAME")332 .help("Give a meaningful name to a command. This can be specified multiple times \333 if several commands are benchmarked."),334 )335 // This option is hidden for now, as it is not yet clear yet if we want to 'stabilize' this,336 // see discussion in https://github.com/sharkdp/hyperfine/issues/527337 .arg(338 Arg::new("min-benchmarking-time")339 .long("min-benchmarking-time")340 .action(ArgAction::Set)341 .hide(true)342 .help("Set the minimum time (in seconds) to run benchmarks. Note that the number of \343 benchmark runs is additionally influenced by the `--min-runs`, `--max-runs`, and \344 `--runs` option.")345 )346 .arg(347 Arg::new("debug-mode")348 .long("debug-mode")349 .action(ArgAction::SetTrue)350 .hide(true)351 .help("Enable debug mode which does not actually run commands, but returns fake times when the command is 'sleep <time>'.")352 )353}354355#[test]356fn verify_app() {357 build_command().debug_assert();358}