2022-05-15 21:09:33 +02:00
|
|
|
use crate::export::markup::MarkupExporter;
|
2022-02-06 17:44:28 +01:00
|
|
|
|
2022-05-15 21:28:35 +02:00
|
|
|
use super::markup::Alignment;
|
|
|
|
|
|
2022-03-24 07:38:18 +01:00
|
|
|
#[derive(Default)]
|
2022-05-15 21:09:33 +02:00
|
|
|
pub struct MarkdownExporter {}
|
2022-03-24 07:38:18 +01:00
|
|
|
|
2022-05-15 21:09:33 +02:00
|
|
|
impl MarkupExporter for MarkdownExporter {
|
2022-05-15 21:28:35 +02:00
|
|
|
fn table_row(&self, cells: &[&str]) -> String {
|
|
|
|
|
format!("| {} |\n", cells.join(" | "))
|
2022-03-24 07:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-15 21:28:35 +02:00
|
|
|
fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String {
|
|
|
|
|
format!(
|
|
|
|
|
"|{}\n",
|
|
|
|
|
cell_aligmnents
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|a| match a {
|
|
|
|
|
Alignment::Left => ":---|",
|
|
|
|
|
Alignment::Right => "---:|",
|
|
|
|
|
})
|
|
|
|
|
.collect::<String>()
|
|
|
|
|
)
|
2022-03-24 07:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn command(&self, cmd: &str) -> String {
|
|
|
|
|
format!("`{}`", cmd)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check Markdown-based data row formatting
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_markdown_formatter_table_data() {
|
2022-05-15 21:09:33 +02:00
|
|
|
let formatter = MarkdownExporter::default();
|
2022-03-24 07:38:18 +01:00
|
|
|
|
2022-05-15 21:28:35 +02:00
|
|
|
assert_eq!(formatter.table_row(&["a", "b", "c"]), "| a | b | c |\n");
|
2022-03-24 07:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check Markdown-based horizontal line formatting
|
|
|
|
|
#[test]
|
2022-05-15 21:28:35 +02:00
|
|
|
fn test_markdown_formatter_table_divider() {
|
2022-05-15 21:09:33 +02:00
|
|
|
let formatter = MarkdownExporter::default();
|
2022-03-24 07:38:18 +01:00
|
|
|
|
2022-05-15 21:29:36 +02:00
|
|
|
let divider = formatter.table_divider(&[Alignment::Left, Alignment::Right, Alignment::Left]);
|
|
|
|
|
assert_eq!(divider, "|:---|---:|:---|\n");
|
2022-03-24 07:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-21 19:57:57 +01:00
|
|
|
/// Test helper function to create unit-based header and horizontal line
|
2022-03-21 20:57:24 +01:00
|
|
|
/// independently from the markup functionality for Markdown.
|
2022-03-21 19:57:57 +01:00
|
|
|
#[cfg(test)]
|
2022-03-24 07:38:18 +01:00
|
|
|
fn cfg_test_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
|
|
|
/// 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() {
|
2022-05-15 21:09:33 +02:00
|
|
|
use super::Exporter;
|
|
|
|
|
use crate::benchmark::benchmark_result::BenchmarkResult;
|
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
|
|
|
|
2022-03-24 07:38:18 +01:00
|
|
|
let actual = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
|
|
|
|
|
let expect = 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
|
|
|
",
|
2022-03-24 07:38:18 +01:00
|
|
|
cfg_test_table_header("ms".to_string())
|
2018-11-21 19:55:58 +01:00
|
|
|
);
|
2018-09-09 08:44:13 -07:00
|
|
|
|
2022-03-24 07:38:18 +01:00
|
|
|
assert_eq!(expect, actual);
|
2018-09-09 08:44:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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() {
|
2022-05-15 21:09:33 +02:00
|
|
|
use super::Exporter;
|
|
|
|
|
use crate::benchmark::benchmark_result::BenchmarkResult;
|
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
|
|
|
|
2022-03-24 07:38:18 +01:00
|
|
|
let actual = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
|
|
|
|
|
let expect = 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
|
|
|
",
|
2022-03-24 07:38:18 +01:00
|
|
|
cfg_test_table_header("s".to_string())
|
2018-11-21 19:55:58 +01:00
|
|
|
);
|
2018-09-09 08:44:13 -07:00
|
|
|
|
2022-03-24 07:38:18 +01:00
|
|
|
assert_eq!(expect, actual);
|
2018-09-09 08:44:13 -07:00
|
|
|
}
|
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() {
|
2022-05-15 21:09:33 +02:00
|
|
|
use super::Exporter;
|
|
|
|
|
use crate::benchmark::benchmark_result::BenchmarkResult;
|
|
|
|
|
use crate::util::units::Unit;
|
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
|
|
|
|
2022-03-24 07:38:18 +01:00
|
|
|
let actual = String::from_utf8(
|
2018-11-21 19:55:58 +01:00
|
|
|
exporter
|
|
|
|
|
.serialize(&timing_results, Some(Unit::Second))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2022-03-24 07:38:18 +01:00
|
|
|
let expect = 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
|
|
|
",
|
2022-03-24 07:38:18 +01:00
|
|
|
cfg_test_table_header("s".to_string())
|
2018-11-21 19:55:58 +01:00
|
|
|
);
|
2018-10-29 18:44:44 -07:00
|
|
|
|
2022-03-24 07:38:18 +01:00
|
|
|
assert_eq!(expect, actual);
|
2018-10-29 18:44:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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() {
|
2022-05-15 21:09:33 +02:00
|
|
|
use super::Exporter;
|
|
|
|
|
use crate::benchmark::benchmark_result::BenchmarkResult;
|
|
|
|
|
use crate::util::units::Unit;
|
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
|
|
|
|
2022-03-24 07:38:18 +01:00
|
|
|
let actual = String::from_utf8(
|
2018-11-21 19:55:58 +01:00
|
|
|
exporter
|
|
|
|
|
.serialize(&timing_results, Some(Unit::MilliSecond))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2022-03-24 07:38:18 +01:00
|
|
|
let expect = 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
|
|
|
",
|
2022-03-24 07:38:18 +01:00
|
|
|
cfg_test_table_header("ms".to_string())
|
2018-11-21 19:55:58 +01:00
|
|
|
);
|
2018-10-29 18:44:44 -07:00
|
|
|
|
2022-03-24 07:38:18 +01:00
|
|
|
assert_eq!(expect, actual);
|
2018-10-29 18:44:44 -07:00
|
|
|
}
|