Files
hyperfine/src/hyperfine/export/markdown.rs
T

256 lines
7.6 KiB
Rust
Raw Normal View History

use super::Exporter;
use hyperfine::format::format_duration_value;
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 {
fn serialize(&self, results: &Vec<BenchmarkResult>, unit: Option<Unit>) -> Result<Vec<u8>> {
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.
2018-10-27 16:51:32 -07:00
format_duration_value(first_result.mean, None).1
} else {
// Default to `Second`.
Unit::Second
};
let mut destination = start_table(unit);
2018-03-20 17:00:24 -04:00
for result in results {
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
}
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
}
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!(
"| `{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
);
}
/// 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
/// the units for all entries when the time unit is not given.
#[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
None, // parameter
2018-11-21 19:55:58 +01: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
None, // parameter
2018-11-21 19:55:58 +01:00
));
let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
let formatted_expected = format!(
2018-11-21 19:55:58 +01: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())
);
assert_eq!(formatted_expected, formatted);
}
/// This (again) demonstrates that the first entry's units (s) are used to set
/// the units for all entries when the time unit is not given.
#[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
None, // parameter
2018-11-21 19:55:58 +01: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
None, // parameter
2018-11-21 19:55:58 +01:00
));
let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
let formatted_expected = format!(
2018-11-21 19:55:58 +01: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())
);
assert_eq!(formatted_expected, formatted);
}
/// 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
None, // parameter
2018-11-21 19:55:58 +01: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
None, // parameter
2018-11-21 19:55:58 +01:00
));
2018-11-21 19:55:58 +01:00
let formatted = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::Second))
.unwrap(),
)
.unwrap();
let formatted_expected = format!(
2018-11-21 19:55:58 +01: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())
);
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
None, // parameter
2018-11-21 19:55:58 +01: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
None, // parameter
2018-11-21 19:55:58 +01:00
));
2018-11-21 19:55:58 +01:00
let formatted = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::MilliSecond))
.unwrap(),
)
.unwrap();
let formatted_expected = format!(
2018-11-21 19:55:58 +01: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())
);
assert_eq!(formatted_expected, formatted);
}