Skip to content
Myr

integration_tests.rs

This file

Path

tests/integration_tests.rs

1,081 lines · 30.2 KiB · Rust

Last changed by

Merge pull request #807 from willcl-ark/expose-run-varDavid Peterbefore adoption30a2b61

History of this file

Take it

1mod common;2use common::{hyperfine, hyperfine_raw_command};34use assert_cmd::assert::OutputAssertExt;5use insta_cmd::assert_cmd_snapshot;6use predicates::prelude::*;78/// Platform-specific I/O utility.9/// - On Unix-like systems, defaults to `cat`.10/// - On Windows, uses `findstr` as an alternative.11///   See: <https://superuser.com/questions/853580/real-windows-equivalent-to-cat-stdin>12const STDIN_READ_COMMAND: &str = if cfg!(windows) { "findstr x*" } else { "cat" };1314pub fn hyperfine_debug() -> std::process::Command {15    let mut cmd = hyperfine_raw_command();16    cmd.arg("--debug-mode");17    cmd18}1920fn snapshot_settings() -> insta::Settings {21    let mut settings = insta::Settings::clone_current();22    // Ignore trailing report padding while preserving indentation and internal spacing.23    settings.add_filter(r"(?m)[ \t]+$", "");24    settings25}2627#[test]28fn runs_successfully() {29    hyperfine()30        .arg("--runs=2")31        .arg("echo dummy benchmark")32        .assert()33        .success();34}3536#[test]37fn one_run_is_supported() {38    hyperfine()39        .arg("--runs=1")40        .arg("echo dummy benchmark")41        .assert()42        .success();43}4445/// Regression test: hyperfine must not panic when writing to a closed46/// stdout pipe (e.g. `hyperfine ... | head -n 1` or a downstream process47/// that terminates early). A `BrokenPipe` error should result in a quiet48/// exit with code 0 instead of a `println!` panic.49#[test]50fn exits_quietly_when_stdout_is_closed() {51    use std::process::Stdio;5253    for args in [54        vec!["--runs=1", "echo dummy benchmark"],55        vec!["--runs=1", "--export-json=-", "echo dummy benchmark"],56        vec!["--runs=1", "echo dummy benchmark", "echo second benchmark"],57    ] {58        // A pipe whose read end has been closed before hyperfine is even59        // started: every write to it will fail with `BrokenPipe`.60        let (reader, writer) = std::io::pipe().expect("failed to create pipe");61        drop(reader);6263        let output = hyperfine_raw_command()64            .args(&args)65            .stdout(writer)66            .stderr(Stdio::piped())67            .spawn()68            .expect("failed to spawn hyperfine")69            .wait_with_output()70            .expect("failed to wait for hyperfine");71        let stderr = String::from_utf8_lossy(&output.stderr);7273        assert!(74            !stderr.contains("panicked"),75            "hyperfine panicked while writing to a closed pipe:\n{}",76            stderr77        );78        assert!(79            output.status.success(),80            "hyperfine did not exit cleanly; stderr:\n{}",81            stderr82        );83    }84}8586#[test]87fn fails_with_zero_runs() {88    for option in ["--runs", "--min-runs", "--max-runs"] {89        hyperfine()90            .arg(option)91            .arg("0")92            .arg("echo dummy benchmark")93            .assert()94            .code(2)95            .stderr(predicate::str::contains(format!(96                "invalid value '0' for '{option} <NUM>'"97            )));98    }99}100101#[test]102fn min_runs_of_one_still_performs_one_run() {103    hyperfine_debug()104        .arg("--min-runs=1")105        .arg("--warmup=0")106        .arg("sleep 4")107        .assert()108        .success()109        .stdout(predicate::str::contains("Time (abs ≡)"));110}111112#[test]113fn can_run_commands_without_a_shell() {114    hyperfine()115        .arg("--runs=1")116        .arg("--show-output")117        .arg("--shell=none")118        .arg("echo 'hello world' argument2")119        .assert()120        .success()121        .stdout(predicate::str::contains("hello world argument2"));122}123124#[test]125fn fails_with_wrong_number_of_command_name_arguments() {126    hyperfine()127        .arg("--command-name=a")128        .arg("--command-name=b")129        .arg("echo a")130        .assert()131        .failure()132        .stderr(predicate::str::contains("Too many --command-name options"));133}134135#[test]136fn fails_with_wrong_number_of_prepare_options() {137    hyperfine()138        .arg("--runs=1")139        .arg("--prepare=echo a")140        .arg("--prepare=echo b")141        .arg("echo a")142        .arg("echo b")143        .assert()144        .success();145146    hyperfine()147        .arg("--runs=1")148        .arg("--prepare=echo ref")149        .arg("--prepare=echo a")150        .arg("--prepare=echo b")151        .arg("--reference=echo ref")152        .arg("echo a")153        .arg("echo b")154        .assert()155        .success();156157    hyperfine()158        .arg("--runs=1")159        .arg("--prepare=echo a")160        .arg("--prepare=echo b")161        .arg("echo a")162        .arg("echo b")163        .arg("echo c")164        .assert()165        .failure()166        .stderr(predicate::str::contains(167            "The '--prepare' option has to be provided",168        ));169170    hyperfine()171        .arg("--runs=1")172        .arg("--prepare=echo a")173        .arg("--prepare=echo b")174        .arg("--reference=echo ref")175        .arg("echo a")176        .arg("echo b")177        .assert()178        .failure()179        .stderr(predicate::str::contains(180            "The '--prepare' option has to be provided",181        ));182}183184#[test]185fn fails_with_wrong_number_of_conclude_options() {186    hyperfine()187        .arg("--runs=1")188        .arg("--conclude=echo a")189        .arg("--conclude=echo b")190        .arg("echo a")191        .arg("echo b")192        .assert()193        .success();194195    hyperfine()196        .arg("--runs=1")197        .arg("--conclude=echo ref")198        .arg("--conclude=echo a")199        .arg("--conclude=echo b")200        .arg("--reference=echo ref")201        .arg("echo a")202        .arg("echo b")203        .assert()204        .success();205206    hyperfine()207        .arg("--runs=1")208        .arg("--conclude=echo a")209        .arg("--conclude=echo b")210        .arg("echo a")211        .arg("echo b")212        .arg("echo c")213        .assert()214        .failure()215        .stderr(predicate::str::contains(216            "The '--conclude' option has to be provided",217        ));218219    hyperfine()220        .arg("--runs=1")221        .arg("--conclude=echo a")222        .arg("--conclude=echo b")223        .arg("--reference=echo ref")224        .arg("echo a")225        .arg("echo b")226        .assert()227        .failure()228        .stderr(predicate::str::contains(229            "The '--conclude' option has to be provided",230        ));231}232233#[test]234fn fails_with_duplicate_parameter_names() {235    hyperfine()236        .arg("--parameter-list")237        .arg("x")238        .arg("1,2,3")239        .arg("--parameter-list")240        .arg("x")241        .arg("a,b,c")242        .arg("echo test")243        .assert()244        .failure()245        .stderr(predicate::str::contains("Duplicate parameter names: x"));246}247248#[test]249fn fails_for_unknown_command() {250    hyperfine()251        .arg("--runs=1")252        .arg("some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")253        .assert()254        .failure()255        .stderr(predicate::str::contains(256            "Command terminated with non-zero exit code",257        ));258}259260#[test]261fn fails_for_unknown_command_without_shell() {262    hyperfine()263        .arg("--shell=none")264        .arg("--runs=1")265        .arg("some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")266        .assert()267        .failure()268        .stderr(predicate::str::contains(269            "Failed to run command 'some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577'",270        ));271}272273#[cfg(unix)]274#[test]275fn fails_for_failing_command_without_shell() {276    hyperfine()277        .arg("--shell=none")278        .arg("--runs=1")279        .arg("false")280        .assert()281        .failure()282        .stderr(predicate::str::contains(283            "Command terminated with non-zero exit code",284        ));285}286287#[test]288fn fails_for_unknown_setup_command() {289    hyperfine()290        .arg("--runs=1")291        .arg("--setup=some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")292        .arg("echo test")293        .assert()294        .failure()295        .stderr(predicate::str::contains(296            "The setup command terminated with a non-zero exit code.",297        ));298}299300#[test]301fn fails_for_unknown_cleanup_command() {302    hyperfine()303        .arg("--runs=1")304        .arg("--cleanup=some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")305        .arg("echo test")306        .assert()307        .failure()308        .stderr(predicate::str::contains(309            "The cleanup command terminated with a non-zero exit code.",310        ));311}312313#[test]314fn fails_for_unknown_prepare_command() {315    hyperfine()316        .arg("--prepare=some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")317        .arg("echo test")318        .assert()319        .failure()320        .stderr(predicate::str::contains(321            "The preparation command terminated with a non-zero exit code.",322        ));323}324325#[test]326fn fails_for_unknown_conclude_command() {327    hyperfine()328        .arg("--conclude=some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")329        .arg("echo test")330        .assert()331        .failure()332        .stderr(predicate::str::contains(333            "The conclusion command terminated with a non-zero exit code.",334        ));335}336337#[cfg(unix)]338#[test]339fn can_run_failing_commands_with_ignore_failure_option() {340    hyperfine()341        .arg("false")342        .assert()343        .failure()344        .stderr(predicate::str::contains(345            "Command terminated with non-zero exit code",346        ));347348    hyperfine()349        .arg("--runs=1")350        .arg("--ignore-failure")351        .arg("false")352        .assert()353        .success();354}355356#[cfg(unix)]357#[test]358fn can_ignore_specific_exit_codes() {359    // Test that specifying exit code 1 ignores it360    hyperfine()361        .arg("--runs=1")362        .arg("--ignore-failure=1")363        .arg("exit 1")364        .assert()365        .success();366367    // Test that other exit codes still fail368    hyperfine()369        .arg("--runs=1")370        .arg("--ignore-failure=1")371        .arg("exit 2")372        .assert()373        .failure()374        .stderr(predicate::str::contains(375            "Command terminated with non-zero exit code 2",376        ));377}378379#[cfg(unix)]380#[test]381fn can_ignore_multiple_exit_codes() {382    // Test that all specified exit codes are ignored383    hyperfine()384        .arg("--runs=1")385        .arg("--ignore-failure=1,2,3")386        .arg("exit 1")387        .assert()388        .success();389390    hyperfine()391        .arg("--runs=1")392        .arg("--ignore-failure=1,2,3")393        .arg("exit 2")394        .assert()395        .success();396397    hyperfine()398        .arg("--runs=1")399        .arg("--ignore-failure=1,2,3")400        .arg("exit 3")401        .assert()402        .success();403404    // Test that other exit codes still fail405    hyperfine()406        .arg("--runs=1")407        .arg("--ignore-failure=1,2,3")408        .arg("exit 4")409        .assert()410        .failure()411        .stderr(predicate::str::contains(412            "Command terminated with non-zero exit code 4",413        ));414}415416#[cfg(unix)]417#[test]418fn ignore_failure_with_all_non_zero() {419    // Test explicit "all-non-zero" mode420    hyperfine()421        .arg("--runs=1")422        .arg("--ignore-failure=all-non-zero")423        .arg("exit 5")424        .assert()425        .success();426}427428#[test]429fn shows_output_of_benchmarked_command() {430    hyperfine()431        .arg("--runs=2")432        .arg("--command-name=dummy")433        .arg("--show-output")434        .arg("echo 4fd47015")435        .assert()436        .success()437        .stdout(predicate::str::contains("4fd47015").count(2));438}439440#[test]441fn runs_commands_using_user_defined_shell() {442    hyperfine()443        .arg("--runs=1")444        .arg("--show-output")445        .arg("--shell")446        .arg("echo 'custom_shell' '--shell-arg'")447        .arg("echo benchmark")448        .assert()449        .success()450        .stdout(451            predicate::str::contains("custom_shell --shell-arg -c echo benchmark").or(452                predicate::str::contains("custom_shell --shell-arg /C echo benchmark"),453            ),454        );455}456457#[test]458fn can_pass_input_to_command_from_a_file() {459    hyperfine()460        .arg("--runs=1")461        .arg("--input=example_input_file.txt")462        .arg("--show-output")463        .arg(STDIN_READ_COMMAND)464        .assert()465        .success()466        .stdout(predicate::str::contains("This text is part of a file"));467}468469#[test]470fn fails_if_invalid_stdin_data_file_provided() {471    hyperfine()472        .arg("--runs=1")473        .arg("--input=example_non_existent_file.txt")474        .arg("--show-output")475        .arg(STDIN_READ_COMMAND)476        .assert()477        .failure()478        .stderr(predicate::str::contains(479            "The file 'example_non_existent_file.txt' specified as '--input' does not exist",480        ));481}482483#[test]484fn returns_mean_time_in_correct_unit() {485    hyperfine_debug()486        .arg("sleep 1.234")487        .assert()488        .success()489        .stdout(predicate::str::contains("Time (mean ± σ):      1.234 s ±"));490491    hyperfine_debug()492        .arg("sleep 0.123")493        .assert()494        .success()495        .stdout(predicate::str::contains("Time (mean ± σ):     123.0 ms ±"));496497    hyperfine_debug()498        .arg("--time-unit=millisecond")499        .arg("sleep 1.234")500        .assert()501        .success()502        .stdout(predicate::str::contains("Time (mean ± σ):     1234.0 ms ±"));503504    hyperfine_debug()505        .arg("--time-unit=microsecond")506        .arg("sleep 1.234")507        .assert()508        .success()509        .stdout(predicate::str::contains(510            "Time (mean ± σ):     1234000.0 µs ±",511        ));512}513514#[test]515fn performs_ten_runs_for_slow_commands() {516    hyperfine_debug()517        .arg("sleep 0.5")518        .assert()519        .success()520        .stdout(predicate::str::contains("10 runs"));521}522523#[test]524fn performs_three_seconds_of_benchmarking_for_fast_commands() {525    hyperfine_debug()526        .arg("sleep 0.01")527        .assert()528        .success()529        .stdout(predicate::str::contains("300 runs"));530}531532#[test]533fn takes_shell_spawning_time_into_account_for_computing_number_of_runs() {534    hyperfine_debug()535        .arg("--shell=sleep 0.02")536        .arg("sleep 0.01")537        .assert()538        .success()539        .stdout(predicate::str::contains("100 runs"));540}541542#[test]543fn takes_preparation_command_into_account_for_computing_number_of_runs() {544    hyperfine_debug()545        .arg("--prepare=sleep 0.02")546        .arg("sleep 0.01")547        .assert()548        .success()549        .stdout(predicate::str::contains("100 runs"));550551    // Shell overhead needs to be added to both the prepare command and the actual command,552    // leading to a total benchmark time of (prepare + shell + cmd + shell = 0.1 s)553    hyperfine_debug()554        .arg("--shell=sleep 0.01")555        .arg("--prepare=sleep 0.03")556        .arg("sleep 0.05")557        .assert()558        .success()559        .stdout(predicate::str::contains("30 runs"));560}561562#[test]563fn takes_conclusion_command_into_account_for_computing_number_of_runs() {564    hyperfine_debug()565        .arg("--conclude=sleep 0.02")566        .arg("sleep 0.01")567        .assert()568        .success()569        .stdout(predicate::str::contains("100 runs"));570571    // Shell overhead needs to be added to both the conclude command and the actual command,572    // leading to a total benchmark time of (cmd + shell + conclude + shell = 0.1 s)573    hyperfine_debug()574        .arg("--shell=sleep 0.01")575        .arg("--conclude=sleep 0.03")576        .arg("sleep 0.05")577        .assert()578        .success()579        .stdout(predicate::str::contains("30 runs"));580}581582#[test]583fn takes_both_preparation_and_conclusion_command_into_account_for_computing_number_of_runs() {584    hyperfine_debug()585        .arg("--prepare=sleep 0.01")586        .arg("--conclude=sleep 0.01")587        .arg("sleep 0.01")588        .assert()589        .success()590        .stdout(predicate::str::contains("100 runs"));591592    // Shell overhead needs to be added to both the prepare, conclude and the actual command,593    // leading to a total benchmark time of (prepare + shell + cmd + shell + conclude + shell = 0.1 s)594    hyperfine_debug()595        .arg("--shell=sleep 0.01")596        .arg("--prepare=sleep 0.01")597        .arg("--conclude=sleep 0.01")598        .arg("sleep 0.05")599        .assert()600        .success()601        .stdout(predicate::str::contains("30 runs"));602}603604#[test]605fn shows_benchmark_comparison_with_relative_times() {606    let _settings = snapshot_settings().bind_to_scope();607    assert_cmd_snapshot!(hyperfine_debug()608        .arg("--style=basic")609        .arg("sleep 1.0")610        .arg("sleep 2.0")611        .arg("sleep 3.0"), @r"612    success: true613    exit_code: 0614    ----- stdout -----615    Benchmark 1: sleep 1.0616      Time (mean ± σ):      1.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]617      Range (min … max):    1.000 s …  1.000 s    10 runs618619    Benchmark 2: sleep 2.0620      Time (mean ± σ):      2.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]621      Range (min … max):    2.000 s …  2.000 s    10 runs622623    Benchmark 3: sleep 3.0624      Time (mean ± σ):      3.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]625      Range (min … max):    3.000 s …  3.000 s    10 runs626627    Summary628      sleep 1.0 ran629        2.00 ± 0.00 times faster than sleep 2.0630        3.00 ± 0.00 times faster than sleep 3.0631632    ----- stderr -----633    ");634}635636#[test]637fn shows_benchmark_comparison_with_same_time() {638    let _settings = snapshot_settings().bind_to_scope();639    assert_cmd_snapshot!(hyperfine_debug()640        .arg("--style=basic")641        .arg("--command-name=A")642        .arg("--command-name=B")643        .arg("sleep 1.0")644        .arg("sleep 1.0")645        .arg("sleep 2.0")646        .arg("sleep 1000.0"), @r"647    success: true648    exit_code: 0649    ----- stdout -----650    Benchmark 1: A651      Time (mean ± σ):      1.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]652      Range (min … max):    1.000 s …  1.000 s    10 runs653654    Benchmark 2: B655      Time (mean ± σ):      1.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]656      Range (min … max):    1.000 s …  1.000 s    10 runs657658    Benchmark 3: sleep 2.0659      Time (mean ± σ):      2.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]660      Range (min … max):    2.000 s …  2.000 s    10 runs661662    Benchmark 4: sleep 1000.0663      Time (mean ± σ):     1000.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]664      Range (min … max):   1000.000 s … 1000.000 s    10 runs665666    Summary667      A ran668        As fast (1.00 ± 0.00) as B669        2.00 ± 0.00 times faster than sleep 2.0670     1000.00 ± 0.00 times faster than sleep 1000.0671672    ----- stderr -----673    ");674}675676#[test]677fn shows_benchmark_comparison_relative_to_reference() {678    let _settings = snapshot_settings().bind_to_scope();679    assert_cmd_snapshot!(hyperfine_debug()680        .arg("--style=basic")681        .arg("--reference=sleep 2.0")682        .arg("sleep 1.0")683        .arg("sleep 3.0"), @r"684    success: true685    exit_code: 0686    ----- stdout -----687    Benchmark 1: sleep 2.0688      Time (mean ± σ):      2.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]689      Range (min … max):    2.000 s …  2.000 s    10 runs690691    Benchmark 2: sleep 1.0692      Time (mean ± σ):      1.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]693      Range (min … max):    1.000 s …  1.000 s    10 runs694695    Benchmark 3: sleep 3.0696      Time (mean ± σ):      3.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]697      Range (min … max):    3.000 s …  3.000 s    10 runs698699    Summary700      sleep 2.0 ran701        2.00 ± 0.00 times slower than sleep 1.0702        1.50 ± 0.00 times faster than sleep 3.0703704    ----- stderr -----705    ");706}707708#[test]709fn shows_reference_name() {710    let _settings = snapshot_settings().bind_to_scope();711    assert_cmd_snapshot!(hyperfine_debug()712        .arg("--style=basic")713        .arg("--reference=sleep 2.0")714        .arg("--reference-name=refabc123")715        .arg("sleep 1.0")716        .arg("sleep 3.0"), @r"717    success: true718    exit_code: 0719    ----- stdout -----720    Benchmark 1: refabc123721      Time (mean ± σ):      2.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]722      Range (min … max):    2.000 s …  2.000 s    10 runs723724    Benchmark 2: sleep 1.0725      Time (mean ± σ):      1.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]726      Range (min … max):    1.000 s …  1.000 s    10 runs727728    Benchmark 3: sleep 3.0729      Time (mean ± σ):      3.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]730      Range (min … max):    3.000 s …  3.000 s    10 runs731732    Summary733      refabc123 ran734        2.00 ± 0.00 times slower than sleep 1.0735        1.50 ± 0.00 times faster than sleep 3.0736737    ----- stderr -----738    ");739}740741#[test]742fn performs_all_benchmarks_in_parameter_scan() {743    let _settings = snapshot_settings().bind_to_scope();744    assert_cmd_snapshot!(hyperfine_debug()745        .arg("--style=basic")746        .arg("--parameter-scan")747        .arg("time")748        .arg("30")749        .arg("45")750        .arg("--parameter-step-size")751        .arg("5")752        .arg("sleep {time}"), @r"753    success: true754    exit_code: 0755    ----- stdout -----756    Benchmark 1: sleep 30757      Time (mean ± σ):     30.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]758      Range (min … max):   30.000 s … 30.000 s    10 runs759760    Benchmark 2: sleep 35761      Time (mean ± σ):     35.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]762      Range (min … max):   35.000 s … 35.000 s    10 runs763764    Benchmark 3: sleep 40765      Time (mean ± σ):     40.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]766      Range (min … max):   40.000 s … 40.000 s    10 runs767768    Benchmark 4: sleep 45769      Time (mean ± σ):     45.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]770      Range (min … max):   45.000 s … 45.000 s    10 runs771772    Summary773      sleep 30 ran774        1.17 ± 0.00 times faster than sleep 35775        1.33 ± 0.00 times faster than sleep 40776        1.50 ± 0.00 times faster than sleep 45777778    ----- stderr -----779    ");780}781782#[test]783fn rejects_negative_parameter_steps() {784    for step in ["-1", "-1.0"] {785        hyperfine()786            .args(["--parameter-scan", "n", "0", "1"])787            .arg(format!("--parameter-step-size={step}"))788            .arg("echo {n}")789            .assert()790            .code(1)791            .stdout(predicate::str::is_empty())792            .stderr(predicate::str::contains(793                "Parameter step size must be positive",794            ));795    }796}797798#[test]799fn performs_reference_and_all_benchmarks_in_parameter_scan() {800    let _settings = snapshot_settings().bind_to_scope();801    assert_cmd_snapshot!(hyperfine_debug()802        .arg("--style=basic")803        .arg("--reference=sleep 25")804        .arg("--parameter-scan")805        .arg("time")806        .arg("30")807        .arg("45")808        .arg("--parameter-step-size")809        .arg("5")810        .arg("sleep {time}"), @r"811    success: true812    exit_code: 0813    ----- stdout -----814    Benchmark 1: sleep 25815      Time (mean ± σ):     25.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]816      Range (min … max):   25.000 s … 25.000 s    10 runs817818    Benchmark 2: sleep 30819      Time (mean ± σ):     30.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]820      Range (min … max):   30.000 s … 30.000 s    10 runs821822    Benchmark 3: sleep 35823      Time (mean ± σ):     35.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]824      Range (min … max):   35.000 s … 35.000 s    10 runs825826    Benchmark 4: sleep 40827      Time (mean ± σ):     40.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]828      Range (min … max):   40.000 s … 40.000 s    10 runs829830    Benchmark 5: sleep 45831      Time (mean ± σ):     45.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]832      Range (min … max):   45.000 s … 45.000 s    10 runs833834    Summary835      sleep 25 ran836        1.20 ± 0.00 times faster than sleep 30837        1.40 ± 0.00 times faster than sleep 35838        1.60 ± 0.00 times faster than sleep 40839        1.80 ± 0.00 times faster than sleep 45840841    ----- stderr -----842    ");843}844845#[test]846fn intermediate_results_are_not_exported_to_stdout() {847    let _settings = snapshot_settings().bind_to_scope();848    assert_cmd_snapshot!(hyperfine_debug()849        .arg("--style=none") // To only see the Markdown export on stdout850        .arg("--export-markdown")851        .arg("-")852        .arg("sleep 1")853        .arg("sleep 2"), @r"854    success: true855    exit_code: 0856    ----- stdout -----857858    | Command | Mean [s] | Min [s] | Max [s] | Relative |859    |:---|---:|---:|---:|---:|860    | `sleep 1` | 1.000 ± 0.000 | 1.000 | 1.000 | 1.00 |861    | `sleep 2` | 2.000 ± 0.000 | 2.000 | 2.000 | 2.00 ± 0.00 |862863864    ----- stderr -----865    ");866}867868#[test]869#[cfg(unix)]870fn exports_intermediate_results_to_file() {871    use tempfile::tempdir;872873    let tempdir = tempdir().unwrap();874    let export_path = tempdir.path().join("results.md");875876    hyperfine()877        .arg("--runs=1")878        .arg("--export-markdown")879        .arg(&export_path)880        .arg("true")881        .arg("false")882        .assert()883        .failure();884885    let contents = std::fs::read_to_string(export_path).unwrap();886    assert!(contents.contains("true"));887}888889#[test]890fn invalid_command_options_preserve_export_files() {891    use tempfile::tempdir;892893    for (option, values) in [894        ("--prepare", ["echo first", "echo second"]),895        ("--conclude", ["echo first", "echo second"]),896        ("--output", ["null", "pipe"]),897    ] {898        let directory = tempdir().unwrap();899        let existing_export = directory.path().join("previous.json");900        let new_export = directory.path().join("new.csv");901        let previous_results =902            b"{\"results\":[{\"command\":\"previous benchmark\",\"mean\":1.0}]}\n";903        std::fs::write(&existing_export, previous_results).unwrap();904905        hyperfine()906            .arg("--runs=1")907            .arg("--export-json")908            .arg(&existing_export)909            .arg("--export-csv")910            .arg(&new_export)911            .args([option, values[0], option, values[1]])912            .arg("echo benchmark")913            .assert()914            .failure()915            .stderr(predicate::str::contains(format!(916                "The '{option}' option has to be provided just once or N times"917            )));918919        assert_eq!(920            std::fs::read(&existing_export).unwrap(),921            previous_results,922            "{option} validation must preserve previous results"923        );924        assert!(925            !new_export.exists(),926            "{} validation must not create export files",927            option928        );929    }930}931932#[test]933fn markdown_export_preserves_backticks_and_pipes_in_command_names() {934    let _settings = snapshot_settings().bind_to_scope();935    assert_cmd_snapshot!(hyperfine_debug()936        .arg("--style=none")937        .arg("--export-markdown=-")938        .arg("--command-name=echo `uname` | cat")939        .arg("sleep 1"), @r"940    success: true941    exit_code: 0942    ----- stdout -----943944    | Command | Mean [s] | Min [s] | Max [s] | Relative |945    |:---|---:|---:|---:|---:|946    | `` echo `uname` \| cat `` | 1.000 ± 0.000 | 1.000 | 1.000 | 1.00 |947948949    ----- stderr -----950    ");951}952953#[test]954fn unused_parameters_are_shown_in_benchmark_name() {955    hyperfine()956        .arg("--runs=2")957        .arg("--parameter-list")958        .arg("branch")959        .arg("master,feature")960        .arg("echo test")961        .assert()962        .success()963        .stdout(964            predicate::str::contains("echo test (branch = master)")965                .and(predicate::str::contains("echo test (branch = feature)")),966        );967}968969#[test]970fn speed_comparison_sort_order() {971    let _settings = snapshot_settings().bind_to_scope();972    insta::allow_duplicates! {973        for sort_order in ["auto", "mean-time"] {974            assert_cmd_snapshot!(hyperfine_debug()975                .arg("--style=basic")976                .arg("sleep 2")977                .arg("sleep 1")978                .arg(format!("--sort={sort_order}")), @r"979            success: true980            exit_code: 0981            ----- stdout -----982            Benchmark 1: sleep 2983              Time (mean ± σ):      2.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]984              Range (min … max):    2.000 s …  2.000 s    10 runs985986            Benchmark 2: sleep 1987              Time (mean ± σ):      1.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]988              Range (min … max):    1.000 s …  1.000 s    10 runs989990            Summary991              sleep 1 ran992                2.00 ± 0.00 times faster than sleep 2993994            ----- stderr -----995            ");996        }997    }998999    assert_cmd_snapshot!(hyperfine_debug()1000        .arg("--style=basic")1001        .arg("sleep 2")1002        .arg("sleep 1")1003        .arg("--sort=command"), @r"1004    success: true1005    exit_code: 01006    ----- stdout -----1007    Benchmark 1: sleep 21008      Time (mean ± σ):      2.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]1009      Range (min … max):    2.000 s …  2.000 s    10 runs10101011    Benchmark 2: sleep 11012      Time (mean ± σ):      1.000 s ±  0.000 s    [User: 0.000 s, System: 0.000 s]1013      Range (min … max):    1.000 s …  1.000 s    10 runs10141015    Relative speed comparison1016            2.00 ±  0.00  sleep 21017            1.00          sleep 110181019    ----- stderr -----1020    ");1021}10221023#[cfg(windows)]1024#[test]1025fn windows_quote_args() {1026    hyperfine()1027        .arg("more \"example_input_file.txt\"")1028        .assert()1029        .success();1030}10311032#[cfg(windows)]1033#[test]1034fn windows_quote_before_quote_args() {1035    hyperfine()1036        .arg("dir \"..\\src\\\" \"..\\tests\\\"")1037        .assert()1038        .success();1039}10401041#[test]1042fn hyperfine_iteration_env_var_in_prepare_and_conclude_commands() {1043    let tempdir = tempfile::tempdir().unwrap();1044    let output_path = tempdir.path().join("iteration output.txt");1045    let iteration = if cfg!(windows) {1046        "%HYPERFINE_ITERATION%"1047    } else {1048        "$HYPERFINE_ITERATION"1049    };1050    let command = |phase| format!(r#"echo {phase}:{iteration} >> "iteration output.txt""#);10511052    hyperfine()1053        .current_dir(tempdir.path())1054        .arg("--runs=2")1055        .arg("--warmup=1")1056        .arg("--prepare")1057        .arg(command("prepare"))1058        .arg(command("benchmark"))1059        .arg("--conclude")1060        .arg(command("conclude"))1061        .assert()1062        .success();10631064    let contents = std::fs::read_to_string(output_path).unwrap();1065    // cmd.exe's echo includes the space before the redirection operator.1066    let lines: Vec<&str> = contents.lines().map(str::trim_end).collect();1067    assert_eq!(1068        lines,1069        [1070            "prepare:warmup-0",1071            "benchmark:warmup-0",1072            "conclude:warmup-0",1073            "prepare:0",1074            "benchmark:0",1075            "conclude:0",1076            "prepare:1",1077            "benchmark:1",1078            "conclude:1",1079        ]1080    );1081}