2021-08-24 21:33:48 +02:00
|
|
|
use std::io::Result;
|
2019-01-29 18:02:19 -05:00
|
|
|
|
2021-08-24 21:33:48 +02:00
|
|
|
use super::Exporter;
|
2021-08-24 21:24:34 +02:00
|
|
|
use crate::benchmark_result::BenchmarkResult;
|
|
|
|
|
use crate::format::format_duration_value;
|
|
|
|
|
use crate::units::Unit;
|
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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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 results {
|
2019-07-13 10:39:39 +02:00
|
|
|
res.push(b'\n');
|
2019-01-29 18:02:19 -05:00
|
|
|
res.append(&mut table_row(result, unit));
|
|
|
|
|
}
|
|
|
|
|
res.append(&mut table_startend());
|
|
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn table_open() -> Vec<u8> {
|
|
|
|
|
"[cols=\"<,>,>\"]\n".bytes().collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn table_startend() -> Vec<u8> {
|
|
|
|
|
"|===\n".bytes().collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn table_header(unittype: Unit) -> Vec<u8> {
|
|
|
|
|
let unit_short_name = unittype.short_name();
|
|
|
|
|
format!(
|
|
|
|
|
"| Command | Mean [{unit}] | Min…Max [{unit}]\n",
|
|
|
|
|
unit = unit_short_name
|
2019-03-29 07:59:23 +05:30
|
|
|
)
|
|
|
|
|
.into_bytes()
|
2019-01-29 18:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn table_row(entry: &BenchmarkResult, unit: Unit) -> Vec<u8> {
|
|
|
|
|
let form = |val| format_duration_value(val, Some(unit));
|
|
|
|
|
format!(
|
|
|
|
|
"| `{}`\n\
|
|
|
|
|
| {} ± {}\n\
|
|
|
|
|
| {}…{}\n",
|
2019-03-29 07:59:23 +05:30
|
|
|
entry.command.replace("|", "\\|"),
|
|
|
|
|
form(entry.mean).0,
|
|
|
|
|
form(entry.stddev).0,
|
|
|
|
|
form(entry.min).0,
|
|
|
|
|
form(entry.max).0
|
|
|
|
|
)
|
|
|
|
|
.into_bytes()
|
2019-01-29 18:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Ensure various options for the header generate correct results
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_asciidoc_header() {
|
|
|
|
|
let conms: Vec<u8> = "| Command | Mean [ms] | Min…Max [ms]\n".bytes().collect();
|
2019-03-29 07:59:23 +05:30
|
|
|
let cons: Vec<u8> = "| Command | Mean [s] | Min…Max [s]\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
|
|
|
let result = BenchmarkResult::new(
|
|
|
|
|
String::from("sleep 1"), // command
|
|
|
|
|
0.10491992406666667, // mean
|
|
|
|
|
0.00397851689425097, // stddev
|
2019-06-08 13:04:39 +02:00
|
|
|
0.10491992406666667, // median
|
2019-01-29 18:02:19 -05:00
|
|
|
0.005182013333333333, // user
|
|
|
|
|
0.0, // system
|
|
|
|
|
0.1003342584, // min
|
|
|
|
|
0.10745223440000001, // max
|
2019-03-29 07:59:23 +05:30
|
|
|
vec![
|
|
|
|
|
// times
|
2019-01-29 18:02:19 -05:00
|
|
|
0.1003342584,
|
|
|
|
|
0.10745223440000001,
|
2019-03-29 07:59:23 +05:30
|
|
|
0.10697327940000001,
|
2019-01-29 18:02:19 -05:00
|
|
|
],
|
2021-05-09 18:20:59 +02:00
|
|
|
vec![Some(0), Some(0), Some(0)], // exit codes
|
|
|
|
|
BTreeMap::new(), // param
|
2019-01-29 18:02:19 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let expms = format!(
|
|
|
|
|
"| `{}`\n\
|
|
|
|
|
| {} ± {}\n\
|
|
|
|
|
| {}…{}\n",
|
|
|
|
|
result.command,
|
2019-03-29 07:59:23 +05:30
|
|
|
Unit::MilliSecond.format(result.mean),
|
|
|
|
|
Unit::MilliSecond.format(result.stddev),
|
|
|
|
|
Unit::MilliSecond.format(result.min),
|
|
|
|
|
Unit::MilliSecond.format(result.max)
|
|
|
|
|
)
|
|
|
|
|
.into_bytes();
|
2019-01-29 18:02:19 -05:00
|
|
|
let exps = format!(
|
|
|
|
|
"| `{}`\n\
|
|
|
|
|
| {} ± {}\n\
|
|
|
|
|
| {}…{}\n",
|
|
|
|
|
result.command,
|
2019-03-29 07:59:23 +05:30
|
|
|
Unit::Second.format(result.mean),
|
|
|
|
|
Unit::Second.format(result.stddev),
|
|
|
|
|
Unit::Second.format(result.min),
|
|
|
|
|
Unit::Second.format(result.max)
|
|
|
|
|
)
|
|
|
|
|
.into_bytes();
|
2019-01-29 18:02:19 -05:00
|
|
|
|
|
|
|
|
let genms = table_row(&result, Unit::MilliSecond);
|
2019-03-29 07:59:23 +05:30
|
|
|
let gens = table_row(&result, Unit::Second);
|
2019-01-29 18:02:19 -05:00
|
|
|
|
|
|
|
|
assert_eq!(expms, genms);
|
|
|
|
|
assert_eq!(exps, gens);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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;
|
2019-01-29 18:02:19 -05:00
|
|
|
let result = BenchmarkResult::new(
|
|
|
|
|
String::from("sleep 1|"), // command
|
|
|
|
|
0.10491992406666667, // mean
|
|
|
|
|
0.00397851689425097, // stddev
|
2019-06-08 13:04:39 +02:00
|
|
|
0.10491992406666667, // median
|
2019-01-29 18:02:19 -05:00
|
|
|
0.005182013333333333, // user
|
|
|
|
|
0.0, // system
|
|
|
|
|
0.1003342584, // min
|
|
|
|
|
0.10745223440000001, // max
|
2019-03-29 07:59:23 +05:30
|
|
|
vec![
|
|
|
|
|
// times
|
2019-01-29 18:02:19 -05:00
|
|
|
0.1003342584,
|
|
|
|
|
0.10745223440000001,
|
2019-03-29 07:59:23 +05:30
|
|
|
0.10697327940000001,
|
2019-01-29 18:02:19 -05:00
|
|
|
],
|
2021-05-09 18:20:59 +02:00
|
|
|
vec![Some(0), Some(0), Some(0)], // exit codes
|
|
|
|
|
BTreeMap::new(), // param
|
2019-01-29 18:02:19 -05:00
|
|
|
);
|
|
|
|
|
let exps = format!(
|
|
|
|
|
"| `sleep 1\\|`\n\
|
|
|
|
|
| {} ± {}\n\
|
|
|
|
|
| {}…{}\n",
|
2019-03-29 07:59:23 +05:30
|
|
|
Unit::Second.format(result.mean),
|
|
|
|
|
Unit::Second.format(result.stddev),
|
|
|
|
|
Unit::Second.format(result.min),
|
|
|
|
|
Unit::Second.format(result.max)
|
|
|
|
|
)
|
|
|
|
|
.into_bytes();
|
|
|
|
|
let gens = table_row(&result, Unit::Second);
|
2019-01-29 18:02:19 -05:00
|
|
|
|
|
|
|
|
assert_eq!(exps, gens);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
|
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,
|
|
|
|
|
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
|
|
|
|
|
let exps: String = String::from(
|
|
|
|
|
"[cols=\"<,>,>\"]\n\
|
|
|
|
|
|===\n\
|
|
|
|
|
| Command | Mean [s] | Min…Max [s]\n\
|
|
|
|
|
\n\
|
2020-09-14 20:28:02 -07:00
|
|
|
| `FOO=1 BAR=2 command \\| 1`\n\
|
2019-01-29 18:02:19 -05:00
|
|
|
| 1.000 ± 2.000\n\
|
|
|
|
|
| 5.000…6.000\n\
|
|
|
|
|
\n\
|
2020-09-14 20:28:02 -07:00
|
|
|
| `FOO=1 BAR=7 command \\| 2`\n\
|
2019-01-29 18:02:19 -05:00
|
|
|
| 11.000 ± 12.000\n\
|
|
|
|
|
| 15.000…16.000\n\
|
|
|
|
|
|===\n\
|
2019-03-29 07:59:23 +05:30
|
|
|
",
|
|
|
|
|
);
|
|
|
|
|
let gens =
|
|
|
|
|
String::from_utf8(exporter.serialize(&results, Some(Unit::Second)).unwrap()).unwrap();
|
2019-01-29 18:02:19 -05:00
|
|
|
|
|
|
|
|
assert_eq!(exps, gens);
|
|
|
|
|
}
|