2021-08-24 21:33:48 +02:00
|
|
|
use super::Exporter;
|
2022-02-20 13:44:34 +01:00
|
|
|
use crate::benchmark::benchmark_result::BenchmarkResult;
|
2022-02-06 19:54:59 +01:00
|
|
|
use crate::benchmark::relative_speed::{self, BenchmarkResultWithRelativeSpeed};
|
2022-02-06 19:40:54 +01:00
|
|
|
use crate::output::format::format_duration_value;
|
2022-02-06 20:01:41 +01:00
|
|
|
use crate::util::units::Unit;
|
2018-03-20 17:00:24 -04:00
|
|
|
|
2022-02-06 17:44:28 +01:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
|
|
2018-03-20 17:00:24 -04:00
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct MarkdownExporter {}
|
|
|
|
|
|
|
|
|
|
impl Exporter for MarkdownExporter {
|
2019-07-13 10:29:05 +02:00
|
|
|
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {
|
2018-11-01 20:52:25 -07:00
|
|
|
let unit = if let Some(unit) = unit {
|
2018-10-29 18:44:44 -07:00
|
|
|
// Use the given unit for all entries.
|
2018-11-01 20:52:25 -07:00
|
|
|
unit
|
2018-10-29 18:44:44 -07:00
|
|
|
} else if let Some(first_result) = results.first() {
|
2018-09-09 08:44:13 -07:00
|
|
|
// Use the first BenchmarkResult entry to determine the unit for all entries.
|
2018-10-27 16:51:32 -07:00
|
|
|
format_duration_value(first_result.mean, None).1
|
|
|
|
|
} else {
|
|
|
|
|
// Default to `Second`.
|
|
|
|
|
Unit::Second
|
|
|
|
|
};
|
2018-09-09 08:44:13 -07:00
|
|
|
|
2021-08-24 22:12:42 +02:00
|
|
|
if let Some(annotated_results) = relative_speed::compute(results) {
|
2020-10-16 10:47:26 +02:00
|
|
|
let mut destination = start_table(unit);
|
2019-06-08 12:04:26 +02:00
|
|
|
|
2020-10-16 10:47:26 +02:00
|
|
|
for result in annotated_results {
|
|
|
|
|
add_table_row(&mut destination, &result, unit);
|
|
|
|
|
}
|
2018-03-20 17:00:24 -04:00
|
|
|
|
2020-10-16 10:47:26 +02:00
|
|
|
Ok(destination)
|
|
|
|
|
} else {
|
2022-02-06 17:44:28 +01:00
|
|
|
Err(anyhow!(
|
|
|
|
|
"Relative speed comparison is not available for Markdown export."
|
2020-10-16 10:47:26 +02:00
|
|
|
))
|
2018-03-20 17:00:24 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-27 16:51:32 -07:00
|
|
|
fn table_header(unit_short_name: String) -> String {
|
2018-11-21 19:55:58 +01:00
|
|
|
format!(
|
2019-06-08 12:07:13 +02:00
|
|
|
"| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|:---|---:|---:|---:|---:|\n",
|
2018-11-21 19:55:58 +01:00
|
|
|
unit = unit_short_name
|
|
|
|
|
)
|
2018-10-27 16:51:32 -07:00
|
|
|
}
|
|
|
|
|
|
2018-09-09 08:44:13 -07:00
|
|
|
fn start_table(unit: Unit) -> Vec<u8> {
|
2018-10-27 16:51:32 -07:00
|
|
|
table_header(unit.short_name()).bytes().collect()
|
2018-03-20 17:00:24 -04:00
|
|
|
}
|
2018-09-09 08:44:13 -07:00
|
|
|
|
2019-06-08 12:04:26 +02:00
|
|
|
fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResultWithRelativeSpeed, unit: Unit) {
|
|
|
|
|
let result = &entry.result;
|
|
|
|
|
let mean_str = format_duration_value(result.mean, Some(unit)).0;
|
2021-11-14 10:18:28 +01:00
|
|
|
let stddev_str = if let Some(stddev) = result.stddev {
|
|
|
|
|
format!(" ± {}", format_duration_value(stddev, Some(unit)).0)
|
|
|
|
|
} else {
|
|
|
|
|
"".into()
|
|
|
|
|
};
|
2019-06-08 12:04:26 +02:00
|
|
|
let min_str = format_duration_value(result.min, Some(unit)).0;
|
|
|
|
|
let max_str = format_duration_value(result.max, Some(unit)).0;
|
2019-09-16 21:27:52 +02:00
|
|
|
let rel_str = format!("{:.2}", entry.relative_speed);
|
2019-10-13 15:40:17 +02:00
|
|
|
let rel_stddev_str = if entry.is_fastest {
|
|
|
|
|
"".into()
|
2022-02-05 22:14:06 +01:00
|
|
|
} else if let Some(stddev) = entry.relative_speed_stddev {
|
|
|
|
|
format!(" ± {:.2}", stddev)
|
2019-10-13 15:40:17 +02:00
|
|
|
} else {
|
2022-02-05 22:14:06 +01:00
|
|
|
"".into()
|
2019-10-13 15:40:17 +02:00
|
|
|
};
|
2018-09-09 08:44:13 -07:00
|
|
|
|
2018-03-20 17:00:24 -04:00
|
|
|
dest.extend(
|
|
|
|
|
format!(
|
2021-11-14 10:18:28 +01:00
|
|
|
"| `{command}` | {mean}{stddev} | {min} | {max} | {rel}{rel_stddev} |\n",
|
2019-06-08 12:04:26 +02:00
|
|
|
command = result.command.replace("|", "\\|"),
|
2018-11-21 19:55:58 +01:00
|
|
|
mean = mean_str,
|
|
|
|
|
stddev = stddev_str,
|
|
|
|
|
min = min_str,
|
|
|
|
|
max = max_str,
|
2019-06-08 12:04:26 +02:00
|
|
|
rel = rel_str,
|
2019-10-13 15:30:05 +02:00
|
|
|
rel_stddev = rel_stddev_str,
|
2018-11-21 19:55:58 +01:00
|
|
|
)
|
|
|
|
|
.as_bytes(),
|
2018-03-20 17:00:24 -04:00
|
|
|
);
|
|
|
|
|
}
|
2018-09-09 08:44:13 -07:00
|
|
|
|
|
|
|
|
/// Ensure the markdown output includes the table header and the multiple
|
|
|
|
|
/// benchmark results as a table. The list of actual times is not included
|
|
|
|
|
/// in the output.
|
|
|
|
|
///
|
|
|
|
|
/// This also demonstrates that the first entry's units (ms) are used to set
|
2018-10-29 18:44:44 -07:00
|
|
|
/// the units for all entries when the time unit is not given.
|
2018-09-09 08:44:13 -07:00
|
|
|
#[test]
|
|
|
|
|
fn test_markdown_format_ms() {
|
2020-09-14 20:28:02 -07:00
|
|
|
use std::collections::BTreeMap;
|
2018-09-09 08:44:13 -07:00
|
|
|
let exporter = MarkdownExporter::default();
|
|
|
|
|
|
2021-10-14 20:47:44 +03:00
|
|
|
let timing_results = vec![
|
2022-02-06 13:36:09 +01:00
|
|
|
BenchmarkResult {
|
|
|
|
|
command: String::from("sleep 0.1"),
|
|
|
|
|
mean: 0.1057,
|
|
|
|
|
stddev: Some(0.0016),
|
|
|
|
|
median: 0.1057,
|
|
|
|
|
user: 0.0009,
|
|
|
|
|
system: 0.0011,
|
|
|
|
|
min: 0.1023,
|
|
|
|
|
max: 0.1080,
|
|
|
|
|
times: Some(vec![0.1, 0.1, 0.1]),
|
|
|
|
|
exit_codes: vec![Some(0), Some(0), Some(0)],
|
|
|
|
|
parameters: BTreeMap::new(),
|
|
|
|
|
},
|
|
|
|
|
BenchmarkResult {
|
|
|
|
|
command: String::from("sleep 2"),
|
|
|
|
|
mean: 2.0050,
|
|
|
|
|
stddev: Some(0.0020),
|
|
|
|
|
median: 2.0050,
|
|
|
|
|
user: 0.0009,
|
|
|
|
|
system: 0.0012,
|
|
|
|
|
min: 2.0020,
|
|
|
|
|
max: 2.0080,
|
|
|
|
|
times: Some(vec![2.0, 2.0, 2.0]),
|
|
|
|
|
exit_codes: vec![Some(0), Some(0), Some(0)],
|
|
|
|
|
parameters: BTreeMap::new(),
|
|
|
|
|
},
|
2021-10-14 20:47:44 +03:00
|
|
|
];
|
2018-09-09 08:44:13 -07:00
|
|
|
|
2018-10-29 18:44:44 -07:00
|
|
|
let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
|
2018-09-09 08:44:13 -07:00
|
|
|
|
|
|
|
|
let formatted_expected = format!(
|
2018-11-21 19:55:58 +01:00
|
|
|
"{}\
|
2019-10-13 15:40:17 +02:00
|
|
|
| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
|
2019-10-13 15:30:05 +02:00
|
|
|
| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
|
2018-11-21 19:55:58 +01:00
|
|
|
",
|
|
|
|
|
table_header("ms".to_string())
|
|
|
|
|
);
|
2018-09-09 08:44:13 -07:00
|
|
|
|
|
|
|
|
assert_eq!(formatted_expected, formatted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This (again) demonstrates that the first entry's units (s) are used to set
|
2018-10-29 18:44:44 -07:00
|
|
|
/// the units for all entries when the time unit is not given.
|
2018-09-09 08:44:13 -07:00
|
|
|
#[test]
|
|
|
|
|
fn test_markdown_format_s() {
|
2020-09-14 20:28:02 -07:00
|
|
|
use std::collections::BTreeMap;
|
2018-09-09 08:44:13 -07:00
|
|
|
let exporter = MarkdownExporter::default();
|
|
|
|
|
|
2021-10-14 20:47:44 +03:00
|
|
|
let timing_results = vec![
|
2022-02-06 13:36:09 +01:00
|
|
|
BenchmarkResult {
|
|
|
|
|
command: String::from("sleep 2"),
|
|
|
|
|
mean: 2.0050,
|
|
|
|
|
stddev: Some(0.0020),
|
|
|
|
|
median: 2.0050,
|
|
|
|
|
user: 0.0009,
|
|
|
|
|
system: 0.0012,
|
|
|
|
|
min: 2.0020,
|
|
|
|
|
max: 2.0080,
|
|
|
|
|
times: Some(vec![2.0, 2.0, 2.0]),
|
|
|
|
|
exit_codes: vec![Some(0), Some(0), Some(0)],
|
|
|
|
|
parameters: BTreeMap::new(),
|
|
|
|
|
},
|
|
|
|
|
BenchmarkResult {
|
|
|
|
|
command: String::from("sleep 0.1"),
|
|
|
|
|
mean: 0.1057,
|
|
|
|
|
stddev: Some(0.0016),
|
|
|
|
|
median: 0.1057,
|
|
|
|
|
user: 0.0009,
|
|
|
|
|
system: 0.0011,
|
|
|
|
|
min: 0.1023,
|
|
|
|
|
max: 0.1080,
|
|
|
|
|
times: Some(vec![0.1, 0.1, 0.1]),
|
|
|
|
|
exit_codes: vec![Some(0), Some(0), Some(0)],
|
|
|
|
|
parameters: BTreeMap::new(),
|
|
|
|
|
},
|
2021-10-14 20:47:44 +03:00
|
|
|
];
|
2018-09-09 08:44:13 -07:00
|
|
|
|
2018-10-29 18:44:44 -07:00
|
|
|
let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
|
2018-09-09 08:44:13 -07:00
|
|
|
|
|
|
|
|
let formatted_expected = format!(
|
2018-11-21 19:55:58 +01:00
|
|
|
"{}\
|
2019-10-13 15:30:05 +02:00
|
|
|
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
|
2019-10-13 15:40:17 +02:00
|
|
|
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
|
2018-11-21 19:55:58 +01:00
|
|
|
",
|
|
|
|
|
table_header("s".to_string())
|
|
|
|
|
);
|
2018-09-09 08:44:13 -07:00
|
|
|
|
|
|
|
|
assert_eq!(formatted_expected, formatted);
|
|
|
|
|
}
|
2018-10-29 18:44:44 -07:00
|
|
|
|
|
|
|
|
/// The given time unit (s) is used to set the units for all entries.
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_markdown_format_time_unit_s() {
|
2020-09-14 20:28:02 -07:00
|
|
|
use std::collections::BTreeMap;
|
2018-10-29 18:44:44 -07:00
|
|
|
let exporter = MarkdownExporter::default();
|
|
|
|
|
|
2021-10-14 20:47:44 +03:00
|
|
|
let timing_results = vec![
|
2022-02-06 13:36:09 +01:00
|
|
|
BenchmarkResult {
|
|
|
|
|
command: String::from("sleep 0.1"),
|
|
|
|
|
mean: 0.1057,
|
|
|
|
|
stddev: Some(0.0016),
|
|
|
|
|
median: 0.1057,
|
|
|
|
|
user: 0.0009,
|
|
|
|
|
system: 0.0011,
|
|
|
|
|
min: 0.1023,
|
|
|
|
|
max: 0.1080,
|
|
|
|
|
times: Some(vec![0.1, 0.1, 0.1]),
|
|
|
|
|
exit_codes: vec![Some(0), Some(0), Some(0)],
|
|
|
|
|
parameters: BTreeMap::new(),
|
|
|
|
|
},
|
|
|
|
|
BenchmarkResult {
|
|
|
|
|
command: String::from("sleep 2"),
|
|
|
|
|
mean: 2.0050,
|
|
|
|
|
stddev: Some(0.0020),
|
|
|
|
|
median: 2.0050,
|
|
|
|
|
user: 0.0009,
|
|
|
|
|
system: 0.0012,
|
|
|
|
|
min: 2.0020,
|
|
|
|
|
max: 2.0080,
|
|
|
|
|
times: Some(vec![2.0, 2.0, 2.0]),
|
|
|
|
|
exit_codes: vec![Some(0), Some(0), Some(0)],
|
|
|
|
|
parameters: BTreeMap::new(),
|
|
|
|
|
},
|
2021-10-14 20:47:44 +03:00
|
|
|
];
|
2018-10-29 18:44:44 -07:00
|
|
|
|
2018-11-21 19:55:58 +01:00
|
|
|
let formatted = String::from_utf8(
|
|
|
|
|
exporter
|
|
|
|
|
.serialize(&timing_results, Some(Unit::Second))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2018-10-29 18:44:44 -07:00
|
|
|
|
|
|
|
|
let formatted_expected = format!(
|
2018-11-21 19:55:58 +01:00
|
|
|
"{}\
|
2019-10-13 15:40:17 +02:00
|
|
|
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
|
2019-10-13 15:30:05 +02:00
|
|
|
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
|
2018-11-21 19:55:58 +01:00
|
|
|
",
|
|
|
|
|
table_header("s".to_string())
|
|
|
|
|
);
|
2018-10-29 18:44:44 -07:00
|
|
|
|
|
|
|
|
assert_eq!(formatted_expected, formatted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This (again) demonstrates that the given time unit (ms) is used to set
|
|
|
|
|
/// the units for all entries.
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_markdown_format_time_unit_ms() {
|
2020-09-14 20:28:02 -07:00
|
|
|
use std::collections::BTreeMap;
|
2018-10-29 18:44:44 -07:00
|
|
|
let exporter = MarkdownExporter::default();
|
|
|
|
|
|
2021-10-14 20:47:44 +03:00
|
|
|
let timing_results = vec![
|
2022-02-06 13:36:09 +01:00
|
|
|
BenchmarkResult {
|
|
|
|
|
command: String::from("sleep 2"),
|
|
|
|
|
mean: 2.0050,
|
|
|
|
|
stddev: Some(0.0020),
|
|
|
|
|
median: 2.0050,
|
|
|
|
|
user: 0.0009,
|
|
|
|
|
system: 0.0012,
|
|
|
|
|
min: 2.0020,
|
|
|
|
|
max: 2.0080,
|
|
|
|
|
times: Some(vec![2.0, 2.0, 2.0]),
|
|
|
|
|
exit_codes: vec![Some(0), Some(0), Some(0)],
|
|
|
|
|
parameters: BTreeMap::new(),
|
|
|
|
|
},
|
|
|
|
|
BenchmarkResult {
|
|
|
|
|
command: String::from("sleep 0.1"),
|
|
|
|
|
mean: 0.1057,
|
|
|
|
|
stddev: Some(0.0016),
|
|
|
|
|
median: 0.1057,
|
|
|
|
|
user: 0.0009,
|
|
|
|
|
system: 0.0011,
|
|
|
|
|
min: 0.1023,
|
|
|
|
|
max: 0.1080,
|
|
|
|
|
times: Some(vec![0.1, 0.1, 0.1]),
|
|
|
|
|
exit_codes: vec![Some(0), Some(0), Some(0)],
|
|
|
|
|
parameters: BTreeMap::new(),
|
|
|
|
|
},
|
2021-10-14 20:47:44 +03:00
|
|
|
];
|
2018-10-29 18:44:44 -07:00
|
|
|
|
2018-11-21 19:55:58 +01:00
|
|
|
let formatted = String::from_utf8(
|
|
|
|
|
exporter
|
|
|
|
|
.serialize(&timing_results, Some(Unit::MilliSecond))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2018-10-29 18:44:44 -07:00
|
|
|
|
|
|
|
|
let formatted_expected = format!(
|
2018-11-21 19:55:58 +01:00
|
|
|
"{}\
|
2019-10-13 15:30:05 +02:00
|
|
|
| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
|
2019-10-13 15:40:17 +02:00
|
|
|
| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
|
2018-11-21 19:55:58 +01:00
|
|
|
",
|
|
|
|
|
table_header("ms".to_string())
|
|
|
|
|
);
|
2018-10-29 18:44:44 -07:00
|
|
|
|
|
|
|
|
assert_eq!(formatted_expected, formatted);
|
|
|
|
|
}
|