Skip to content
Myr

Use insta-cmd for benchmark report snapshots

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Peter · committed by David Peter · parent c8f8c4e · Browse files · 3 files+115 added−65 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+115 added−65 removed

  • tests:+81 added−64 removed
  • other:+34 added−1 removed

3 files+115 added−65 removed

modified:tests/integration_tests.rs+81 added-64 removed
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" };1312pub fn hyperfine_debug() -> assert_cmd::Command {13 let mut cmd = hyperfine();14pub fn hyperfine_debug() -> std::process::Command {15 let mut cmd = hyperfine_raw_command();16 cmd.arg("--debug-mode");17 cmd18}1918// Ignore trailing padding on report lines, including the space-only separators.19// Keep indentation and internal spacing so snapshots still check the layout.20fn stdout(output: &assert_cmd::assert::Assert) -> String {21 String::from_utf8_lossy(&output.get_output().stdout)22 .lines()23 .map(str::trim_end)24 .collect::<Vec<_>>()25 .join("\n")20fn 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() {
573 unmodified lines
602}603604#[test]605fn shows_benchmark_comparison_with_relative_times() {607 let output = hyperfine_debug()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")612 .assert()613 .success();614615 insta::assert_snapshot!(stdout(&output), @r"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 runs618
8 unmodified lines
627 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() {637 let output = hyperfine_debug()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")644 .arg("sleep 1000.0")645 .assert()646 .success();647648 insta::assert_snapshot!(stdout(&output), @r"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 runs653
13 unmodified lines
667 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() {675 let output = hyperfine_debug()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")679 .arg("sleep 3.0")680 .assert()681 .success();682683 insta::assert_snapshot!(stdout(&output), @r"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 runs690
8 unmodified lines
699 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() {705 let output = hyperfine_debug()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")710 .arg("sleep 3.0")711 .assert()712 .success();713714 insta::assert_snapshot!(stdout(&output), @r"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 runs723
8 unmodified lines
732 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() {736 let output = hyperfine_debug()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")744 .arg("sleep {time}")745 .assert()746 .success();747748 insta::assert_snapshot!(stdout(&output), @r"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 runs759
13 unmodified lines
773 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 performs_reference_and_all_benchmarks_in_parameter_scan() {775 let output = hyperfine_debug()784 let _settings = snapshot_settings().bind_to_scope();785 assert_cmd_snapshot!(hyperfine_debug()786 .arg("--style=basic")787 .arg("--reference=sleep 25")788 .arg("--parameter-scan")789 .arg("time")790 .arg("30")791 .arg("45")792 .arg("--parameter-step-size")793 .arg("5")784 .arg("sleep {time}")785 .assert()786 .success();787788 insta::assert_snapshot!(stdout(&output), @r"794 .arg("sleep {time}"), @r"795 success: true796 exit_code: 0797 ----- stdout -----798 Benchmark 1: sleep 25799 Time (mean ± σ): 25.000 s ± 0.000 s [User: 0.000 s, System: 0.000 s]800 Range (min … max): 25.000 s … 25.000 s 10 runs801
18 unmodified lines
820 1.20 ± 0.00 times faster than sleep 30821 1.40 ± 0.00 times faster than sleep 35822 1.60 ± 0.00 times faster than sleep 40823 1.80 ± 0.00 times faster than sleep 45824825 ----- stderr -----826 ");827}828829#[test]830fn intermediate_results_are_not_exported_to_stdout() {820 let output = hyperfine_debug()831 let _settings = snapshot_settings().bind_to_scope();832 assert_cmd_snapshot!(hyperfine_debug()833 .arg("--style=none") // To only see the Markdown export on stdout834 .arg("--export-markdown")835 .arg("-")836 .arg("sleep 1")825 .arg("sleep 2")826 .assert()827 .success();837 .arg("sleep 2"), @r"838 success: true839 exit_code: 0840 ----- stdout -----841829 insta::assert_snapshot!(stdout(&output), @r"830842 | Command | Mean [s] | Min [s] | Max [s] | Relative |843 |:---|---:|---:|---:|---:|844 | `sleep 1` | 1.000 ± 0.000 | 1.000 | 1.000 | 1.00 |845 | `sleep 2` | 2.000 ± 0.000 | 2.000 | 2.000 | 2.00 ± 0.00 |846847848 ----- stderr -----849 ");850}851852#[test]
34 unmodified lines
887}888889#[test]890fn speed_comparison_sort_order() {891 let _settings = snapshot_settings().bind_to_scope();892 insta::allow_duplicates! {893 for sort_order in ["auto", "mean-time"] {879 let output = hyperfine_debug()894 assert_cmd_snapshot!(hyperfine_debug()895 .arg("--style=basic")896 .arg("sleep 2")897 .arg("sleep 1")883 .arg(format!("--sort={sort_order}"))884 .assert()885 .success();886887 insta::assert_snapshot!(stdout(&output), @r"898 .arg(format!("--sort={sort_order}")), @r"899 success: true900 exit_code: 0901 ----- stdout -----902 Benchmark 1: sleep 2903 Time (mean ± σ): 2.000 s ± 0.000 s [User: 0.000 s, System: 0.000 s]904 Range (min … max): 2.000 s … 2.000 s 10 runs905
3 unmodified lines
909910 Summary911 sleep 1 ran912 2.00 ± 0.00 times faster than sleep 2913914 ----- stderr -----915 ");916 }917 }918903 let output = hyperfine_debug()919 assert_cmd_snapshot!(hyperfine_debug()920 .arg("--style=basic")921 .arg("sleep 2")922 .arg("sleep 1")907 .arg("--sort=command")908 .assert()909 .success();910911 insta::assert_snapshot!(stdout(&output), @r"923 .arg("--sort=command"), @r"924 success: true925 exit_code: 0926 ----- stdout -----927 Benchmark 1: sleep 2928 Time (mean ± σ): 2.000 s ± 0.000 s [User: 0.000 s, System: 0.000 s]929 Range (min … max): 2.000 s … 2.000 s 10 runs930
3 unmodified lines
934935 Relative speed comparison936 2.00 ± 0.00 sleep 2937 1.00 sleep 1938939 ----- stderr -----940 ");941}942943#[cfg(windows)]
16 unmodified lines
modified:Cargo.lock+32 added
410 unmodified lines
411 "colored",412 "csv",413 "indicatif",414 "insta",415 "insta-cmd",416 "libc",417 "nix",418 "once_cell",419 "predicates",
38 unmodified lines
458checksum = "86f0f8fee8c926415c58d6ae43a08523a26faccb2323f5e6b644fe7dd4ef6b82"459dependencies = [460 "console 0.16.6",461 "once_cell",462 "regex",463 "serde",464 "similar",465 "strip-ansi-escapes",466 "tempfile",467]468469[[package]]470name = "insta-cmd"471version = "0.7.0"472source = "registry+https://github.com/rust-lang/crates.io-index"473checksum = "bffdf4af1db390cf0401535d7c1303cd079a074d28d8473b026fdb6559c41403"474dependencies = [475 "insta",476 "serde",477 "serde_json",478]479480[[package]]481name = "instant"482version = "0.1.13"483source = "registry+https://github.com/rust-lang/crates.io-index"484checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
553 unmodified lines
1038 "rand 0.6.5",1039]10401041[[package]]1042name = "strip-ansi-escapes"1043version = "0.2.1"1044source = "registry+https://github.com/rust-lang/crates.io-index"1045checksum = "2a8f8038e7e7969abb3f1b7c2a811225e9296da208539e0f79c5251d6cac0025"1046dependencies = [1047 "vte",1048]10491050[[package]]1051name = "strsim"1052version = "0.11.1"1053source = "registry+https://github.com/rust-lang/crates.io-index"1054checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
123 unmodified lines
1178source = "registry+https://github.com/rust-lang/crates.io-index"1179checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"11801181[[package]]1182name = "vte"1183version = "0.14.1"1184source = "registry+https://github.com/rust-lang/crates.io-index"1185checksum = "231fdcd7ef3037e8330d8e17e61011a2c244126acc0a982f4040ac3f9f0bc077"1186dependencies = [1187 "memchr",1188]11891190[[package]]1191name = "wait-timeout"1192version = "0.2.1"1193source = "registry+https://github.com/rust-lang/crates.io-index"1194checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11"
215 unmodified lines
modified:Cargo.toml+2 added-1 removed