2021-11-30 18:30:30 +01:00
|
|
|
use std::io::{Error, ErrorKind, Result};
|
2019-01-29 18:02:19 -05:00
|
|
|
|
2021-08-24 21:24:34 +02:00
|
|
|
use crate::benchmark_result::BenchmarkResult;
|
|
|
|
|
use crate::format::format_duration_value;
|
2021-11-30 18:30:30 +01:00
|
|
|
use crate::relative_speed::{self, BenchmarkResultWithRelativeSpeed};
|
2021-08-24 21:24:34 +02:00
|
|
|
use crate::units::Unit;
|
2019-01-29 18:02:19 -05:00
|
|
|
|
2021-11-30 18:30:30 +01:00
|
|
|
use super::Exporter;
|
|
|
|
|
|
2019-01-29 18:02:19 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct AsciidocExporter {}
|
|
|
|
|
|
|
|
|
|
impl Exporter for AsciidocExporter {
|
2019-07-13 10:29:05 +02:00
|
|
|
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {
|
2019-01-29 18:02:19 -05:00
|
|
|
let unit = if let Some(unit) = unit {
|
|
|
|
|
// Use the given unit for all entries.
|
|
|
|
|
unit
|
|
|
|
|
} else if let Some(first_result) = results.first() {
|
|
|
|
|
// Use the first BenchmarkResult entry to determine the unit for all entries.
|
|
|
|
|
format_duration_value(first_result.mean, None).1
|
|
|
|
|
} else {
|
|
|
|
|
// Default to `Second`.
|
|
|
|
|
Unit::Second
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-30 18:30:30 +01:00
|
|
|
if let Some(annotated_results) = relative_speed::compute(results) {
|
|
|
|
|
let mut res: Vec<u8> = Vec::new();
|
|
|
|
|
res.append(&mut table_open());
|
|
|
|
|
res.append(&mut table_startend());
|
|
|
|
|
res.append(&mut table_header(unit));
|
|
|
|
|
for result in annotated_results {
|
|
|
|
|
res.push(b'\n');
|
|
|
|
|
res.append(&mut table_row(&result, unit));
|
|
|
|
|
}
|
|
|
|
|
res.append(&mut table_startend());
|
2019-01-29 18:02:19 -05:00
|
|
|
|
2021-11-30 18:30:30 +01:00
|
|
|
Ok(res)
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::new(
|
|
|
|
|
ErrorKind::Other,
|
|
|
|
|
"Relative speed comparison is not available for Asciidoctor export.",
|
|
|
|
|
))
|
|
|
|
|
}
|
2019-01-29 18:02:19 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn table_open() -> Vec<u8> {
|
2021-11-30 18:30:30 +01:00
|
|
|
"[cols=\"<,>,>,>,>\"]\n".bytes().collect()
|
2019-01-29 18:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn table_startend() -> Vec<u8> {
|
|
|
|
|
"|===\n".bytes().collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn table_header(unittype: Unit) -> Vec<u8> {
|
|
|
|
|
let unit_short_name = unittype.short_name();
|
|
|
|
|
format!(
|
2021-11-30 18:30:30 +01:00
|
|
|
"| Command \n| Mean [{unit}] \n| Min [{unit}] \n| Max [{unit}] \n| Relative \n",
|
2019-01-29 18:02:19 -05:00
|
|
|
unit = unit_short_name
|
2019-03-29 07:59:23 +05:30
|
|
|
)
|
|
|
|
|
.into_bytes()
|
2019-01-29 18:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
2021-11-30 18:30:30 +01:00
|
|
|
fn table_row(entry: &BenchmarkResultWithRelativeSpeed, unit: Unit) -> Vec<u8> {
|
|
|
|
|
let result = &entry.result;
|
|
|
|
|
let mean_str = format_duration_value(result.mean, Some(unit)).0;
|
|
|
|
|
let stddev_str = if let Some(stddev) = result.stddev {
|
|
|
|
|
format!(" ± {}", format_duration_value(stddev, Some(unit)).0)
|
|
|
|
|
} else {
|
|
|
|
|
"".into()
|
|
|
|
|
};
|
|
|
|
|
let min_str = format_duration_value(result.min, Some(unit)).0;
|
|
|
|
|
let max_str = format_duration_value(result.max, Some(unit)).0;
|
|
|
|
|
let rel_str = format!("{:.2}", entry.relative_speed);
|
|
|
|
|
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)
|
2021-11-30 18:30:30 +01:00
|
|
|
} else {
|
2022-02-05 22:14:06 +01:00
|
|
|
"".into()
|
2021-11-30 18:30:30 +01:00
|
|
|
};
|
|
|
|
|
|
2019-01-29 18:02:19 -05:00
|
|
|
format!(
|
2021-11-30 18:30:30 +01:00
|
|
|
"| `{command}` \n\
|
|
|
|
|
| {mean}{stddev} \n\
|
|
|
|
|
| {min} \n\
|
|
|
|
|
| {max} \n\
|
|
|
|
|
| {rel}{rel_stddev} \n",
|
|
|
|
|
command = result.command.replace("|", "\\|"),
|
|
|
|
|
mean = mean_str,
|
|
|
|
|
stddev = stddev_str,
|
|
|
|
|
min = min_str,
|
|
|
|
|
max = max_str,
|
|
|
|
|
rel = rel_str,
|
|
|
|
|
rel_stddev = rel_stddev_str
|
2019-03-29 07:59:23 +05:30
|
|
|
)
|
|
|
|
|
.into_bytes()
|
2019-01-29 18:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Ensure various options for the header generate correct results
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_asciidoc_header() {
|
2021-11-30 18:30:30 +01:00
|
|
|
let conms: Vec<u8> = "| Command \n| Mean [ms] \n| Min [ms] \n| Max [ms] \n| Relative \n"
|
|
|
|
|
.bytes()
|
|
|
|
|
.collect();
|
|
|
|
|
let cons: Vec<u8> = "| Command \n| Mean [s] \n| Min [s] \n| Max [s] \n| Relative \n"
|
|
|
|
|
.bytes()
|
|
|
|
|
.collect();
|
2019-01-29 18:02:19 -05:00
|
|
|
let genms = table_header(Unit::MilliSecond);
|
2019-03-29 07:59:23 +05:30
|
|
|
let gens = table_header(Unit::Second);
|
2019-01-29 18:02:19 -05:00
|
|
|
|
|
|
|
|
assert_eq!(conms, genms);
|
|
|
|
|
assert_eq!(cons, gens);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Ensure each table row is generated properly
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_asciidoc_table_row() {
|
2020-09-14 20:28:02 -07:00
|
|
|
use std::collections::BTreeMap;
|
2019-01-29 18:02:19 -05:00
|
|
|
|
2021-11-30 18:30:30 +01:00
|
|
|
let timing_result = BenchmarkResultWithRelativeSpeed {
|
|
|
|
|
result: &BenchmarkResult::new(
|
|
|
|
|
String::from("sleep 1"), // command
|
|
|
|
|
0.10491992406666667, // mean
|
|
|
|
|
Some(0.00397851689425097), // stddev
|
|
|
|
|
0.10491992406666667, // median
|
|
|
|
|
0.005182013333333333, // user
|
|
|
|
|
0.0, // system
|
|
|
|
|
0.1003342584, // min
|
|
|
|
|
0.10745223440000001, // max
|
|
|
|
|
vec![
|
|
|
|
|
// times
|
|
|
|
|
0.1003342584,
|
|
|
|
|
0.10745223440000001,
|
|
|
|
|
0.10697327940000001,
|
|
|
|
|
],
|
|
|
|
|
vec![Some(0), Some(0), Some(0)], // exit codes
|
|
|
|
|
BTreeMap::new(), // param
|
|
|
|
|
),
|
|
|
|
|
relative_speed: 1.000, // relative_speed
|
|
|
|
|
relative_speed_stddev: Option::from(1.03), // relative_speed_stddev
|
|
|
|
|
is_fastest: true, // is_fastest
|
|
|
|
|
};
|
2019-01-29 18:02:19 -05:00
|
|
|
|
2021-11-30 18:30:30 +01:00
|
|
|
let formatted = String::from_utf8(table_row(&timing_result, Unit::MilliSecond)).unwrap();
|
2019-01-29 18:02:19 -05:00
|
|
|
|
2021-11-30 18:30:30 +01:00
|
|
|
let formatted_expected = "| `sleep 1` \n\
|
|
|
|
|
| 104.9 ± 4.0 \n\
|
|
|
|
|
| 100.3 \n\
|
|
|
|
|
| 107.5 \n\
|
|
|
|
|
| 1.00 \n";
|
|
|
|
|
|
|
|
|
|
assert_eq!(formatted_expected, formatted);
|
|
|
|
|
|
|
|
|
|
let formatted_seconds = String::from_utf8(table_row(&timing_result, Unit::Second)).unwrap();
|
|
|
|
|
let formatted_expected_seconds = "| `sleep 1` \n\
|
|
|
|
|
| 0.105 ± 0.004 \n\
|
|
|
|
|
| 0.100 \n\
|
|
|
|
|
| 0.107 \n\
|
|
|
|
|
| 1.00 \n";
|
|
|
|
|
|
|
|
|
|
assert_eq!(formatted_expected_seconds, formatted_seconds);
|
2019-01-29 18:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Ensure commands get properly escaped
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_asciidoc_table_row_command_escape() {
|
2020-09-14 20:28:02 -07:00
|
|
|
use std::collections::BTreeMap;
|
2021-11-30 18:30:30 +01:00
|
|
|
let benchmark_result = BenchmarkResultWithRelativeSpeed {
|
|
|
|
|
result: &BenchmarkResult::new(
|
|
|
|
|
String::from("sleep 1|"), // command
|
|
|
|
|
0.10491992406666667, // mean
|
|
|
|
|
Some(0.00397851689425097), // stddev
|
|
|
|
|
0.10491992406666667, // median
|
|
|
|
|
0.005182013333333333, // user
|
|
|
|
|
0.0, // system
|
|
|
|
|
0.1003342584, // min
|
|
|
|
|
0.10745223440000001, // max
|
|
|
|
|
vec![
|
|
|
|
|
// times
|
|
|
|
|
0.1003342584,
|
|
|
|
|
0.10745223440000001,
|
|
|
|
|
0.10697327940000001,
|
|
|
|
|
],
|
|
|
|
|
vec![Some(0), Some(0), Some(0)], // exit codes
|
|
|
|
|
BTreeMap::new(), // param
|
|
|
|
|
),
|
|
|
|
|
relative_speed: 1.000, // relative_speed
|
|
|
|
|
relative_speed_stddev: Option::from(1.03), // relative_speed_stddev
|
|
|
|
|
is_fastest: true, // is_fastest
|
|
|
|
|
};
|
|
|
|
|
let expected = String::from_utf8(
|
|
|
|
|
format!(
|
|
|
|
|
"| `sleep 1\\|` \n\
|
|
|
|
|
| {} ± {} \n\
|
|
|
|
|
| {} \n\
|
|
|
|
|
| {} \n\
|
|
|
|
|
| {:.2} \n",
|
|
|
|
|
Unit::Second.format(benchmark_result.result.mean),
|
|
|
|
|
Unit::Second.format(benchmark_result.result.stddev.unwrap()),
|
|
|
|
|
Unit::Second.format(benchmark_result.result.min),
|
|
|
|
|
Unit::Second.format(benchmark_result.result.max),
|
|
|
|
|
benchmark_result.relative_speed
|
|
|
|
|
)
|
|
|
|
|
.into_bytes(),
|
2019-01-29 18:02:19 -05:00
|
|
|
);
|
2021-11-30 18:30:30 +01:00
|
|
|
let generated_seconds = String::from_utf8(table_row(&benchmark_result, Unit::Second));
|
2019-01-29 18:02:19 -05:00
|
|
|
|
2021-11-30 18:30:30 +01:00
|
|
|
assert_eq!(expected, generated_seconds);
|
2019-01-29 18:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Integration test
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_asciidoc() {
|
2020-09-14 20:28:02 -07:00
|
|
|
use std::collections::BTreeMap;
|
2019-01-29 18:02:19 -05:00
|
|
|
let exporter = AsciidocExporter::default();
|
|
|
|
|
// NOTE: results are fabricated, unlike above
|
|
|
|
|
let results = vec![
|
|
|
|
|
BenchmarkResult::new(
|
2020-09-14 20:28:02 -07:00
|
|
|
String::from("FOO=1 BAR=2 command | 1"),
|
2019-01-29 18:02:19 -05:00
|
|
|
1.0,
|
2021-11-14 10:18:28 +01:00
|
|
|
Some(2.0),
|
2019-06-08 13:04:39 +02:00
|
|
|
1.0,
|
2019-01-29 18:02:19 -05:00
|
|
|
3.0,
|
|
|
|
|
4.0,
|
|
|
|
|
5.0,
|
|
|
|
|
6.0,
|
2019-03-29 07:59:23 +05:30
|
|
|
vec![7.0, 8.0, 9.0],
|
2021-05-09 18:20:59 +02:00
|
|
|
vec![Some(0), Some(0), Some(0)],
|
2020-09-14 20:28:02 -07:00
|
|
|
{
|
|
|
|
|
let mut params = BTreeMap::new();
|
|
|
|
|
params.insert("foo".into(), "1".into());
|
|
|
|
|
params.insert("bar".into(), "2".into());
|
|
|
|
|
params
|
|
|
|
|
},
|
2019-01-29 18:02:19 -05:00
|
|
|
),
|
|
|
|
|
BenchmarkResult::new(
|
2020-09-14 20:28:02 -07:00
|
|
|
String::from("FOO=1 BAR=7 command | 2"),
|
2019-01-29 18:02:19 -05:00
|
|
|
11.0,
|
2021-11-14 10:18:28 +01:00
|
|
|
Some(12.0),
|
2019-06-08 13:04:39 +02:00
|
|
|
11.0,
|
2019-01-29 18:02:19 -05:00
|
|
|
13.0,
|
|
|
|
|
14.0,
|
|
|
|
|
15.0,
|
|
|
|
|
16.0,
|
2019-03-29 07:59:23 +05:30
|
|
|
vec![17.0, 18.0, 19.0],
|
2021-05-09 18:20:59 +02:00
|
|
|
vec![Some(0), Some(0), Some(0)],
|
2020-09-14 20:28:02 -07:00
|
|
|
{
|
|
|
|
|
let mut params = BTreeMap::new();
|
|
|
|
|
params.insert("foo".into(), "1".into());
|
|
|
|
|
params.insert("bar".into(), "7".into());
|
|
|
|
|
params
|
|
|
|
|
},
|
2019-03-29 07:59:23 +05:30
|
|
|
),
|
2019-01-29 18:02:19 -05:00
|
|
|
];
|
|
|
|
|
// NOTE: only testing with s, s/ms is tested elsewhere
|
2021-11-30 18:30:30 +01:00
|
|
|
let expected: String = String::from(
|
|
|
|
|
"[cols=\"<,>,>,>,>\"]\n\
|
2019-01-29 18:02:19 -05:00
|
|
|
|===\n\
|
2021-11-30 18:30:30 +01:00
|
|
|
| Command \n\
|
|
|
|
|
| Mean [s] \n\
|
|
|
|
|
| Min [s] \n\
|
|
|
|
|
| Max [s] \n\
|
|
|
|
|
| Relative \n\
|
2019-01-29 18:02:19 -05:00
|
|
|
\n\
|
2021-11-30 18:30:30 +01:00
|
|
|
| `FOO=1 BAR=2 command \\| 1` \n\
|
|
|
|
|
| 1.000 ± 2.000 \n\
|
|
|
|
|
| 5.000 \n\
|
|
|
|
|
| 6.000 \n\
|
|
|
|
|
| 1.00 \n\
|
2019-01-29 18:02:19 -05:00
|
|
|
\n\
|
2021-11-30 18:30:30 +01:00
|
|
|
| `FOO=1 BAR=7 command \\| 2` \n\
|
|
|
|
|
| 11.000 ± 12.000 \n\
|
|
|
|
|
| 15.000 \n\
|
|
|
|
|
| 16.000 \n\
|
|
|
|
|
| 11.00 ± 25.06 \n\
|
2019-01-29 18:02:19 -05:00
|
|
|
|===\n\
|
2019-03-29 07:59:23 +05:30
|
|
|
",
|
|
|
|
|
);
|
2021-11-30 18:30:30 +01:00
|
|
|
let given =
|
2019-03-29 07:59:23 +05:30
|
|
|
String::from_utf8(exporter.serialize(&results, Some(Unit::Second)).unwrap()).unwrap();
|
2019-01-29 18:02:19 -05:00
|
|
|
|
2021-11-30 18:30:30 +01:00
|
|
|
assert_eq!(expected, given);
|
2019-01-29 18:02:19 -05:00
|
|
|
}
|