Skip to content
Myr

Add relative speed to markdown export

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by sharkdp · committed by David Peter · parent e09707c · Browse files · 2 files+39 added−36 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+39 added−36 removed

  • other:+39 added−36 removed

2 files+39 added−36 removed

modified:src/hyperfine/export/markdown.rs+24 added-18 removed
1use super::Exporter;23use crate::hyperfine::format::format_duration_value;4use crate::hyperfine::internal::{compute_relative_speed, BenchmarkResultWithRelativeSpeed};5use crate::hyperfine::types::BenchmarkResult;6use crate::hyperfine::units::Unit;78use std::io::Result;
13 unmodified lines
22 // Default to `Second`.23 Unit::Second24 };2526 let annotated_results = compute_relative_speed(&results);2728 let mut destination = start_table(unit);2927 for result in results {28 add_table_row(&mut destination, result, unit);30 for result in annotated_results {31 add_table_row(&mut destination, &result, unit);32 }3334 Ok(destination)35 }36}3738fn table_header(unit_short_name: String) -> String {39 format!(37 "| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n",40 "| Command | Mean [{unit}] | Min…Max [{unit}] | Relative |\n|:---|---:|---:|---:|\n",41 unit = unit_short_name42 )43}4445fn start_table(unit: Unit) -> Vec<u8> {46 table_header(unit.short_name()).bytes().collect()47}4846fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResult, unit: Unit) {47 let mean_str = format_duration_value(entry.mean, Some(unit)).0;48 let stddev_str = format_duration_value(entry.stddev, Some(unit)).0;49 let min_str = format_duration_value(entry.min, Some(unit)).0;50 let max_str = format_duration_value(entry.max, Some(unit)).0;49fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResultWithRelativeSpeed, unit: Unit) {50 let result = &entry.result;51 let mean_str = format_duration_value(result.mean, Some(unit)).0;52 let stddev_str = format_duration_value(result.stddev, Some(unit)).0;53 let min_str = format_duration_value(result.min, Some(unit)).0;54 let max_str = format_duration_value(result.max, Some(unit)).0;55 let rel_str = format!("{:.1}", entry.relative_speed);5657 dest.extend(58 format!(54 "| `{command}` | {mean} ± {stddev} | {min}…{max} |\n",55 command = entry.command.replace("|", "\\|"),59 "| `{command}` | {mean} ± {stddev} | {min}…{max} | {rel} |\n",60 command = result.command.replace("|", "\\|"),61 mean = mean_str,62 stddev = stddev_str,63 min = min_str,64 max = max_str,65 rel = rel_str,66 )67 .as_bytes(),68 );69}
37 unmodified lines
107 let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();108109 let formatted_expected = format!(110 "{}\105| `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 |106| `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 |111| `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 | 1.0 |112| `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 | 19.0 |113",114 table_header("ms".to_string())115 );116
35 unmodified lines
152 let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();153154 let formatted_expected = format!(155 "{}\150| `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 |151| `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 |156| `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 | 19.0 |157| `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 | 1.0 |158",159 table_header("s".to_string())160 );161
39 unmodified lines
201 .unwrap();202203 let formatted_expected = format!(204 "{}\199| `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 |200| `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 |205| `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 | 1.0 |206| `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 | 19.0 |207",208 table_header("s".to_string())209 );210
40 unmodified lines
251 .unwrap();252253 let formatted_expected = format!(254 "{}\249| `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 |250| `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 |255| `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 | 19.0 |256| `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 | 1.0 |257",258 table_header("ms".to_string())259 );260
2 unmodified lines
modified:src/hyperfine/internal.rs+15 added-18 removed
45 unmodified lines
46 .unwrap()47}4849pub struct BenchmarkResultWithRelativeSpeed<'a> {50 result: &'a BenchmarkResult,51 relative_speed: Scalar,52 relative_speed_stddev: Scalar,50 pub result: &'a BenchmarkResult,51 pub relative_speed: Scalar,52 pub relative_speed_stddev: Scalar,53}5455fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {56 l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)
14 unmodified lines
7172 // https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas73 // Covariance asssumed to be 0, i.e. variables are assumed to be independent74 let ratio_stddev = ratio75 * ((result.stddev / result.mean).powi(2)76 + (fastest.stddev / fastest.mean).powi(2))77 .sqrt();75 * ((result.stddev / result.mean).powi(2) + (fastest.stddev / fastest.mean).powi(2))76 .sqrt();7778 BenchmarkResultWithRelativeSpeed {79 result,80 relative_speed: ratio,
39 unmodified lines
120#[test]121fn test_compute_relative_speed() {122 use approx::assert_relative_eq;123125 let create_result = |name: &str, mean| {126 BenchmarkResult {127 command: name.into(),128 mean: mean,129 stddev: 1.0,130 user: mean,131 system: 0.0,132 min: mean,133 max: mean,134 times: None,135 parameter: None,124 let create_result = |name: &str, mean| BenchmarkResult {125 command: name.into(),126 mean: mean,127 stddev: 1.0,128 user: mean,129 system: 0.0,130 min: mean,131 max: mean,132 times: None,133 parameter: None,136 }134 };135136 let results = vec![137 create_result("cmd1", 3.0),
10 unmodified lines