use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::benchmark::measurement::{Measurement, Measurements};
use crate::export::asciidoc::AsciidocExporter;
use crate::export::markdown::MarkdownExporter;
use crate::export::orgmode::OrgmodeExporter;
use crate::metric::{MetricSelection, Unit};
use crate::quantity::{byte, second, Information, Time, Zero};
use std::collections::BTreeMap;
use std::process::ExitStatus;
fn get_output<E: Exporter + Default>(results: &[BenchmarkResult], unit: Option<Unit>) -> String {
let exporter = E::default();
String::from_utf8(
exporter
.serialize(
results,
&[MetricSelection {
unit,
..MetricSelection::default()
}],
)
.unwrap(),
)
.unwrap()
}
#[test]
fn test_markup_export_auto_ms() {
let results = [
BenchmarkResult {
environment: BTreeMap::new(),
command: String::from("sleep 0.1"),
name: None,
display_name: String::from("sleep 0.1"),
measurements: Measurements::new(vec![
Measurement {
time_wall_clock: Time::new::<second>(0.09),
time_cpu: Time::new::<second>(0.09),
time_user: Time::new::<second>(0.09),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(0.10),
time_cpu: Time::new::<second>(0.10),
time_user: Time::new::<second>(0.10),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(0.14),
time_cpu: Time::new::<second>(0.14),
time_user: Time::new::<second>(0.14),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
]),
parameters: BTreeMap::new(),
},
BenchmarkResult {
environment: BTreeMap::new(),
command: String::from("sleep 2"),
name: None,
display_name: String::from("sleep 2"),
measurements: Measurements::new(vec![
Measurement {
time_wall_clock: Time::new::<second>(2.0),
time_cpu: Time::new::<second>(2.0),
time_user: Time::new::<second>(2.0),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(3.0),
time_cpu: Time::new::<second>(3.0),
time_user: Time::new::<second>(3.0),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(4.0),
time_cpu: Time::new::<second>(4.0),
time_user: Time::new::<second>(4.0),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
]),
parameters: BTreeMap::new(),
},
];
insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None), @"
| Command | Mean Wall Time [ms] | Change | Factor |
|:---|---:|---:|:---|
| `sleep 0.1` | 110.0 ± 26.5 | | |
| `sleep 2` | 3000.0 ± 1000.0 | +2627.3% | (27.3x slower) |
");
insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None), @r#"
[cols="<,>,>,<"]
|===
| Command
| Mean Wall Time [ms]
| Change
| Factor
| `sleep 0.1`
| 110.0 ± 26.5
|
|
| `sleep 2`
| 3000.0 ± 1000.0
| +2627.3%
| (27.3x slower)
|===
"#);
insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None), @"
| Command | Mean Wall Time [ms] | Change | Factor |
|--+--+--+--|
| =sleep 0.1= | 110.0 ± 26.5 | | |
| =sleep 2= | 3000.0 ± 1000.0 | +2627.3% | (27.3x slower) |
");
}
#[test]
fn test_markup_export_auto_s() {
let results = [
BenchmarkResult {
environment: BTreeMap::new(),
command: String::from("sleep 2"),
name: None,
display_name: String::from("sleep 2"),
measurements: Measurements::new(vec![
Measurement {
time_wall_clock: Time::new::<second>(2.1),
time_cpu: Time::new::<second>(2.1),
time_user: Time::new::<second>(2.1),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(2.2),
time_cpu: Time::new::<second>(2.2),
time_user: Time::new::<second>(2.2),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(2.3),
time_cpu: Time::new::<second>(2.3),
time_user: Time::new::<second>(2.3),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
]),
parameters: BTreeMap::new(),
},
BenchmarkResult {
environment: BTreeMap::new(),
command: String::from("sleep 0.1"),
name: None,
display_name: String::from("sleep 0.1"),
measurements: Measurements::new(vec![
Measurement {
time_wall_clock: Time::new::<second>(0.1),
time_cpu: Time::new::<second>(0.1),
time_user: Time::new::<second>(0.1),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(0.2),
time_cpu: Time::new::<second>(0.2),
time_user: Time::new::<second>(0.2),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(0.3),
time_cpu: Time::new::<second>(0.3),
time_user: Time::new::<second>(0.3),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
]),
parameters: BTreeMap::new(),
},
];
insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None), @"
| Command | Mean Wall Time [s] | Change | Factor |
|:---|---:|---:|:---|
| `sleep 2` | 2.200 ± 0.100 | | |
| `sleep 0.1` | 0.200 ± 0.100 | -90.9% | (11.0x faster) |
");
insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None), @r#"
[cols="<,>,>,<"]
|===
| Command
| Mean Wall Time [s]
| Change
| Factor
| `sleep 2`
| 2.200 ± 0.100
|
|
| `sleep 0.1`
| 0.200 ± 0.100
| -90.9%
| (11.0x faster)
|===
"#);
insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None), @"
| Command | Mean Wall Time [s] | Change | Factor |
|--+--+--+--|
| =sleep 2= | 2.200 ± 0.100 | | |
| =sleep 0.1= | 0.200 ± 0.100 | -90.9% | (11.0x faster) |
");
}
#[test]
fn test_markup_export_manual_ms() {
let timing_results = [
BenchmarkResult {
environment: BTreeMap::new(),
command: String::from("sleep 2"),
name: None,
display_name: String::from("sleep 2"),
measurements: Measurements::new(vec![
Measurement {
time_wall_clock: Time::new::<second>(2.1),
time_cpu: Time::new::<second>(2.1),
time_user: Time::new::<second>(2.1),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(2.2),
time_cpu: Time::new::<second>(2.2),
time_user: Time::new::<second>(2.2),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(2.3),
time_cpu: Time::new::<second>(2.3),
time_user: Time::new::<second>(2.3),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
]),
parameters: BTreeMap::new(),
},
BenchmarkResult {
environment: BTreeMap::new(),
command: String::from("sleep 0.1"),
name: None,
display_name: String::from("sleep 0.1"),
measurements: Measurements::new(vec![
Measurement {
time_wall_clock: Time::new::<second>(0.1),
time_cpu: Time::new::<second>(0.1),
time_user: Time::new::<second>(0.1),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(0.2),
time_cpu: Time::new::<second>(0.2),
time_user: Time::new::<second>(0.2),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(0.3),
time_cpu: Time::new::<second>(0.3),
time_user: Time::new::<second>(0.3),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
]),
parameters: BTreeMap::new(),
},
];
insta::assert_snapshot!(get_output::<MarkdownExporter>(&timing_results, Some(Unit { symbol: "ms", scale: 0.001 })), @"
| Command | Mean Wall Time [ms] | Change | Factor |
|:---|---:|---:|:---|
| `sleep 2` | 2200.0 ± 100.0 | | |
| `sleep 0.1` | 200.0 ± 100.0 | -90.9% | (11.0x faster) |
");
insta::assert_snapshot!(get_output::<AsciidocExporter>(&timing_results, Some(Unit { symbol: "ms", scale: 0.001 })), @r#"
[cols="<,>,>,<"]
|===
| Command
| Mean Wall Time [ms]
| Change
| Factor
| `sleep 2`
| 2200.0 ± 100.0
|
|
| `sleep 0.1`
| 200.0 ± 100.0
| -90.9%
| (11.0x faster)
|===
"#);
insta::assert_snapshot!(get_output::<OrgmodeExporter>(&timing_results, Some(Unit { symbol: "ms", scale: 0.001 })), @"
| Command | Mean Wall Time [ms] | Change | Factor |
|--+--+--+--|
| =sleep 2= | 2200.0 ± 100.0 | | |
| =sleep 0.1= | 200.0 ± 100.0 | -90.9% | (11.0x faster) |
");
}
#[test]
fn test_markup_export_manual_s() {
let results = [
BenchmarkResult {
environment: BTreeMap::new(),
command: String::from("sleep 2"),
name: None,
display_name: String::from("sleep 2"),
measurements: Measurements::new(vec![
Measurement {
time_wall_clock: Time::new::<second>(2.01),
time_cpu: Time::new::<second>(2.01),
time_user: Time::new::<second>(2.01),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(2.02),
time_cpu: Time::new::<second>(2.02),
time_user: Time::new::<second>(2.02),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(2.03),
time_cpu: Time::new::<second>(2.03),
time_user: Time::new::<second>(2.03),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
]),
parameters: BTreeMap::new(),
},
BenchmarkResult {
environment: BTreeMap::new(),
command: String::from("sleep 0.1"),
name: None,
display_name: String::from("sleep 0.1"),
measurements: Measurements::new(vec![
Measurement {
time_wall_clock: Time::new::<second>(0.11),
time_cpu: Time::new::<second>(0.11),
time_user: Time::new::<second>(0.11),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(0.12),
time_cpu: Time::new::<second>(0.12),
time_user: Time::new::<second>(0.12),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(0.13),
time_cpu: Time::new::<second>(0.13),
time_user: Time::new::<second>(0.13),
time_system: Time::zero(),
memory_peak_resident: Some(Information::new::<byte>(1024.)),
hardware_counters: Default::default(),
exit_status: ExitStatus::default(),
},
]),
parameters: BTreeMap::new(),
},
];
insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, Some(Unit { symbol: "s", scale: 1.0 })), @"
| Command | Mean Wall Time [s] | Change | Factor |
|:---|---:|---:|:---|
| `sleep 2` | 2.020 ± 0.010 | | |
| `sleep 0.1` | 0.120 ± 0.010 | -94.1% | (16.8x faster) |
");
insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, Some(Unit { symbol: "s", scale: 1.0 })), @r#"
[cols="<,>,>,<"]
|===
| Command
| Mean Wall Time [s]
| Change
| Factor
| `sleep 2`
| 2.020 ± 0.010
|
|
| `sleep 0.1`
| 0.120 ± 0.010
| -94.1%
| (16.8x faster)
|===
"#);
}