Skip to content
Myr

cli.rs

This file

Path

src/cli.rs

319 lines · 14.2 KiB · Rust

Last changed by

Add new (hidden) --min-benchmarking-time optionDavid Peterbefore adoptiondffbdc3

History of this file

Take it

1use std::ffi::OsString;23use clap::{crate_version, AppSettings, Arg, ArgMatches, Command};45pub fn get_cli_arguments<'a, I, T>(args: I) -> ArgMatches6where7    I: IntoIterator<Item = T>,8    T: Into<OsString> + Clone + 'a,9{10    let command = build_command();11    command.get_matches_from(args)12}1314/// Build the clap command for parsing command line arguments15fn build_command() -> Command<'static> {16    Command::new("hyperfine")17        .version(crate_version!())18        .setting(AppSettings::DeriveDisplayOrder)19        .next_line_help(true)20        .hide_possible_values(true)21        .max_term_width(90)22        .about("A command-line benchmarking tool.")23        .arg(24            Arg::new("command")25                .help("The command to benchmark. This can be the name of an executable, a command \26                       line like \"grep -i todo\" or a shell command like \"sleep 0.5 && echo test\". \27                       The latter is only available if the shell is not explicitly disabled via \28                       '--shell=none'. If multiple commands are given, hyperfine will show a \29                       comparison of the respective runtimes.")30                .required(true)31                .multiple_occurrences(true)32                .forbid_empty_values(true),33        )34        .arg(35            Arg::new("warmup")36                .long("warmup")37                .short('w')38                .takes_value(true)39                .value_name("NUM")40                .help(41                    "Perform NUM warmup runs before the actual benchmark. This can be used \42                     to fill (disk) caches for I/O-heavy programs.",43                ),44        )45        .arg(46            Arg::new("min-runs")47                .long("min-runs")48                .short('m')49                .takes_value(true)50                .value_name("NUM")51                .help("Perform at least NUM runs for each command (default: 10)."),52        )53        .arg(54            Arg::new("max-runs")55                .long("max-runs")56                .short('M')57                .takes_value(true)58                .value_name("NUM")59                .help("Perform at most NUM runs for each command. By default, there is no limit."),60        )61        .arg(62            Arg::new("runs")63                .long("runs")64                .conflicts_with_all(&["max-runs", "min-runs"])65                .short('r')66                .takes_value(true)67                .value_name("NUM")68                .help("Perform exactly NUM runs for each command. If this option is not specified, \69                       hyperfine automatically determines the number of runs."),70        )71        .arg(72            Arg::new("setup")73                .long("setup")74                .short('s')75                .takes_value(true)76                .number_of_values(1)77                .value_name("CMD")78                .help(79                    "Execute CMD before each set of timing runs. This is useful for \80                     compiling your software with the provided parameters, or to do any \81                     other work that should happen once before a series of benchmark runs, \82                     not every time as would happen with the --prepare option."83                ),84        )85        .arg(86            Arg::new("prepare")87                .long("prepare")88                .short('p')89                .takes_value(true)90                .multiple_occurrences(true)91                .number_of_values(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                .takes_value(true)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                .takes_value(true)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                .takes_value(true)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                .takes_value(true)151                .multiple_occurrences(true)152                .allow_hyphen_values(true)153                .value_names(&["VAR", "VALUES"])154                .conflicts_with_all(&["parameter-scan", "parameter-step-size"])155                .help(156                    "Perform benchmark runs for each value in the comma-separated list VALUES. \157                     Replaces the string '{VAR}' in each command by the current parameter value\158                     .\n\nExample:  hyperfine -L compiler gcc,clang '{compiler} -O2 main.cpp'\n\n\159                     This performs benchmarks for 'gcc -O2 main.cpp' and 'clang -O2 main.cpp'.\n\n\160                     The option can be specified multiple times to run benchmarks for all \161                     possible parameter combinations.\n"162                ),163        )164        .arg(165            Arg::new("style")166                .long("style")167                .takes_value(true)168                .value_name("TYPE")169                .possible_values(&["auto", "basic", "full", "nocolor", "color", "none"])170                .help(171                    "Set output style type (default: auto). Set this to 'basic' to disable output \172                     coloring and interactive elements. Set it to 'full' to enable all effects \173                     even if no interactive terminal was detected. Set this to 'nocolor' to \174                     keep the interactive output without any colors. Set this to 'color' to keep \175                     the colors without any interactive output. Set this to 'none' to disable all \176                     the output of the tool.",177                ),178        )179        .arg(180            Arg::new("shell")181                .long("shell")182                .short('S')183                .takes_value(true)184                .value_name("SHELL")185                .overrides_with("shell")186                .help("Set the shell to use for executing benchmarked commands. This can be the \187                       name or the path to the shell executable, or a full command line \188                       like \"bash --norc\". It can also be set to \"default\" to explicitly select \189                       the default shell on this platform. Finally, this can also be set to \190                       \"none\" to disable the shell. In this case, commands will be executed \191                       directly. They can still have arguments, but more complex things like \192                       \"sleep 0.1; sleep 0.2\" are not possible without a shell.")193        )194        .arg(195            Arg::new("no-shell")196                .short('N')197                .conflicts_with_all(&["shell", "debug-mode"])198                .help("An alias for '--shell=none'.")199        )200        .arg(201            Arg::new("ignore-failure")202                .long("ignore-failure")203                .short('i')204                .help("Ignore non-zero exit codes of the benchmarked programs."),205        )206        .arg(207            Arg::new("time-unit")208                .long("time-unit")209                .short('u')210                .takes_value(true)211                .value_name("UNIT")212                .possible_values(&["millisecond", "second"])213                .help("Set the time unit to be used. Possible values: millisecond, second. \214                       If the option is not given, the time unit is determined automatically. \215                       This option affects the standard output as well as all export formats except for CSV and JSON."),216        )217        .arg(218            Arg::new("export-asciidoc")219                .long("export-asciidoc")220                .takes_value(true)221                .value_name("FILE")222                .help("Export the timing summary statistics as an AsciiDoc table to the given FILE. \223                       The output time unit can be changed using the --time-unit option."),224        )225        .arg(226            Arg::new("export-csv")227                .long("export-csv")228                .takes_value(true)229                .value_name("FILE")230                .help("Export the timing summary statistics as CSV to the given FILE. If you need \231                       the timing results for each individual run, use the JSON export format. \232                       The output time unit is always seconds."),233        )234        .arg(235            Arg::new("export-json")236                .long("export-json")237                .takes_value(true)238                .value_name("FILE")239                .help("Export the timing summary statistics and timings of individual runs as JSON to the given FILE. \240                       The output time unit is always seconds"),241        )242        .arg(243            Arg::new("export-markdown")244                .long("export-markdown")245                .takes_value(true)246                .value_name("FILE")247                .help("Export the timing summary statistics as a Markdown table to the given FILE. \248                       The output time unit can be changed using the --time-unit option."),249        )250        .arg(251            Arg::new("export-orgmode")252                .long("export-orgmode")253                .takes_value(true)254                .value_name("FILE")255                .help("Export the timing summary statistics as a Emacs org-mode table to the given FILE. \256                       The output time unit can be changed using the --time-unit option."),257        )258        .arg(259            Arg::new("show-output")260                .long("show-output")261                .conflicts_with("style")262                .help(263                    "Print the stdout and stderr of the benchmark instead of suppressing it. \264                     This will increase the time it takes for benchmarks to run, \265                     so it should only be used for debugging purposes or \266                     when trying to benchmark output speed.",267                ),268        )269        .arg(270            Arg::new("output")271                .long("output")272                .conflicts_with("show-output")273                .takes_value(true)274                .value_name("WHERE")275                .help(276                    "Control where the output of the benchmark is redirected. <WHERE> can be:\n\n\277                     null: Redirect output to /dev/null (the default). \278                     Note that some programs like 'grep' detect when standard output is /dev/null \279                     and apply certain optimizations. To avoid that, consider using \280                     '--output=pipe'.\n\n\281                     pipe: Feed the output through a pipe before discarding it.\n\n\282                     inherit: Don't redirect the output at all (same as '--show-output').\n\n\283                     <FILE>: Write the output to the given file.",284                ),285        )286        .arg(287            Arg::new("command-name")288                .long("command-name")289                .short('n')290                .takes_value(true)291                .multiple_occurrences(true)292                .number_of_values(1)293                .value_name("NAME")294                .help("Give a meaningful name to a command. This can be specified multiple times \295                       if several commands are benchmarked."),296        )297        // This option is hidden for now, as it is not yet clear yet if we want to 'stabilize' this,298        // see discussion in https://github.com/sharkdp/hyperfine/issues/527299        .arg(300            Arg::new("min-benchmarking-time")301            .long("min-benchmarking-time")302            .takes_value(true)303            .hide(true)304            .help("Set the minimum time (in seconds) to run benchmarks. Note that the number of \305                   benchmark runs is additionally influenced by the `--min-runs`, `--max-runs`, and \306                   `--runs` option.")307        )308        .arg(309            Arg::new("debug-mode")310            .long("debug-mode")311            .hide(true)312            .help("Enable debug mode which does not actually run commands, but returns fake times when the command is 'sleep <time>'.")313        )314}315316#[test]317fn verify_app() {318    build_command().debug_assert();319}