Skip to content
Myr

tests.rs

This file

Path

src/export/tests.rs

470 lines · 18.2 KiB · Rust

Last changed by

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

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            command: String::from("sleep 0.1"),39            name: None,40            display_name: String::from("sleep 0.1"),41            measurements: Measurements::new(vec![42                Measurement {43                    time_wall_clock: Time::new::<second>(0.09),44                    time_cpu: Time::new::<second>(0.09),45                    time_user: Time::new::<second>(0.09),46                    time_system: Time::zero(),47                    memory_peak_resident: Some(Information::new::<byte>(1024.)),48                    hardware_counters: Default::default(),49                    exit_status: ExitStatus::default(),50                },51                Measurement {52                    time_wall_clock: Time::new::<second>(0.10),53                    time_cpu: Time::new::<second>(0.10),54                    time_user: Time::new::<second>(0.10),55                    time_system: Time::zero(),56                    memory_peak_resident: Some(Information::new::<byte>(1024.)),57                    hardware_counters: Default::default(),58                    exit_status: ExitStatus::default(),59                },60                Measurement {61                    time_wall_clock: Time::new::<second>(0.14),62                    time_cpu: Time::new::<second>(0.14),63                    time_user: Time::new::<second>(0.14),64                    time_system: Time::zero(),65                    memory_peak_resident: Some(Information::new::<byte>(1024.)),66                    hardware_counters: Default::default(),67                    exit_status: ExitStatus::default(),68                },69            ]),70            parameters: BTreeMap::new(),71        },72        BenchmarkResult {73            command: String::from("sleep 2"),74            name: None,75            display_name: String::from("sleep 2"),76            measurements: Measurements::new(vec![77                Measurement {78                    time_wall_clock: Time::new::<second>(2.0),79                    time_cpu: Time::new::<second>(2.0),80                    time_user: Time::new::<second>(2.0),81                    time_system: Time::zero(),82                    memory_peak_resident: Some(Information::new::<byte>(1024.)),83                    hardware_counters: Default::default(),84                    exit_status: ExitStatus::default(),85                },86                Measurement {87                    time_wall_clock: Time::new::<second>(3.0),88                    time_cpu: Time::new::<second>(3.0),89                    time_user: Time::new::<second>(3.0),90                    time_system: Time::zero(),91                    memory_peak_resident: Some(Information::new::<byte>(1024.)),92                    hardware_counters: Default::default(),93                    exit_status: ExitStatus::default(),94                },95                Measurement {96                    time_wall_clock: Time::new::<second>(4.0),97                    time_cpu: Time::new::<second>(4.0),98                    time_user: Time::new::<second>(4.0),99                    time_system: Time::zero(),100                    memory_peak_resident: Some(Information::new::<byte>(1024.)),101                    hardware_counters: Default::default(),102                    exit_status: ExitStatus::default(),103                },104            ]),105            parameters: BTreeMap::new(),106        },107    ];108109    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None), @"110    | Command | Mean Wall Time [ms] | Change | Factor |111    |:---|---:|---:|:---|112    | `sleep 0.1` | 110.0 ± 26.5 |  |  |113    | `sleep 2` | 3000.0 ± 1000.0 | +2627.3% | (27.3x slower) |114    ");115116    insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None), @r#"117    [cols="<,>,>,<"]118    |===119    | Command120    | Mean Wall Time [ms]121    | Change122    | Factor123124    | `sleep 0.1`125    | 110.0 ± 26.5126    |127    |128129    | `sleep 2`130    | 3000.0 ± 1000.0131    | +2627.3%132    | (27.3x slower)133    |===134    "#);135136    insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None), @"137    | Command  |  Mean Wall Time [ms] |  Change |  Factor |138    |--+--+--+--|139    | =sleep 0.1=  |  110.0 ± 26.5 |   |   |140    | =sleep 2=  |  3000.0 ± 1000.0 |  +2627.3% |  (27.3x slower) |141    ");142}143144/// This (again) demonstrates that the first entry's units (s) are used to set145/// the units for all entries when the time unit is not given.146#[test]147fn test_markup_export_auto_s() {148    let results = [149        BenchmarkResult {150            command: String::from("sleep 2"),151            name: None,152            display_name: String::from("sleep 2"),153            measurements: Measurements::new(vec![154                Measurement {155                    time_wall_clock: Time::new::<second>(2.1),156                    time_cpu: Time::new::<second>(2.1),157                    time_user: Time::new::<second>(2.1),158                    time_system: Time::zero(),159                    memory_peak_resident: Some(Information::new::<byte>(1024.)),160                    hardware_counters: Default::default(),161                    exit_status: ExitStatus::default(),162                },163                Measurement {164                    time_wall_clock: Time::new::<second>(2.2),165                    time_cpu: Time::new::<second>(2.2),166                    time_user: Time::new::<second>(2.2),167                    time_system: Time::zero(),168                    memory_peak_resident: Some(Information::new::<byte>(1024.)),169                    hardware_counters: Default::default(),170                    exit_status: ExitStatus::default(),171                },172                Measurement {173                    time_wall_clock: Time::new::<second>(2.3),174                    time_cpu: Time::new::<second>(2.3),175                    time_user: Time::new::<second>(2.3),176                    time_system: Time::zero(),177                    memory_peak_resident: Some(Information::new::<byte>(1024.)),178                    hardware_counters: Default::default(),179                    exit_status: ExitStatus::default(),180                },181            ]),182            parameters: BTreeMap::new(),183        },184        BenchmarkResult {185            command: String::from("sleep 0.1"),186            name: None,187            display_name: String::from("sleep 0.1"),188            measurements: Measurements::new(vec![189                Measurement {190                    time_wall_clock: Time::new::<second>(0.1),191                    time_cpu: Time::new::<second>(0.1),192                    time_user: Time::new::<second>(0.1),193                    time_system: Time::zero(),194                    memory_peak_resident: Some(Information::new::<byte>(1024.)),195                    hardware_counters: Default::default(),196                    exit_status: ExitStatus::default(),197                },198                Measurement {199                    time_wall_clock: Time::new::<second>(0.2),200                    time_cpu: Time::new::<second>(0.2),201                    time_user: Time::new::<second>(0.2),202                    time_system: Time::zero(),203                    memory_peak_resident: Some(Information::new::<byte>(1024.)),204                    hardware_counters: Default::default(),205                    exit_status: ExitStatus::default(),206                },207                Measurement {208                    time_wall_clock: Time::new::<second>(0.3),209                    time_cpu: Time::new::<second>(0.3),210                    time_user: Time::new::<second>(0.3),211                    time_system: Time::zero(),212                    memory_peak_resident: Some(Information::new::<byte>(1024.)),213                    hardware_counters: Default::default(),214                    exit_status: ExitStatus::default(),215                },216            ]),217            parameters: BTreeMap::new(),218        },219    ];220221    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None), @"222    | Command | Mean Wall Time [s] | Change | Factor |223    |:---|---:|---:|:---|224    | `sleep 2` | 2.200 ± 0.100 |  |  |225    | `sleep 0.1` | 0.200 ± 0.100 | -90.9% | (11.0x faster) |226    ");227228    insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None), @r#"229    [cols="<,>,>,<"]230    |===231    | Command232    | Mean Wall Time [s]233    | Change234    | Factor235236    | `sleep 2`237    | 2.200 ± 0.100238    |239    |240241    | `sleep 0.1`242    | 0.200 ± 0.100243    | -90.9%244    | (11.0x faster)245    |===246    "#);247248    insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None), @"249    | Command  |  Mean Wall Time [s] |  Change |  Factor |250    |--+--+--+--|251    | =sleep 2=  |  2.200 ± 0.100 |   |   |252    | =sleep 0.1=  |  0.200 ± 0.100 |  -90.9% |  (11.0x faster) |253    ");254}255256/// This (again) demonstrates that the given time unit (ms) is used to set257/// the units for all entries.258#[test]259fn test_markup_export_manual_ms() {260    let timing_results = [261        BenchmarkResult {262            command: String::from("sleep 2"),263            name: None,264            display_name: String::from("sleep 2"),265            measurements: Measurements::new(vec![266                Measurement {267                    time_wall_clock: Time::new::<second>(2.1),268                    time_cpu: Time::new::<second>(2.1),269                    time_user: Time::new::<second>(2.1),270                    time_system: Time::zero(),271                    memory_peak_resident: Some(Information::new::<byte>(1024.)),272                    hardware_counters: Default::default(),273                    exit_status: ExitStatus::default(),274                },275                Measurement {276                    time_wall_clock: Time::new::<second>(2.2),277                    time_cpu: Time::new::<second>(2.2),278                    time_user: Time::new::<second>(2.2),279                    time_system: Time::zero(),280                    memory_peak_resident: Some(Information::new::<byte>(1024.)),281                    hardware_counters: Default::default(),282                    exit_status: ExitStatus::default(),283                },284                Measurement {285                    time_wall_clock: Time::new::<second>(2.3),286                    time_cpu: Time::new::<second>(2.3),287                    time_user: Time::new::<second>(2.3),288                    time_system: Time::zero(),289                    memory_peak_resident: Some(Information::new::<byte>(1024.)),290                    hardware_counters: Default::default(),291                    exit_status: ExitStatus::default(),292                },293            ]),294            parameters: BTreeMap::new(),295        },296        BenchmarkResult {297            command: String::from("sleep 0.1"),298            name: None,299            display_name: String::from("sleep 0.1"),300            measurements: Measurements::new(vec![301                Measurement {302                    time_wall_clock: Time::new::<second>(0.1),303                    time_cpu: Time::new::<second>(0.1),304                    time_user: Time::new::<second>(0.1),305                    time_system: Time::zero(),306                    memory_peak_resident: Some(Information::new::<byte>(1024.)),307                    hardware_counters: Default::default(),308                    exit_status: ExitStatus::default(),309                },310                Measurement {311                    time_wall_clock: Time::new::<second>(0.2),312                    time_cpu: Time::new::<second>(0.2),313                    time_user: Time::new::<second>(0.2),314                    time_system: Time::zero(),315                    memory_peak_resident: Some(Information::new::<byte>(1024.)),316                    hardware_counters: Default::default(),317                    exit_status: ExitStatus::default(),318                },319                Measurement {320                    time_wall_clock: Time::new::<second>(0.3),321                    time_cpu: Time::new::<second>(0.3),322                    time_user: Time::new::<second>(0.3),323                    time_system: Time::zero(),324                    memory_peak_resident: Some(Information::new::<byte>(1024.)),325                    hardware_counters: Default::default(),326                    exit_status: ExitStatus::default(),327                },328            ]),329            parameters: BTreeMap::new(),330        },331    ];332333    insta::assert_snapshot!(get_output::<MarkdownExporter>(&timing_results, Some(Unit { symbol: "ms", scale: 0.001 })), @"334    | Command | Mean Wall Time [ms] | Change | Factor |335    |:---|---:|---:|:---|336    | `sleep 2` | 2200.0 ± 100.0 |  |  |337    | `sleep 0.1` | 200.0 ± 100.0 | -90.9% | (11.0x faster) |338    ");339340    insta::assert_snapshot!(get_output::<AsciidocExporter>(&timing_results, Some(Unit { symbol: "ms", scale: 0.001 })), @r#"341    [cols="<,>,>,<"]342    |===343    | Command344    | Mean Wall Time [ms]345    | Change346    | Factor347348    | `sleep 2`349    | 2200.0 ± 100.0350    |351    |352353    | `sleep 0.1`354    | 200.0 ± 100.0355    | -90.9%356    | (11.0x faster)357    |===358    "#);359360    insta::assert_snapshot!(get_output::<OrgmodeExporter>(&timing_results, Some(Unit { symbol: "ms", scale: 0.001 })), @"361    | Command  |  Mean Wall Time [ms] |  Change |  Factor |362    |--+--+--+--|363    | =sleep 2=  |  2200.0 ± 100.0 |   |   |364    | =sleep 0.1=  |  200.0 ± 100.0 |  -90.9% |  (11.0x faster) |365    ");366}367368/// The given time unit (s) is used to set the units for all entries.369#[test]370fn test_markup_export_manual_s() {371    let results = [372        BenchmarkResult {373            command: String::from("sleep 2"),374            name: None,375            display_name: String::from("sleep 2"),376            measurements: Measurements::new(vec![377                Measurement {378                    time_wall_clock: Time::new::<second>(2.01),379                    time_cpu: Time::new::<second>(2.01),380                    time_user: Time::new::<second>(2.01),381                    time_system: Time::zero(),382                    memory_peak_resident: Some(Information::new::<byte>(1024.)),383                    hardware_counters: Default::default(),384                    exit_status: ExitStatus::default(),385                },386                Measurement {387                    time_wall_clock: Time::new::<second>(2.02),388                    time_cpu: Time::new::<second>(2.02),389                    time_user: Time::new::<second>(2.02),390                    time_system: Time::zero(),391                    memory_peak_resident: Some(Information::new::<byte>(1024.)),392                    hardware_counters: Default::default(),393                    exit_status: ExitStatus::default(),394                },395                Measurement {396                    time_wall_clock: Time::new::<second>(2.03),397                    time_cpu: Time::new::<second>(2.03),398                    time_user: Time::new::<second>(2.03),399                    time_system: Time::zero(),400                    memory_peak_resident: Some(Information::new::<byte>(1024.)),401                    hardware_counters: Default::default(),402                    exit_status: ExitStatus::default(),403                },404            ]),405            parameters: BTreeMap::new(),406        },407        BenchmarkResult {408            command: String::from("sleep 0.1"),409            name: None,410            display_name: String::from("sleep 0.1"),411            measurements: Measurements::new(vec![412                Measurement {413                    time_wall_clock: Time::new::<second>(0.11),414                    time_cpu: Time::new::<second>(0.11),415                    time_user: Time::new::<second>(0.11),416                    time_system: Time::zero(),417                    memory_peak_resident: Some(Information::new::<byte>(1024.)),418                    hardware_counters: Default::default(),419                    exit_status: ExitStatus::default(),420                },421                Measurement {422                    time_wall_clock: Time::new::<second>(0.12),423                    time_cpu: Time::new::<second>(0.12),424                    time_user: Time::new::<second>(0.12),425                    time_system: Time::zero(),426                    memory_peak_resident: Some(Information::new::<byte>(1024.)),427                    hardware_counters: Default::default(),428                    exit_status: ExitStatus::default(),429                },430                Measurement {431                    time_wall_clock: Time::new::<second>(0.13),432                    time_cpu: Time::new::<second>(0.13),433                    time_user: Time::new::<second>(0.13),434                    time_system: Time::zero(),435                    memory_peak_resident: Some(Information::new::<byte>(1024.)),436                    hardware_counters: Default::default(),437                    exit_status: ExitStatus::default(),438                },439            ]),440            parameters: BTreeMap::new(),441        },442    ];443444    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, Some(Unit { symbol: "s", scale: 1.0 })), @"445    | Command | Mean Wall Time [s] | Change | Factor |446    |:---|---:|---:|:---|447    | `sleep 2` | 2.020 ± 0.010 |  |  |448    | `sleep 0.1` | 0.120 ± 0.010 | -94.1% | (16.8x faster) |449    ");450451    insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, Some(Unit { symbol: "s", scale: 1.0 })), @r#"452    [cols="<,>,>,<"]453    |===454    | Command455    | Mean Wall Time [s]456    | Change457    | Factor458459    | `sleep 2`460    | 2.020 ± 0.010461    |462    |463464    | `sleep 0.1`465    | 0.120 ± 0.010466    | -94.1%467    | (16.8x faster)468    |===469    "#);470}