Skip to content
Myr

tests.rs

This file

Path

src/export/tests.rs

319 lines · 9.9 KiB · Rust

Last changed by

Move more tests to instaDavid Peterbefore adoptionda399ab

History of this file

Take it

1use super::Exporter;2use crate::benchmark::benchmark_result::BenchmarkResult;3use crate::export::asciidoc::AsciidocExporter;4use crate::export::orgmode::OrgmodeExporter;5use crate::util::units::Unit;6use crate::{export::markdown::MarkdownExporter, options::SortOrder};7use std::collections::BTreeMap;89fn get_output<E: Exporter + Default>(10    results: &[BenchmarkResult],11    unit: Option<Unit>,12    sort_order: SortOrder,13) -> String {14    let exporter = E::default();15    String::from_utf8(exporter.serialize(results, unit, sort_order).unwrap()).unwrap()16}1718/// Ensure the makrup output includes the table header and the multiple19/// benchmark results as a table. The list of actual times is not included20/// in the output.21///22/// This also demonstrates that the first entry's units (ms) are used to set23/// the units for all entries when the time unit is not specified.24#[test]25fn test_markup_export_auto_ms() {26    let results = [27        BenchmarkResult {28            command: String::from("sleep 0.1"),29            command_with_unused_parameters: String::from("sleep 0.1"),30            mean: 0.1057,31            stddev: Some(0.0016),32            median: 0.1057,33            user: 0.0009,34            system: 0.0011,35            min: 0.1023,36            max: 0.1080,37            times: Some(vec![0.1, 0.1, 0.1]),38            memory_usage_byte: None,39            exit_codes: vec![Some(0), Some(0), Some(0)],40            parameters: BTreeMap::new(),41        },42        BenchmarkResult {43            command: String::from("sleep 2"),44            command_with_unused_parameters: String::from("sleep 2"),45            mean: 2.0050,46            stddev: Some(0.0020),47            median: 2.0050,48            user: 0.0009,49            system: 0.0012,50            min: 2.0020,51            max: 2.0080,52            times: Some(vec![2.0, 2.0, 2.0]),53            memory_usage_byte: None,54            exit_codes: vec![Some(0), Some(0), Some(0)],55            parameters: BTreeMap::new(),56        },57    ];5859    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None, SortOrder::Command), @r#"60    | Command | Mean [ms] | Min [ms] | Max [ms] | Relative |61    |:---|---:|---:|---:|---:|62    | `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |63    | `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |64    "#);6566    insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None, SortOrder::Command), @r#"67    [cols="<,>,>,>,>"]68    |===69    | Command 70    | Mean [ms] 71    | Min [ms] 72    | Max [ms] 73    | Relative 7475    | `sleep 0.1` 76    | 105.7 ± 1.6 77    | 102.3 78    | 108.0 79    | 1.00 8081    | `sleep 2` 82    | 2005.0 ± 2.0 83    | 2002.0 84    | 2008.0 85    | 18.97 ± 0.29 86    |===87    "#);8889    insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None, SortOrder::Command), @r#"90    | Command  |  Mean [ms] |  Min [ms] |  Max [ms] |  Relative |91    |--+--+--+--+--|92    | =sleep 0.1=  |  105.7 ± 1.6 |  102.3 |  108.0 |  1.00 |93    | =sleep 2=  |  2005.0 ± 2.0 |  2002.0 |  2008.0 |  18.97 ± 0.29 |94    "#);95}9697/// This (again) demonstrates that the first entry's units (s) are used to set98/// the units for all entries when the time unit is not given.99#[test]100fn test_markup_export_auto_s() {101    let results = [102        BenchmarkResult {103            command: String::from("sleep 2"),104            command_with_unused_parameters: String::from("sleep 2"),105            mean: 2.0050,106            stddev: Some(0.0020),107            median: 2.0050,108            user: 0.0009,109            system: 0.0012,110            min: 2.0020,111            max: 2.0080,112            times: Some(vec![2.0, 2.0, 2.0]),113            memory_usage_byte: None,114            exit_codes: vec![Some(0), Some(0), Some(0)],115            parameters: BTreeMap::new(),116        },117        BenchmarkResult {118            command: String::from("sleep 0.1"),119            command_with_unused_parameters: String::from("sleep 0.1"),120            mean: 0.1057,121            stddev: Some(0.0016),122            median: 0.1057,123            user: 0.0009,124            system: 0.0011,125            min: 0.1023,126            max: 0.1080,127            times: Some(vec![0.1, 0.1, 0.1]),128            memory_usage_byte: None,129            exit_codes: vec![Some(0), Some(0), Some(0)],130            parameters: BTreeMap::new(),131        },132    ];133134    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None, SortOrder::Command), @r#"135    | Command | Mean [s] | Min [s] | Max [s] | Relative |136    |:---|---:|---:|---:|---:|137    | `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |138    | `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |139    "#);140141    insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None, SortOrder::Command), @r#"142    [cols="<,>,>,>,>"]143    |===144    | Command 145    | Mean [s] 146    | Min [s] 147    | Max [s] 148    | Relative 149150    | `sleep 2` 151    | 2.005 ± 0.002 152    | 2.002 153    | 2.008 154    | 18.97 ± 0.29 155156    | `sleep 0.1` 157    | 0.106 ± 0.002 158    | 0.102 159    | 0.108 160    | 1.00 161    |===162    "#);163164    insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None, SortOrder::Command), @r#"165    | Command  |  Mean [s] |  Min [s] |  Max [s] |  Relative |166    |--+--+--+--+--|167    | =sleep 2=  |  2.005 ± 0.002 |  2.002 |  2.008 |  18.97 ± 0.29 |168    | =sleep 0.1=  |  0.106 ± 0.002 |  0.102 |  0.108 |  1.00 |169    "#);170}171172/// This (again) demonstrates that the given time unit (ms) is used to set173/// the units for all entries.174#[test]175fn test_markup_export_manual_ms() {176    let timing_results = [177        BenchmarkResult {178            command: String::from("sleep 2"),179            command_with_unused_parameters: String::from("sleep 2"),180            mean: 2.0050,181            stddev: Some(0.0020),182            median: 2.0050,183            user: 0.0009,184            system: 0.0012,185            min: 2.0020,186            max: 2.0080,187            times: Some(vec![2.0, 2.0, 2.0]),188            memory_usage_byte: None,189            exit_codes: vec![Some(0), Some(0), Some(0)],190            parameters: BTreeMap::new(),191        },192        BenchmarkResult {193            command: String::from("sleep 0.1"),194            command_with_unused_parameters: String::from("sleep 0.1"),195            mean: 0.1057,196            stddev: Some(0.0016),197            median: 0.1057,198            user: 0.0009,199            system: 0.0011,200            min: 0.1023,201            max: 0.1080,202            times: Some(vec![0.1, 0.1, 0.1]),203            memory_usage_byte: None,204            exit_codes: vec![Some(0), Some(0), Some(0)],205            parameters: BTreeMap::new(),206        },207    ];208209    insta::assert_snapshot!(get_output::<MarkdownExporter>(&timing_results, Some(Unit::MilliSecond), SortOrder::Command), @r#"210    | Command | Mean [ms] | Min [ms] | Max [ms] | Relative |211    |:---|---:|---:|---:|---:|212    | `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |213    | `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |214    "#);215216    insta::assert_snapshot!(get_output::<AsciidocExporter>(&timing_results, Some(Unit::MilliSecond), SortOrder::Command), @r#"217    [cols="<,>,>,>,>"]218    |===219    | Command 220    | Mean [ms] 221    | Min [ms] 222    | Max [ms] 223    | Relative 224225    | `sleep 2` 226    | 2005.0 ± 2.0 227    | 2002.0 228    | 2008.0 229    | 18.97 ± 0.29 230231    | `sleep 0.1` 232    | 105.7 ± 1.6 233    | 102.3 234    | 108.0 235    | 1.00 236    |===237    "#);238239    insta::assert_snapshot!(get_output::<OrgmodeExporter>(&timing_results, Some(Unit::MilliSecond), SortOrder::Command), @r#"240    | Command  |  Mean [ms] |  Min [ms] |  Max [ms] |  Relative |241    |--+--+--+--+--|242    | =sleep 2=  |  2005.0 ± 2.0 |  2002.0 |  2008.0 |  18.97 ± 0.29 |243    | =sleep 0.1=  |  105.7 ± 1.6 |  102.3 |  108.0 |  1.00 |244    "#);245}246247/// The given time unit (s) is used to set the units for all entries.248#[test]249fn test_markup_export_manual_s() {250    let results = [251        BenchmarkResult {252            command: String::from("sleep 2"),253            command_with_unused_parameters: String::from("sleep 2"),254            mean: 2.0050,255            stddev: Some(0.0020),256            median: 2.0050,257            user: 0.0009,258            system: 0.0012,259            min: 2.0020,260            max: 2.0080,261            times: Some(vec![2.0, 2.0, 2.0]),262            memory_usage_byte: None,263            exit_codes: vec![Some(0), Some(0), Some(0)],264            parameters: BTreeMap::new(),265        },266        BenchmarkResult {267            command: String::from("sleep 0.1"),268            command_with_unused_parameters: String::from("sleep 0.1"),269            mean: 0.1057,270            stddev: Some(0.0016),271            median: 0.1057,272            user: 0.0009,273            system: 0.0011,274            min: 0.1023,275            max: 0.1080,276            times: Some(vec![0.1, 0.1, 0.1]),277            memory_usage_byte: None,278            exit_codes: vec![Some(0), Some(0), Some(0)],279            parameters: BTreeMap::new(),280        },281    ];282283    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, Some(Unit::Second), SortOrder::Command), @r#"284        | Command | Mean [s] | Min [s] | Max [s] | Relative |285        |:---|---:|---:|---:|---:|286        | `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |287        | `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |288        "#);289290    insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, Some(Unit::Second), SortOrder::MeanTime), @r#"291        | Command | Mean [s] | Min [s] | Max [s] | Relative |292        |:---|---:|---:|---:|---:|293        | `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |294        | `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |295        "#);296297    insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, Some(Unit::Second), SortOrder::Command), @r#"298    [cols="<,>,>,>,>"]299    |===300    | Command 301    | Mean [s] 302    | Min [s] 303    | Max [s] 304    | Relative 305306    | `sleep 2` 307    | 2.005 ± 0.002 308    | 2.002 309    | 2.008 310    | 18.97 ± 0.29 311312    | `sleep 0.1` 313    | 0.106 ± 0.002 314    | 0.102 315    | 0.108 316    | 1.00 317    |===318    "#);319}