Skip to content
Myr

app.rs

This file

Path

src/app.rs

263 lines · 10.9 KiB · Rust

Last changed by

Add debug/mock modeDavid Peterbefore adoption6f51232

History of this file

Take it

1use std::ffi::OsString;23use clap::{crate_version, App, AppSettings, Arg, ArgMatches};45pub fn get_cli_arguments<'a, I, T>(args: I) -> ArgMatches6where7    I: IntoIterator<Item = T>,8    T: Into<OsString> + Clone + 'a,9{10    let app = build_app();11    app.get_matches_from(args)12}1314/// Build the clap app for parsing command line arguments15fn build_app() -> App<'static> {16    App::new("hyperfine")17        .version(crate_version!())18        .setting(AppSettings::DeriveDisplayOrder)19        .setting(AppSettings::NextLineHelp)20        .setting(AppSettings::HidePossibleValues)21        .max_term_width(90)22        .about("A command-line benchmarking tool.")23        .arg(24            Arg::new("command")25                .help("Command to benchmark. This can be the name of an executable or a shell \26                       command like \"sleep 0.5 && echo test\". If multiple commands are given, \27                       hyperfine will show a comparison of the respective runtimes.")28                .required(true)29                .multiple_occurrences(true)30                .forbid_empty_values(true),31        )32        .arg(33            Arg::new("warmup")34                .long("warmup")35                .short('w')36                .takes_value(true)37                .value_name("NUM")38                .help(39                    "Perform NUM warmup runs before the actual benchmark. This can be used \40                     to fill (disk) caches for I/O-heavy programs.",41                ),42        )43        .arg(44            Arg::new("min-runs")45                .long("min-runs")46                .short('m')47                .takes_value(true)48                .value_name("NUM")49                .help("Perform at least NUM runs for each command (default: 10)."),50        )51        .arg(52            Arg::new("max-runs")53                .long("max-runs")54                .short('M')55                .takes_value(true)56                .value_name("NUM")57                .help("Perform at most NUM runs for each command. By default, there is no limit."),58        )59        .arg(60            Arg::new("runs")61                .long("runs")62                .conflicts_with_all(&["max-runs", "min-runs"])63                .short('r')64                .takes_value(true)65                .value_name("NUM")66                .help("Perform exactly NUM runs for each command. If this option is not specified, \67                       hyperfine automatically determines the number of runs."),68        )69        .arg(70            Arg::new("setup")71                .long("setup")72                .short('s')73                .takes_value(true)74                .number_of_values(1)75                .value_name("CMD")76                .help(77                    "Execute CMD before each set of timing runs. This is useful for \78                     compiling your software with the provided parameters, or to do any \79                     other work that should happen once before a series of benchmark runs, \80                     not every time as would happen with the --prepare option."81                ),82        )83        .arg(84            Arg::new("prepare")85                .long("prepare")86                .short('p')87                .takes_value(true)88                .multiple_occurrences(true)89                .number_of_values(1)90                .value_name("CMD")91                .help(92                    "Execute CMD before each timing run. This is useful for \93                     clearing disk caches, for example.\nThe --prepare option can \94                     be specified once for all commands or multiple times, once for \95                     each command. In the latter case, each preparation command will \96                     be run prior to the corresponding benchmark command.",97                ),98        )99        .arg(100            Arg::new("cleanup")101                .long("cleanup")102                .short('c')103                .takes_value(true)104                .value_name("CMD")105                .help(106                    "Execute CMD after the completion of all benchmarking \107                     runs for each individual command to be benchmarked. \108                     This is useful if the commands to be benchmarked produce \109                     artifacts that need to be cleaned up."110                ),111        )112        .arg(113            Arg::new("parameter-scan")114                .long("parameter-scan")115                .short('P')116                .takes_value(true)117                .allow_hyphen_values(true)118                .value_names(&["VAR", "MIN", "MAX"])119                .help(120                    "Perform benchmark runs for each value in the range MIN..MAX. Replaces the \121                     string '{VAR}' in each command by the current parameter value.\n\n  \122                     Example:  hyperfine -P threads 1 8 'make -j {threads}'\n\n\123                     This performs benchmarks for 'make -j 1', 'make -j 2', …, 'make -j 8'.\n\n\124                     To have the value increase following different patterns, use shell arithmetics.\n\n  \125                     Example: hyperfine -P size 0 3 'sleep $((2**{size}))'\n\n\126                     This performs benchmarks with power of 2 increases: 'sleep 1', 'sleep 2', 'sleep 4', …\n\127                     The exact syntax may vary depending on your shell and OS."128                ),129        )130        .arg(131            Arg::new("parameter-step-size")132                .long("parameter-step-size")133                .short('D')134                .takes_value(true)135                .value_names(&["DELTA"])136                .requires("parameter-scan")137                .help(138                    "This argument requires --parameter-scan to be specified as well. \139                     Traverse the range MIN..MAX in steps of DELTA.\n\n  \140                     Example:  hyperfine -P delay 0.3 0.7 -D 0.2 'sleep {delay}'\n\n\141                     This performs benchmarks for 'sleep 0.3', 'sleep 0.5' and 'sleep 0.7'.",142                ),143        )144        .arg(145            Arg::new("parameter-list")146                .long("parameter-list")147                .short('L')148                .takes_value(true)149                .multiple_occurrences(true)150                .allow_hyphen_values(true)151                .value_names(&["VAR", "VALUES"])152                .conflicts_with_all(&["parameter-scan", "parameter-step-size"])153                .help(154                    "Perform benchmark runs for each value in the comma-separated list VALUES. \155                     Replaces the string '{VAR}' in each command by the current parameter value\156                     .\n\nExample:  hyperfine -L compiler gcc,clang '{compiler} -O2 main.cpp'\n\n\157                     This performs benchmarks for 'gcc -O2 main.cpp' and 'clang -O2 main.cpp'.\n\n\158                     The option can be specified multiple times to run benchmarks for all \159                     possible parameter combinations.\n"160                ),161        )162        .arg(163            Arg::new("style")164                .long("style")165                .takes_value(true)166                .value_name("TYPE")167                .possible_values(&["auto", "basic", "full", "nocolor", "color", "none"])168                .help(169                    "Set output style type (default: auto). Set this to 'basic' to disable output \170                     coloring and interactive elements. Set it to 'full' to enable all effects \171                     even if no interactive terminal was detected. Set this to 'nocolor' to \172                     keep the interactive output without any colors. Set this to 'color' to keep \173                     the colors without any interactive output. Set this to 'none' to disable all \174                     the output of the tool.",175                ),176        )177        .arg(178            Arg::new("shell")179                .long("shell")180                .short('S')181                .takes_value(true)182                .value_name("SHELL")183                .overrides_with("shell")184                .help("Set the shell to use for executing benchmarked commands."),185        )186        .arg(187            Arg::new("ignore-failure")188                .long("ignore-failure")189                .short('i')190                .help("Ignore non-zero exit codes of the benchmarked programs."),191        )192        .arg(193            Arg::new("time-unit")194                .long("time-unit")195                .short('u')196                .takes_value(true)197                .value_name("UNIT")198                .possible_values(&["millisecond", "second"])199                .help("Set the time unit to be used. Possible values: millisecond, second."),200        )201        .arg(202            Arg::new("export-asciidoc")203                .long("export-asciidoc")204                .takes_value(true)205                .value_name("FILE")206                .help("Export the timing summary statistics as an AsciiDoc table to the given FILE."),207        )208        .arg(209            Arg::new("export-csv")210                .long("export-csv")211                .takes_value(true)212                .value_name("FILE")213                .help("Export the timing summary statistics as CSV to the given FILE. If you need \214                       the timing results for each individual run, use the JSON export format."),215        )216        .arg(217            Arg::new("export-json")218                .long("export-json")219                .takes_value(true)220                .value_name("FILE")221                .help("Export the timing summary statistics and timings of individual runs as JSON to the given FILE."),222        )223        .arg(224            Arg::new("export-markdown")225                .long("export-markdown")226                .takes_value(true)227                .value_name("FILE")228                .help("Export the timing summary statistics as a Markdown table to the given FILE."),229        )230        .arg(231            Arg::new("show-output")232                .long("show-output")233                .conflicts_with("style")234                .help(235                    "Print the stdout and stderr of the benchmark instead of suppressing it. \236                     This will increase the time it takes for benchmarks to run, \237                     so it should only be used for debugging purposes or \238                     when trying to benchmark output speed.",239                ),240        )241        .arg(242            Arg::new("command-name")243                .long("command-name")244                .short('n')245                .takes_value(true)246                .multiple_occurrences(true)247                .number_of_values(1)248                .value_name("NAME")249                .help("Give a meaningful name to a command. This can be specified multiple times \250                       if several commands are benchmarked."),251        )252        .arg(253            Arg::new("debug-mode")254            .long("debug-mode")255            .hide(true)256            .help("Enable debug mode which does not actually run commands, but returns fake times when the command is 'sleep <time>'")257        )258}259260#[test]261fn verify_app() {262    build_app().debug_assert();263}