2018-03-22 21:53:36 -04:00
|
|
|
use super::Exporter;
|
2018-09-09 08:44:13 -07:00
|
|
|
|
2018-10-29 18:44:44 -07:00
|
|
|
use hyperfine::format::format_duration_value;
|
2018-11-01 20:52:25 -07:00
|
|
|
use hyperfine::types::BenchmarkResult;
|
|
|
|
|
use hyperfine::units::Unit;
|
2018-03-20 17:00:24 -04:00
|
|
|
|
|
|
|
|
use std::io::Result;
|
|
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct MarkdownExporter {}
|
|
|
|
|
|
|
|
|
|
impl Exporter for MarkdownExporter {
|
2018-10-29 18:44:44 -07:00
|
|
|
fn serialize(&self, results: &Vec<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
|
|
|
|
|
|
|
|
let mut destination = start_table(unit);
|
2018-03-20 17:00:24 -04:00
|
|
|
|
|
|
|
|
for result in results {
|
2018-09-09 08:44:13 -07:00
|
|
|
add_table_row(&mut destination, result, unit);
|
2018-03-20 17:00:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(destination)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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!(
|
|
|
|
|
"| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n",
|
|
|
|
|
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
|
|
|
|
|
|
|
|
fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResult, unit: Unit) {
|
|
|
|
|
let mean_str = format_duration_value(entry.mean, Some(unit)).0;
|
|
|
|
|
let stddev_str = format_duration_value(entry.stddev, Some(unit)).0;
|
|
|
|
|
let min_str = format_duration_value(entry.min, Some(unit)).0;
|
|
|
|
|
let max_str = format_duration_value(entry.max, Some(unit)).0;
|
|
|
|
|
|
2018-03-20 17:00:24 -04:00
|
|
|
dest.extend(
|
|
|
|
|
format!(
|
2018-09-09 08:44:13 -07:00
|
|
|
"| `{command}` | {mean} ± {stddev} | {min}…{max} |\n",
|
2018-11-21 19:55:58 +01:00
|
|
|
command = entry.command.replace("|", "\\|"),
|
|
|
|
|
mean = mean_str,
|
|
|
|
|
stddev = stddev_str,
|
|
|
|
|
min = min_str,
|
|
|
|
|
max = max_str,
|
|
|
|
|
)
|
|
|
|
|
.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() {
|
|
|
|
|
let exporter = MarkdownExporter::default();
|
|
|
|
|
|
|
|
|
|
let mut timing_results = vec![];
|
|
|
|
|
|
|
|
|
|
timing_results.push(BenchmarkResult::new(
|
2018-11-21 19:55:58 +01:00
|
|
|
String::from("sleep 0.1"),
|
|
|
|
|
0.1057, // mean
|
|
|
|
|
0.0016, // std dev
|
|
|
|
|
0.0009, // user_mean
|
|
|
|
|
0.0011, // system_mean
|
|
|
|
|
0.1023, // min
|
|
|
|
|
0.1080, // max
|
|
|
|
|
vec![0.1, 0.1, 0.1], // times
|
2019-01-16 12:42:15 +01:00
|
|
|
None, // parameter
|
2018-11-21 19:55:58 +01:00
|
|
|
));
|
2018-09-09 08:44:13 -07:00
|
|
|
|
|
|
|
|
timing_results.push(BenchmarkResult::new(
|
2018-11-21 19:55:58 +01:00
|
|
|
String::from("sleep 2"),
|
|
|
|
|
2.0050, // mean
|
|
|
|
|
0.0020, // std dev
|
|
|
|
|
0.0009, // user_mean
|
|
|
|
|
0.0012, // system_mean
|
|
|
|
|
2.0020, // min
|
|
|
|
|
2.0080, // max
|
|
|
|
|
vec![2.0, 2.0, 2.0], // times
|
2019-01-16 12:42:15 +01:00
|
|
|
None, // parameter
|
2018-11-21 19:55:58 +01: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
|
|
|
"{}\
|
2018-09-09 08:44:13 -07:00
|
|
|
| `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 |
|
|
|
|
|
| `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 |
|
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() {
|
|
|
|
|
let exporter = MarkdownExporter::default();
|
|
|
|
|
|
|
|
|
|
let mut timing_results = vec![];
|
|
|
|
|
|
|
|
|
|
timing_results.push(BenchmarkResult::new(
|
2018-11-21 19:55:58 +01:00
|
|
|
String::from("sleep 2"),
|
|
|
|
|
2.0050, // mean
|
|
|
|
|
0.0020, // std dev
|
|
|
|
|
0.0009, // user_mean
|
|
|
|
|
0.0012, // system_mean
|
|
|
|
|
2.0020, // min
|
|
|
|
|
2.0080, // max
|
|
|
|
|
vec![2.0, 2.0, 2.0], // times
|
2019-01-16 12:42:15 +01:00
|
|
|
None, // parameter
|
2018-11-21 19:55:58 +01:00
|
|
|
));
|
2018-09-09 08:44:13 -07:00
|
|
|
|
|
|
|
|
timing_results.push(BenchmarkResult::new(
|
2018-11-21 19:55:58 +01:00
|
|
|
String::from("sleep 0.1"),
|
|
|
|
|
0.1057, // mean
|
|
|
|
|
0.0016, // std dev
|
|
|
|
|
0.0009, // user_mean
|
|
|
|
|
0.0011, // system_mean
|
|
|
|
|
0.1023, // min
|
|
|
|
|
0.1080, // max
|
|
|
|
|
vec![0.1, 0.1, 0.1], // times
|
2019-01-16 12:42:15 +01:00
|
|
|
None, // parameter
|
2018-11-21 19:55:58 +01: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
|
|
|
"{}\
|
2018-09-09 08:44:13 -07:00
|
|
|
| `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 |
|
|
|
|
|
| `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 |
|
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() {
|
|
|
|
|
let exporter = MarkdownExporter::default();
|
|
|
|
|
|
|
|
|
|
let mut timing_results = vec![];
|
|
|
|
|
|
|
|
|
|
timing_results.push(BenchmarkResult::new(
|
2018-11-21 19:55:58 +01:00
|
|
|
String::from("sleep 0.1"),
|
|
|
|
|
0.1057, // mean
|
|
|
|
|
0.0016, // std dev
|
|
|
|
|
0.0009, // user_mean
|
|
|
|
|
0.0011, // system_mean
|
|
|
|
|
0.1023, // min
|
|
|
|
|
0.1080, // max
|
|
|
|
|
vec![0.1, 0.1, 0.1], // times
|
2019-01-16 12:42:15 +01:00
|
|
|
None, // parameter
|
2018-11-21 19:55:58 +01:00
|
|
|
));
|
2018-10-29 18:44:44 -07:00
|
|
|
|
|
|
|
|
timing_results.push(BenchmarkResult::new(
|
2018-11-21 19:55:58 +01:00
|
|
|
String::from("sleep 2"),
|
|
|
|
|
2.0050, // mean
|
|
|
|
|
0.0020, // std dev
|
|
|
|
|
0.0009, // user_mean
|
|
|
|
|
0.0012, // system_mean
|
|
|
|
|
2.0020, // min
|
|
|
|
|
2.0080, // max
|
|
|
|
|
vec![2.0, 2.0, 2.0], // times
|
2019-01-16 12:42:15 +01:00
|
|
|
None, // parameter
|
2018-11-21 19:55:58 +01: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
|
|
|
"{}\
|
2018-10-29 18:44:44 -07:00
|
|
|
| `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 |
|
|
|
|
|
| `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 |
|
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() {
|
|
|
|
|
let exporter = MarkdownExporter::default();
|
|
|
|
|
|
|
|
|
|
let mut timing_results = vec![];
|
|
|
|
|
|
|
|
|
|
timing_results.push(BenchmarkResult::new(
|
2018-11-21 19:55:58 +01:00
|
|
|
String::from("sleep 2"),
|
|
|
|
|
2.0050, // mean
|
|
|
|
|
0.0020, // std dev
|
|
|
|
|
0.0009, // user_mean
|
|
|
|
|
0.0012, // system_mean
|
|
|
|
|
2.0020, // min
|
|
|
|
|
2.0080, // max
|
|
|
|
|
vec![2.0, 2.0, 2.0], // times
|
2019-01-16 12:42:15 +01:00
|
|
|
None, // parameter
|
2018-11-21 19:55:58 +01:00
|
|
|
));
|
2018-10-29 18:44:44 -07:00
|
|
|
|
|
|
|
|
timing_results.push(BenchmarkResult::new(
|
2018-11-21 19:55:58 +01:00
|
|
|
String::from("sleep 0.1"),
|
|
|
|
|
0.1057, // mean
|
|
|
|
|
0.0016, // std dev
|
|
|
|
|
0.0009, // user_mean
|
|
|
|
|
0.0011, // system_mean
|
|
|
|
|
0.1023, // min
|
|
|
|
|
0.1080, // max
|
|
|
|
|
vec![0.1, 0.1, 0.1], // times
|
2019-01-16 12:42:15 +01:00
|
|
|
None, // parameter
|
2018-11-21 19:55:58 +01: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
|
|
|
"{}\
|
2018-10-29 18:44:44 -07:00
|
|
|
| `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 |
|
|
|
|
|
| `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 |
|
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);
|
|
|
|
|
}
|