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
Provenance
- Commit
14be4a0- Tree
93df850- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
e09707c
2 files changed+39 added−36 removed
modified:src/hyperfine/export/markdown.rs+24 added-18 removed
1 use super::Exporter;2 3 use crate::hyperfine::format::format_duration_value;4+use crate::hyperfine::internal::{compute_relative_speed, BenchmarkResultWithRelativeSpeed};5 use crate::hyperfine::types::BenchmarkResult;6 use crate::hyperfine::units::Unit;7 8 use std::io::Result;13 unmodified lines22 // Default to `Second`.23 Unit::Second24 };25 26+ let annotated_results = compute_relative_speed(&results);27+28 let mut destination = start_table(unit);29 27- 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 }33 34 Ok(destination)35 }36 }37 38 fn 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 }44 45 fn start_table(unit: Unit) -> Vec<u8> {46 table_header(unit.short_name()).bytes().collect()47 }48 46-fn 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;49+fn 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);56 57 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 lines107 let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();108 109 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 lines152 let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();153 154 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 lines201 .unwrap();202 203 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 lines251 .unwrap();252 253 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 linesmodified:src/hyperfine/internal.rs+15 added-18 removed
45 unmodified lines46 .unwrap()47 }48 49 pub 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 }54 55 fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {56 l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)14 unmodified lines71 72 // 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();77 78 BenchmarkResultWithRelativeSpeed {79 result,80 relative_speed: ratio,39 unmodified lines120 #[test]121 fn test_compute_relative_speed() {122 use approx::assert_relative_eq;123 125- 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 };135 136 let results = vec![137 create_result("cmd1", 3.0),10 unmodified lines