Add relative speed to markdown export

This commit is contained in:
sharkdp 2019-06-08 12:04:26 +02:00 committed by David Peter
parent e09707c90f
commit 14be4a0939
2 changed files with 39 additions and 36 deletions

View File

@ -1,6 +1,7 @@
use super::Exporter;
use crate::hyperfine::format::format_duration_value;
use crate::hyperfine::internal::{compute_relative_speed, BenchmarkResultWithRelativeSpeed};
use crate::hyperfine::types::BenchmarkResult;
use crate::hyperfine::units::Unit;
@ -22,10 +23,12 @@ impl Exporter for MarkdownExporter {
Unit::Second
};
let annotated_results = compute_relative_speed(&results);
let mut destination = start_table(unit);
for result in results {
add_table_row(&mut destination, result, unit);
for result in annotated_results {
add_table_row(&mut destination, &result, unit);
}
Ok(destination)
@ -34,7 +37,7 @@ impl Exporter for MarkdownExporter {
fn table_header(unit_short_name: String) -> String {
format!(
"| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n",
"| Command | Mean [{unit}] | Min…Max [{unit}] | Relative |\n|:---|---:|---:|---:|\n",
unit = unit_short_name
)
}
@ -43,20 +46,23 @@ fn start_table(unit: Unit) -> Vec<u8> {
table_header(unit.short_name()).bytes().collect()
}
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;
fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResultWithRelativeSpeed, unit: Unit) {
let result = &entry.result;
let mean_str = format_duration_value(result.mean, Some(unit)).0;
let stddev_str = format_duration_value(result.stddev, Some(unit)).0;
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!("{:.1}", entry.relative_speed);
dest.extend(
format!(
"| `{command}` | {mean} ± {stddev} | {min}…{max} |\n",
command = entry.command.replace("|", "\\|"),
"| `{command}` | {mean} ± {stddev} | {min}…{max} | {rel} |\n",
command = result.command.replace("|", "\\|"),
mean = mean_str,
stddev = stddev_str,
min = min_str,
max = max_str,
rel = rel_str,
)
.as_bytes(),
);
@ -102,8 +108,8 @@ fn test_markdown_format_ms() {
let formatted_expected = format!(
"{}\
| `sleep 0.1` | 105.7 ± 1.6 | 102.3108.0 |
| `sleep 2` | 2005.0 ± 2.0 | 2002.02008.0 |
| `sleep 0.1` | 105.7 ± 1.6 | 102.3108.0 | 1.0 |
| `sleep 2` | 2005.0 ± 2.0 | 2002.02008.0 | 19.0 |
",
table_header("ms".to_string())
);
@ -147,8 +153,8 @@ fn test_markdown_format_s() {
let formatted_expected = format!(
"{}\
| `sleep 2` | 2.005 ± 0.002 | 2.0022.008 |
| `sleep 0.1` | 0.106 ± 0.002 | 0.1020.108 |
| `sleep 2` | 2.005 ± 0.002 | 2.0022.008 | 19.0 |
| `sleep 0.1` | 0.106 ± 0.002 | 0.1020.108 | 1.0 |
",
table_header("s".to_string())
);
@ -196,8 +202,8 @@ fn test_markdown_format_time_unit_s() {
let formatted_expected = format!(
"{}\
| `sleep 0.1` | 0.106 ± 0.002 | 0.1020.108 |
| `sleep 2` | 2.005 ± 0.002 | 2.0022.008 |
| `sleep 0.1` | 0.106 ± 0.002 | 0.1020.108 | 1.0 |
| `sleep 2` | 2.005 ± 0.002 | 2.0022.008 | 19.0 |
",
table_header("s".to_string())
);
@ -246,8 +252,8 @@ fn test_markdown_format_time_unit_ms() {
let formatted_expected = format!(
"{}\
| `sleep 2` | 2005.0 ± 2.0 | 2002.02008.0 |
| `sleep 0.1` | 105.7 ± 1.6 | 102.3108.0 |
| `sleep 2` | 2005.0 ± 2.0 | 2002.02008.0 | 19.0 |
| `sleep 0.1` | 105.7 ± 1.6 | 102.3108.0 | 1.0 |
",
table_header("ms".to_string())
);

View File

@ -47,9 +47,9 @@ pub fn min(vals: &[f64]) -> f64 {
}
pub struct BenchmarkResultWithRelativeSpeed<'a> {
result: &'a BenchmarkResult,
relative_speed: Scalar,
relative_speed_stddev: Scalar,
pub result: &'a BenchmarkResult,
pub relative_speed: Scalar,
pub relative_speed_stddev: Scalar,
}
fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {
@ -72,9 +72,8 @@ pub fn compute_relative_speed<'a>(
// https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas
// Covariance asssumed to be 0, i.e. variables are assumed to be independent
let ratio_stddev = ratio
* ((result.stddev / result.mean).powi(2)
+ (fastest.stddev / fastest.mean).powi(2))
.sqrt();
* ((result.stddev / result.mean).powi(2) + (fastest.stddev / fastest.mean).powi(2))
.sqrt();
BenchmarkResultWithRelativeSpeed {
result,
@ -122,18 +121,16 @@ fn test_max() {
fn test_compute_relative_speed() {
use approx::assert_relative_eq;
let create_result = |name: &str, mean| {
BenchmarkResult {
command: name.into(),
mean: mean,
stddev: 1.0,
user: mean,
system: 0.0,
min: mean,
max: mean,
times: None,
parameter: None,
}
let create_result = |name: &str, mean| BenchmarkResult {
command: name.into(),
mean: mean,
stddev: 1.0,
user: mean,
system: 0.0,
min: mean,
max: mean,
times: None,
parameter: None,
};
let results = vec![