Skip to content
Myr

tests.rs

This file

Path

src/export/tests.rs

425 lines · 16.0 KiB · Rust

Last changed by

Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520

History of this file

Take it

1use super::Exporter;2use crate::benchmark::benchmark_result::BenchmarkResult;3use crate::benchmark::measurement::{Measurement, Measurements};4use crate::export::asciidoc::AsciidocExporter;5use crate::export::orgmode::OrgmodeExporter;6use crate::quantity::{byte, second, Information, Time, TimeUnit, Zero};7use crate::{export::markdown::MarkdownExporter, options::SortOrder};8use std::collections::BTreeMap;9use std::process::ExitStatus;1011fn get_output<E: Exporter + Default>(12    results: &[BenchmarkResult],13    unit: Option<TimeUnit>,14    sort_order: SortOrder,15) -> String {16    let exporter = E::default();17    String::from_utf8(exporter.serialize(results, unit, sort_order).unwrap()).unwrap()18}1920/// Ensure the makrup output includes the table header and the multiple21/// benchmark results as a table. The list of actual times is not included22/// in the output.23///24/// This also demonstrates that the first entry's units (ms) are used to set25/// the units for all entries when the time unit is not specified.26#[test]27fn test_markup_export_auto_ms() {28    let results = [29        BenchmarkResult {30            command: String::from("sleep 0.1"),31            command_with_unused_parameters: String::from("sleep 0.1"),32            measurements: Measurements::new(vec![33                Measurement {34                    time_wall_clock: Time::new::<second>(0.09),35                    time_user: Time::new::<second>(0.09),36                    time_system: Time::zero(),37                    peak_memory_usage: Information::new::<byte>(1024.),38                    exit_status: ExitStatus::default(),39                },40                Measurement {41                    time_wall_clock: Time::new::<second>(0.10),42                    time_user: Time::new::<second>(0.10),43                    time_system: Time::zero(),44                    peak_memory_usage: Information::new::<byte>(1024.),45                    exit_status: ExitStatus::default(),46                },47                Measurement {48                    time_wall_clock: Time::new::<second>(0.14),49                    time_user: Time::new::<second>(0.14),50                    time_system: Time::zero(),51                    peak_memory_usage: Information::new::<byte>(1024.),52                    exit_status: ExitStatus::default(),53                },54            ]),55            parameters: BTreeMap::new(),56        },57        BenchmarkResult {58            command: String::from("sleep 2"),59            command_with_unused_parameters: String::from("sleep 2"),60            measurements: Measurements::new(vec![61                Measurement {62                    time_wall_clock: Time::new::<second>(2.0),63                    time_user: Time::new::<second>(2.0),64                    time_system: Time::zero(),65                    peak_memory_usage: Information::new::<byte>(1024.),66                    exit_status: ExitStatus::default(),67                },68                Measurement {69                    time_wall_clock: Time::new::<second>(3.0),70                    time_user: Time::new::<second>(3.0),71                    time_system: Time::zero(),72                    peak_memory_usage: Information::new::<byte>(1024.),73                    exit_status: ExitStatus::default(),74                },75                Measurement {76                    time_wall_clock: Time::new::<second>(4.0),77                    time_user: Time::new::<second>(4.0),78                    time_system: Time::zero(),79                    peak_memory_usage: Information::new::<byte>(1024.),80                    exit_status: ExitStatus::default(),81                },82            ]),83            parameters: BTreeMap::new(),84        },85    ];8687    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None, SortOrder::Command), @r#"88    | Command | Mean [ms] | Min [ms] | Max [ms] | Relative |89    |:---|---:|---:|---:|---:|90    | `sleep 0.1` | 110.0 ± 26.5 | 90.0 | 140.0 | 1.00 |91    | `sleep 2` | 3000.0 ± 1000.0 | 2000.0 | 4000.0 | 27.27 ± 11.21 |92    "#);9394    insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None, SortOrder::Command), @r#"95    [cols="<,>,>,>,>"]96    |===97    | Command 98    | Mean [ms] 99    | Min [ms] 100    | Max [ms] 101    | Relative 102103    | `sleep 0.1` 104    | 110.0 ± 26.5 105    | 90.0 106    | 140.0 107    | 1.00 108109    | `sleep 2` 110    | 3000.0 ± 1000.0 111    | 2000.0 112    | 4000.0 113    | 27.27 ± 11.21 114    |===115    "#);116117    insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None, SortOrder::Command), @r#"118    | Command  |  Mean [ms] |  Min [ms] |  Max [ms] |  Relative |119    |--+--+--+--+--|120    | =sleep 0.1=  |  110.0 ± 26.5 |  90.0 |  140.0 |  1.00 |121    | =sleep 2=  |  3000.0 ± 1000.0 |  2000.0 |  4000.0 |  27.27 ± 11.21 |122    "#);123}124125/// This (again) demonstrates that the first entry's units (s) are used to set126/// the units for all entries when the time unit is not given.127#[test]128fn test_markup_export_auto_s() {129    let results = [130        BenchmarkResult {131            command: String::from("sleep 2"),132            command_with_unused_parameters: String::from("sleep 2"),133            measurements: Measurements::new(vec![134                Measurement {135                    time_wall_clock: Time::new::<second>(2.1),136                    time_user: Time::new::<second>(2.1),137                    time_system: Time::zero(),138                    peak_memory_usage: Information::new::<byte>(1024.),139                    exit_status: ExitStatus::default(),140                },141                Measurement {142                    time_wall_clock: Time::new::<second>(2.2),143                    time_user: Time::new::<second>(2.2),144                    time_system: Time::zero(),145                    peak_memory_usage: Information::new::<byte>(1024.),146                    exit_status: ExitStatus::default(),147                },148                Measurement {149                    time_wall_clock: Time::new::<second>(2.3),150                    time_user: Time::new::<second>(2.3),151                    time_system: Time::zero(),152                    peak_memory_usage: Information::new::<byte>(1024.),153                    exit_status: ExitStatus::default(),154                },155            ]),156            parameters: BTreeMap::new(),157        },158        BenchmarkResult {159            command: String::from("sleep 0.1"),160            command_with_unused_parameters: String::from("sleep 0.1"),161            measurements: Measurements::new(vec![162                Measurement {163                    time_wall_clock: Time::new::<second>(0.1),164                    time_user: Time::new::<second>(0.1),165                    time_system: Time::zero(),166                    peak_memory_usage: Information::new::<byte>(1024.),167                    exit_status: ExitStatus::default(),168                },169                Measurement {170                    time_wall_clock: Time::new::<second>(0.2),171                    time_user: Time::new::<second>(0.2),172                    time_system: Time::zero(),173                    peak_memory_usage: Information::new::<byte>(1024.),174                    exit_status: ExitStatus::default(),175                },176                Measurement {177                    time_wall_clock: Time::new::<second>(0.3),178                    time_user: Time::new::<second>(0.3),179                    time_system: Time::zero(),180                    peak_memory_usage: Information::new::<byte>(1024.),181                    exit_status: ExitStatus::default(),182                },183            ]),184            parameters: BTreeMap::new(),185        },186    ];187188    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None, SortOrder::Command), @r#"189    | Command | Mean [s] | Min [s] | Max [s] | Relative |190    |:---|---:|---:|---:|---:|191    | `sleep 2` | 2.200 ± 0.100 | 2.100 | 2.300 | 11.00 ± 5.52 |192    | `sleep 0.1` | 0.200 ± 0.100 | 0.100 | 0.300 | 1.00 |193    "#);194195    insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None, SortOrder::Command), @r#"196    [cols="<,>,>,>,>"]197    |===198    | Command 199    | Mean [s] 200    | Min [s] 201    | Max [s] 202    | Relative 203204    | `sleep 2` 205    | 2.200 ± 0.100 206    | 2.100 207    | 2.300 208    | 11.00 ± 5.52 209210    | `sleep 0.1` 211    | 0.200 ± 0.100 212    | 0.100 213    | 0.300 214    | 1.00 215    |===216    "#);217218    insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None, SortOrder::Command), @r#"219    | Command  |  Mean [s] |  Min [s] |  Max [s] |  Relative |220    |--+--+--+--+--|221    | =sleep 2=  |  2.200 ± 0.100 |  2.100 |  2.300 |  11.00 ± 5.52 |222    | =sleep 0.1=  |  0.200 ± 0.100 |  0.100 |  0.300 |  1.00 |223    "#);224}225226/// This (again) demonstrates that the given time unit (ms) is used to set227/// the units for all entries.228#[test]229fn test_markup_export_manual_ms() {230    let timing_results = [231        BenchmarkResult {232            command: String::from("sleep 2"),233            command_with_unused_parameters: String::from("sleep 2"),234            measurements: Measurements::new(vec![235                Measurement {236                    time_wall_clock: Time::new::<second>(2.1),237                    time_user: Time::new::<second>(2.1),238                    time_system: Time::zero(),239                    peak_memory_usage: Information::new::<byte>(1024.),240                    exit_status: ExitStatus::default(),241                },242                Measurement {243                    time_wall_clock: Time::new::<second>(2.2),244                    time_user: Time::new::<second>(2.2),245                    time_system: Time::zero(),246                    peak_memory_usage: Information::new::<byte>(1024.),247                    exit_status: ExitStatus::default(),248                },249                Measurement {250                    time_wall_clock: Time::new::<second>(2.3),251                    time_user: Time::new::<second>(2.3),252                    time_system: Time::zero(),253                    peak_memory_usage: Information::new::<byte>(1024.),254                    exit_status: ExitStatus::default(),255                },256            ]),257            parameters: BTreeMap::new(),258        },259        BenchmarkResult {260            command: String::from("sleep 0.1"),261            command_with_unused_parameters: String::from("sleep 0.1"),262            measurements: Measurements::new(vec![263                Measurement {264                    time_wall_clock: Time::new::<second>(0.1),265                    time_user: Time::new::<second>(0.1),266                    time_system: Time::zero(),267                    peak_memory_usage: Information::new::<byte>(1024.),268                    exit_status: ExitStatus::default(),269                },270                Measurement {271                    time_wall_clock: Time::new::<second>(0.2),272                    time_user: Time::new::<second>(0.2),273                    time_system: Time::zero(),274                    peak_memory_usage: Information::new::<byte>(1024.),275                    exit_status: ExitStatus::default(),276                },277                Measurement {278                    time_wall_clock: Time::new::<second>(0.3),279                    time_user: Time::new::<second>(0.3),280                    time_system: Time::zero(),281                    peak_memory_usage: Information::new::<byte>(1024.),282                    exit_status: ExitStatus::default(),283                },284            ]),285            parameters: BTreeMap::new(),286        },287    ];288289    insta::assert_snapshot!(get_output::<MarkdownExporter>(&timing_results, Some(TimeUnit::MilliSecond), SortOrder::Command), @r#"290    | Command | Mean [ms] | Min [ms] | Max [ms] | Relative |291    |:---|---:|---:|---:|---:|292    | `sleep 2` | 2200.0 ± 100.0 | 2100.0 | 2300.0 | 11.00 ± 5.52 |293    | `sleep 0.1` | 200.0 ± 100.0 | 100.0 | 300.0 | 1.00 |294    "#);295296    insta::assert_snapshot!(get_output::<AsciidocExporter>(&timing_results, Some(TimeUnit::MilliSecond), SortOrder::Command), @r#"297    [cols="<,>,>,>,>"]298    |===299    | Command 300    | Mean [ms] 301    | Min [ms] 302    | Max [ms] 303    | Relative 304305    | `sleep 2` 306    | 2200.0 ± 100.0 307    | 2100.0 308    | 2300.0 309    | 11.00 ± 5.52 310311    | `sleep 0.1` 312    | 200.0 ± 100.0 313    | 100.0 314    | 300.0 315    | 1.00 316    |===317    "#);318319    insta::assert_snapshot!(get_output::<OrgmodeExporter>(&timing_results, Some(TimeUnit::MilliSecond), SortOrder::Command), @r#"320    | Command  |  Mean [ms] |  Min [ms] |  Max [ms] |  Relative |321    |--+--+--+--+--|322    | =sleep 2=  |  2200.0 ± 100.0 |  2100.0 |  2300.0 |  11.00 ± 5.52 |323    | =sleep 0.1=  |  200.0 ± 100.0 |  100.0 |  300.0 |  1.00 |324    "#);325}326327/// The given time unit (s) is used to set the units for all entries.328#[test]329fn test_markup_export_manual_s() {330    let results = [331        BenchmarkResult {332            command: String::from("sleep 2"),333            command_with_unused_parameters: String::from("sleep 2"),334            measurements: Measurements::new(vec![335                Measurement {336                    time_wall_clock: Time::new::<second>(2.01),337                    time_user: Time::new::<second>(2.01),338                    time_system: Time::zero(),339                    peak_memory_usage: Information::new::<byte>(1024.),340                    exit_status: ExitStatus::default(),341                },342                Measurement {343                    time_wall_clock: Time::new::<second>(2.02),344                    time_user: Time::new::<second>(2.02),345                    time_system: Time::zero(),346                    peak_memory_usage: Information::new::<byte>(1024.),347                    exit_status: ExitStatus::default(),348                },349                Measurement {350                    time_wall_clock: Time::new::<second>(2.03),351                    time_user: Time::new::<second>(2.03),352                    time_system: Time::zero(),353                    peak_memory_usage: Information::new::<byte>(1024.),354                    exit_status: ExitStatus::default(),355                },356            ]),357            parameters: BTreeMap::new(),358        },359        BenchmarkResult {360            command: String::from("sleep 0.1"),361            command_with_unused_parameters: String::from("sleep 0.1"),362            measurements: Measurements::new(vec![363                Measurement {364                    time_wall_clock: Time::new::<second>(0.11),365                    time_user: Time::new::<second>(0.11),366                    time_system: Time::zero(),367                    peak_memory_usage: Information::new::<byte>(1024.),368                    exit_status: ExitStatus::default(),369                },370                Measurement {371                    time_wall_clock: Time::new::<second>(0.12),372                    time_user: Time::new::<second>(0.12),373                    time_system: Time::zero(),374                    peak_memory_usage: Information::new::<byte>(1024.),375                    exit_status: ExitStatus::default(),376                },377                Measurement {378                    time_wall_clock: Time::new::<second>(0.13),379                    time_user: Time::new::<second>(0.13),380                    time_system: Time::zero(),381                    peak_memory_usage: Information::new::<byte>(1024.),382                    exit_status: ExitStatus::default(),383                },384            ]),385            parameters: BTreeMap::new(),386        },387    ];388389    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, Some(TimeUnit::Second), SortOrder::Command), @r#"390    | Command | Mean [s] | Min [s] | Max [s] | Relative |391    |:---|---:|---:|---:|---:|392    | `sleep 2` | 2.020 ± 0.010 | 2.010 | 2.030 | 16.83 ± 1.41 |393    | `sleep 0.1` | 0.120 ± 0.010 | 0.110 | 0.130 | 1.00 |394    "#);395396    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, Some(TimeUnit::Second), SortOrder::MeanTime), @r#"397    | Command | Mean [s] | Min [s] | Max [s] | Relative |398    |:---|---:|---:|---:|---:|399    | `sleep 0.1` | 0.120 ± 0.010 | 0.110 | 0.130 | 1.00 |400    | `sleep 2` | 2.020 ± 0.010 | 2.010 | 2.030 | 16.83 ± 1.41 |401    "#);402403    insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, Some(TimeUnit::Second), SortOrder::Command), @r#"404    [cols="<,>,>,>,>"]405    |===406    | Command 407    | Mean [s] 408    | Min [s] 409    | Max [s] 410    | Relative 411412    | `sleep 2` 413    | 2.020 ± 0.010 414    | 2.010 415    | 2.030 416    | 16.83 ± 1.41 417418    | `sleep 0.1` 419    | 0.120 ± 0.010 420    | 0.110 421    | 0.130 422    | 1.00 423    |===424    "#);425}