Skip to content
Myr

tests.rs

This file

Path

src/export/tests.rs

478 lines · 18.5 KiB · Rust

Last changed by

Add --env for benchmark environment overridesDavid Peterbefore adoption6dddb02

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::markdown::MarkdownExporter;6use crate::export::orgmode::OrgmodeExporter;7use crate::metric::{MetricSelection, Unit};8use crate::quantity::{byte, second, Information, Time, Zero};9use std::collections::BTreeMap;10use std::process::ExitStatus;1112fn get_output<E: Exporter + Default>(results: &[BenchmarkResult], unit: Option<Unit>) -> String {13    let exporter = E::default();14    String::from_utf8(15        exporter16            .serialize(17                results,18                &[MetricSelection {19                    unit,20                    ..MetricSelection::default()21                }],22            )23            .unwrap(),24    )25    .unwrap()26}2728/// Ensure the makrup output includes the table header and the multiple29/// benchmark results as a table. The list of actual times is not included30/// in the output.31///32/// This also demonstrates that the first entry's units (ms) are used to set33/// the units for all entries when the time unit is not specified.34#[test]35fn test_markup_export_auto_ms() {36    let results = [37        BenchmarkResult {38            environment: BTreeMap::new(),39            command: String::from("sleep 0.1"),40            name: None,41            display_name: String::from("sleep 0.1"),42            measurements: Measurements::new(vec![43                Measurement {44                    time_wall_clock: Time::new::<second>(0.09),45                    time_cpu: Time::new::<second>(0.09),46                    time_user: Time::new::<second>(0.09),47                    time_system: Time::zero(),48                    memory_peak_resident: Some(Information::new::<byte>(1024.)),49                    hardware_counters: Default::default(),50                    exit_status: ExitStatus::default(),51                },52                Measurement {53                    time_wall_clock: Time::new::<second>(0.10),54                    time_cpu: Time::new::<second>(0.10),55                    time_user: Time::new::<second>(0.10),56                    time_system: Time::zero(),57                    memory_peak_resident: Some(Information::new::<byte>(1024.)),58                    hardware_counters: Default::default(),59                    exit_status: ExitStatus::default(),60                },61                Measurement {62                    time_wall_clock: Time::new::<second>(0.14),63                    time_cpu: Time::new::<second>(0.14),64                    time_user: Time::new::<second>(0.14),65                    time_system: Time::zero(),66                    memory_peak_resident: Some(Information::new::<byte>(1024.)),67                    hardware_counters: Default::default(),68                    exit_status: ExitStatus::default(),69                },70            ]),71            parameters: BTreeMap::new(),72        },73        BenchmarkResult {74            environment: BTreeMap::new(),75            command: String::from("sleep 2"),76            name: None,77            display_name: String::from("sleep 2"),78            measurements: Measurements::new(vec![79                Measurement {80                    time_wall_clock: Time::new::<second>(2.0),81                    time_cpu: Time::new::<second>(2.0),82                    time_user: Time::new::<second>(2.0),83                    time_system: Time::zero(),84                    memory_peak_resident: Some(Information::new::<byte>(1024.)),85                    hardware_counters: Default::default(),86                    exit_status: ExitStatus::default(),87                },88                Measurement {89                    time_wall_clock: Time::new::<second>(3.0),90                    time_cpu: Time::new::<second>(3.0),91                    time_user: Time::new::<second>(3.0),92                    time_system: Time::zero(),93                    memory_peak_resident: Some(Information::new::<byte>(1024.)),94                    hardware_counters: Default::default(),95                    exit_status: ExitStatus::default(),96                },97                Measurement {98                    time_wall_clock: Time::new::<second>(4.0),99                    time_cpu: Time::new::<second>(4.0),100                    time_user: Time::new::<second>(4.0),101                    time_system: Time::zero(),102                    memory_peak_resident: Some(Information::new::<byte>(1024.)),103                    hardware_counters: Default::default(),104                    exit_status: ExitStatus::default(),105                },106            ]),107            parameters: BTreeMap::new(),108        },109    ];110111    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None), @"112    | Command | Mean Wall Time [ms] | Change | Factor |113    |:---|---:|---:|:---|114    | `sleep 0.1` | 110.0 ± 26.5 |  |  |115    | `sleep 2` | 3000.0 ± 1000.0 | +2627.3% | (27.3x slower) |116    ");117118    insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None), @r#"119    [cols="<,>,>,<"]120    |===121    | Command122    | Mean Wall Time [ms]123    | Change124    | Factor125126    | `sleep 0.1`127    | 110.0 ± 26.5128    |129    |130131    | `sleep 2`132    | 3000.0 ± 1000.0133    | +2627.3%134    | (27.3x slower)135    |===136    "#);137138    insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None), @"139    | Command  |  Mean Wall Time [ms] |  Change |  Factor |140    |--+--+--+--|141    | =sleep 0.1=  |  110.0 ± 26.5 |   |   |142    | =sleep 2=  |  3000.0 ± 1000.0 |  +2627.3% |  (27.3x slower) |143    ");144}145146/// This (again) demonstrates that the first entry's units (s) are used to set147/// the units for all entries when the time unit is not given.148#[test]149fn test_markup_export_auto_s() {150    let results = [151        BenchmarkResult {152            environment: BTreeMap::new(),153            command: String::from("sleep 2"),154            name: None,155            display_name: String::from("sleep 2"),156            measurements: Measurements::new(vec![157                Measurement {158                    time_wall_clock: Time::new::<second>(2.1),159                    time_cpu: Time::new::<second>(2.1),160                    time_user: Time::new::<second>(2.1),161                    time_system: Time::zero(),162                    memory_peak_resident: Some(Information::new::<byte>(1024.)),163                    hardware_counters: Default::default(),164                    exit_status: ExitStatus::default(),165                },166                Measurement {167                    time_wall_clock: Time::new::<second>(2.2),168                    time_cpu: Time::new::<second>(2.2),169                    time_user: Time::new::<second>(2.2),170                    time_system: Time::zero(),171                    memory_peak_resident: Some(Information::new::<byte>(1024.)),172                    hardware_counters: Default::default(),173                    exit_status: ExitStatus::default(),174                },175                Measurement {176                    time_wall_clock: Time::new::<second>(2.3),177                    time_cpu: Time::new::<second>(2.3),178                    time_user: Time::new::<second>(2.3),179                    time_system: Time::zero(),180                    memory_peak_resident: Some(Information::new::<byte>(1024.)),181                    hardware_counters: Default::default(),182                    exit_status: ExitStatus::default(),183                },184            ]),185            parameters: BTreeMap::new(),186        },187        BenchmarkResult {188            environment: BTreeMap::new(),189            command: String::from("sleep 0.1"),190            name: None,191            display_name: String::from("sleep 0.1"),192            measurements: Measurements::new(vec![193                Measurement {194                    time_wall_clock: Time::new::<second>(0.1),195                    time_cpu: Time::new::<second>(0.1),196                    time_user: Time::new::<second>(0.1),197                    time_system: Time::zero(),198                    memory_peak_resident: Some(Information::new::<byte>(1024.)),199                    hardware_counters: Default::default(),200                    exit_status: ExitStatus::default(),201                },202                Measurement {203                    time_wall_clock: Time::new::<second>(0.2),204                    time_cpu: Time::new::<second>(0.2),205                    time_user: Time::new::<second>(0.2),206                    time_system: Time::zero(),207                    memory_peak_resident: Some(Information::new::<byte>(1024.)),208                    hardware_counters: Default::default(),209                    exit_status: ExitStatus::default(),210                },211                Measurement {212                    time_wall_clock: Time::new::<second>(0.3),213                    time_cpu: Time::new::<second>(0.3),214                    time_user: Time::new::<second>(0.3),215                    time_system: Time::zero(),216                    memory_peak_resident: Some(Information::new::<byte>(1024.)),217                    hardware_counters: Default::default(),218                    exit_status: ExitStatus::default(),219                },220            ]),221            parameters: BTreeMap::new(),222        },223    ];224225    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None), @"226    | Command | Mean Wall Time [s] | Change | Factor |227    |:---|---:|---:|:---|228    | `sleep 2` | 2.200 ± 0.100 |  |  |229    | `sleep 0.1` | 0.200 ± 0.100 | -90.9% | (11.0x faster) |230    ");231232    insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None), @r#"233    [cols="<,>,>,<"]234    |===235    | Command236    | Mean Wall Time [s]237    | Change238    | Factor239240    | `sleep 2`241    | 2.200 ± 0.100242    |243    |244245    | `sleep 0.1`246    | 0.200 ± 0.100247    | -90.9%248    | (11.0x faster)249    |===250    "#);251252    insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None), @"253    | Command  |  Mean Wall Time [s] |  Change |  Factor |254    |--+--+--+--|255    | =sleep 2=  |  2.200 ± 0.100 |   |   |256    | =sleep 0.1=  |  0.200 ± 0.100 |  -90.9% |  (11.0x faster) |257    ");258}259260/// This (again) demonstrates that the given time unit (ms) is used to set261/// the units for all entries.262#[test]263fn test_markup_export_manual_ms() {264    let timing_results = [265        BenchmarkResult {266            environment: BTreeMap::new(),267            command: String::from("sleep 2"),268            name: None,269            display_name: String::from("sleep 2"),270            measurements: Measurements::new(vec![271                Measurement {272                    time_wall_clock: Time::new::<second>(2.1),273                    time_cpu: Time::new::<second>(2.1),274                    time_user: Time::new::<second>(2.1),275                    time_system: Time::zero(),276                    memory_peak_resident: Some(Information::new::<byte>(1024.)),277                    hardware_counters: Default::default(),278                    exit_status: ExitStatus::default(),279                },280                Measurement {281                    time_wall_clock: Time::new::<second>(2.2),282                    time_cpu: Time::new::<second>(2.2),283                    time_user: Time::new::<second>(2.2),284                    time_system: Time::zero(),285                    memory_peak_resident: Some(Information::new::<byte>(1024.)),286                    hardware_counters: Default::default(),287                    exit_status: ExitStatus::default(),288                },289                Measurement {290                    time_wall_clock: Time::new::<second>(2.3),291                    time_cpu: Time::new::<second>(2.3),292                    time_user: Time::new::<second>(2.3),293                    time_system: Time::zero(),294                    memory_peak_resident: Some(Information::new::<byte>(1024.)),295                    hardware_counters: Default::default(),296                    exit_status: ExitStatus::default(),297                },298            ]),299            parameters: BTreeMap::new(),300        },301        BenchmarkResult {302            environment: BTreeMap::new(),303            command: String::from("sleep 0.1"),304            name: None,305            display_name: String::from("sleep 0.1"),306            measurements: Measurements::new(vec![307                Measurement {308                    time_wall_clock: Time::new::<second>(0.1),309                    time_cpu: Time::new::<second>(0.1),310                    time_user: Time::new::<second>(0.1),311                    time_system: Time::zero(),312                    memory_peak_resident: Some(Information::new::<byte>(1024.)),313                    hardware_counters: Default::default(),314                    exit_status: ExitStatus::default(),315                },316                Measurement {317                    time_wall_clock: Time::new::<second>(0.2),318                    time_cpu: Time::new::<second>(0.2),319                    time_user: Time::new::<second>(0.2),320                    time_system: Time::zero(),321                    memory_peak_resident: Some(Information::new::<byte>(1024.)),322                    hardware_counters: Default::default(),323                    exit_status: ExitStatus::default(),324                },325                Measurement {326                    time_wall_clock: Time::new::<second>(0.3),327                    time_cpu: Time::new::<second>(0.3),328                    time_user: Time::new::<second>(0.3),329                    time_system: Time::zero(),330                    memory_peak_resident: Some(Information::new::<byte>(1024.)),331                    hardware_counters: Default::default(),332                    exit_status: ExitStatus::default(),333                },334            ]),335            parameters: BTreeMap::new(),336        },337    ];338339    insta::assert_snapshot!(get_output::<MarkdownExporter>(&timing_results, Some(Unit { symbol: "ms", scale: 0.001 })), @"340    | Command | Mean Wall Time [ms] | Change | Factor |341    |:---|---:|---:|:---|342    | `sleep 2` | 2200.0 ± 100.0 |  |  |343    | `sleep 0.1` | 200.0 ± 100.0 | -90.9% | (11.0x faster) |344    ");345346    insta::assert_snapshot!(get_output::<AsciidocExporter>(&timing_results, Some(Unit { symbol: "ms", scale: 0.001 })), @r#"347    [cols="<,>,>,<"]348    |===349    | Command350    | Mean Wall Time [ms]351    | Change352    | Factor353354    | `sleep 2`355    | 2200.0 ± 100.0356    |357    |358359    | `sleep 0.1`360    | 200.0 ± 100.0361    | -90.9%362    | (11.0x faster)363    |===364    "#);365366    insta::assert_snapshot!(get_output::<OrgmodeExporter>(&timing_results, Some(Unit { symbol: "ms", scale: 0.001 })), @"367    | Command  |  Mean Wall Time [ms] |  Change |  Factor |368    |--+--+--+--|369    | =sleep 2=  |  2200.0 ± 100.0 |   |   |370    | =sleep 0.1=  |  200.0 ± 100.0 |  -90.9% |  (11.0x faster) |371    ");372}373374/// The given time unit (s) is used to set the units for all entries.375#[test]376fn test_markup_export_manual_s() {377    let results = [378        BenchmarkResult {379            environment: BTreeMap::new(),380            command: String::from("sleep 2"),381            name: None,382            display_name: String::from("sleep 2"),383            measurements: Measurements::new(vec![384                Measurement {385                    time_wall_clock: Time::new::<second>(2.01),386                    time_cpu: Time::new::<second>(2.01),387                    time_user: Time::new::<second>(2.01),388                    time_system: Time::zero(),389                    memory_peak_resident: Some(Information::new::<byte>(1024.)),390                    hardware_counters: Default::default(),391                    exit_status: ExitStatus::default(),392                },393                Measurement {394                    time_wall_clock: Time::new::<second>(2.02),395                    time_cpu: Time::new::<second>(2.02),396                    time_user: Time::new::<second>(2.02),397                    time_system: Time::zero(),398                    memory_peak_resident: Some(Information::new::<byte>(1024.)),399                    hardware_counters: Default::default(),400                    exit_status: ExitStatus::default(),401                },402                Measurement {403                    time_wall_clock: Time::new::<second>(2.03),404                    time_cpu: Time::new::<second>(2.03),405                    time_user: Time::new::<second>(2.03),406                    time_system: Time::zero(),407                    memory_peak_resident: Some(Information::new::<byte>(1024.)),408                    hardware_counters: Default::default(),409                    exit_status: ExitStatus::default(),410                },411            ]),412            parameters: BTreeMap::new(),413        },414        BenchmarkResult {415            environment: BTreeMap::new(),416            command: String::from("sleep 0.1"),417            name: None,418            display_name: String::from("sleep 0.1"),419            measurements: Measurements::new(vec![420                Measurement {421                    time_wall_clock: Time::new::<second>(0.11),422                    time_cpu: Time::new::<second>(0.11),423                    time_user: Time::new::<second>(0.11),424                    time_system: Time::zero(),425                    memory_peak_resident: Some(Information::new::<byte>(1024.)),426                    hardware_counters: Default::default(),427                    exit_status: ExitStatus::default(),428                },429                Measurement {430                    time_wall_clock: Time::new::<second>(0.12),431                    time_cpu: Time::new::<second>(0.12),432                    time_user: Time::new::<second>(0.12),433                    time_system: Time::zero(),434                    memory_peak_resident: Some(Information::new::<byte>(1024.)),435                    hardware_counters: Default::default(),436                    exit_status: ExitStatus::default(),437                },438                Measurement {439                    time_wall_clock: Time::new::<second>(0.13),440                    time_cpu: Time::new::<second>(0.13),441                    time_user: Time::new::<second>(0.13),442                    time_system: Time::zero(),443                    memory_peak_resident: Some(Information::new::<byte>(1024.)),444                    hardware_counters: Default::default(),445                    exit_status: ExitStatus::default(),446                },447            ]),448            parameters: BTreeMap::new(),449        },450    ];451452    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, Some(Unit { symbol: "s", scale: 1.0 })), @"453    | Command | Mean Wall Time [s] | Change | Factor |454    |:---|---:|---:|:---|455    | `sleep 2` | 2.020 ± 0.010 |  |  |456    | `sleep 0.1` | 0.120 ± 0.010 | -94.1% | (16.8x faster) |457    ");458459    insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, Some(Unit { symbol: "s", scale: 1.0 })), @r#"460    [cols="<,>,>,<"]461    |===462    | Command463    | Mean Wall Time [s]464    | Change465    | Factor466467    | `sleep 2`468    | 2.020 ± 0.010469    |470    |471472    | `sleep 0.1`473    | 0.120 ± 0.010474    | -94.1%475    | (16.8x faster)476    |===477    "#);478}