Skip to content
Myr

Refactor Markdown table contents generation to generic Markup function

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

authored by Philipp Paulweber · committed by David Peter · parent 23d9fe2 · Browse files · 2 files+50 added−42 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+50 added−42 removed

  • other:+50 added−42 removed

2 files+50 added−42 removed

modified:src/export/markdown.rs+3 added-42 removed
3 unmodified lines
4use crate::export::markup::markup_results_unit;5use crate::export::markup::markup_table_data;6use crate::export::markup::markup_table_line;7use crate::export::markup::MarkupType;8use crate::output::format::format_duration_value;8use crate::export::markup::markup_results;9use crate::util::units::Unit;1011use anyhow::{anyhow, Result};12
10 unmodified lines
23 "Relative speed comparison is not available for Markdown export."24 ));25 }2627 // prepare table header strings28 let notation = format!("[{}]", unit.short_name());29 let mut data: Vec<Vec<_>> = vec![vec![30 format!("Command"),31 format!("Mean {}", notation),32 format!("Min {}", notation),33 format!("Max {}", notation),34 format!("Relative"),35 ]];3637 for entry in entries.unwrap() {38 let measurement = &entry.result;39 // prepare data row strings40 let cmd_str = measurement.command.replace("|", "\\|");41 let mean_str = format_duration_value(measurement.mean, Some(unit)).0;42 let stddev_str = if let Some(stddev) = measurement.stddev {43 format!(" ± {}", format_duration_value(stddev, Some(unit)).0)44 } else {45 "".into()46 };47 let min_str = format_duration_value(measurement.min, Some(unit)).0;48 let max_str = format_duration_value(measurement.max, Some(unit)).0;49 let rel_str = format!("{:.2}", entry.relative_speed);50 let rel_stddev_str = if entry.is_fastest {51 "".into()52 } else if let Some(stddev) = entry.relative_speed_stddev {53 format!(" ± {:.2}", stddev)54 } else {55 "".into()56 };57 // prepare table row entries58 data.push(vec![59 format!("`{}`", cmd_str),60 format!("{}{}", mean_str, stddev_str),61 format!("{}", min_str),62 format!("{}", max_str),63 format!("{}{}", rel_str, rel_stddev_str),64 ])65 }6627 let kind = MarkupType::Markdown;28 let data = markup_results( &kind, &entries.unwrap(), unit );29 let head: &Vec<String> = data.first().unwrap();30 let tail: &[Vec<String>] = &data[1..];69 let kind = MarkupType::Markdown;3132 // emit header33 let mut table = markup_table_data(&kind, head);34
230 unmodified lines
modified:src/export/markup.rs+47 added
1use crate::benchmark::benchmark_result::BenchmarkResult;2use crate::output::format::format_duration_value;3use crate::util::units::Unit;4use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;56#[derive(Clone)]7pub enum MarkupType {8 /// Markdown table
11 unmodified lines
20 MarkupType::Markdown => format!("|:---|{}\n", "---:|".repeat(size - 1)),21 };22}2324pub fn markup_results(kind: &MarkupType, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> Vec<Vec<String>> {25 // prepare table header strings26 let notation = format!("[{}]", unit.short_name());27 let mut data: Vec<Vec<_>> = vec![vec![28 format!("Command"),29 format!("Mean {}", notation),30 format!("Min {}", notation),31 format!("Max {}", notation),32 format!("Relative"),33 ]];3435 for entry in entries {36 let measurement = &entry.result;37 // prepare data row strings38 let cmd_str = measurement.command.replace("|", "\\|");39 let mean_str = format_duration_value(measurement.mean, Some(unit)).0;40 let stddev_str = if let Some(stddev) = measurement.stddev {41 format!(" ± {}", format_duration_value(stddev, Some(unit)).0)42 } else {43 "".into()44 };45 let min_str = format_duration_value(measurement.min, Some(unit)).0;46 let max_str = format_duration_value(measurement.max, Some(unit)).0;47 let rel_str = format!("{:.2}", entry.relative_speed);48 let rel_stddev_str = if entry.is_fastest {49 "".into()50 } else if let Some(stddev) = entry.relative_speed_stddev {51 format!(" ± {:.2}", stddev)52 } else {53 "".into()54 };55 // prepare table row entries56 data.push(vec![57 match kind {58 MarkupType::Markdown => format!( "`{}`", cmd_str ),59 },60 format!("{}{}", mean_str, stddev_str),61 format!("{}", min_str),62 format!("{}", max_str),63 format!("{}{}", rel_str, rel_stddev_str),64 ])65 }6667 return data;68}6970pub fn markup_results_unit(results: &[BenchmarkResult], unit: Option<Unit>) -> Unit {71 return if let Some(unit) = unit {72 // Use the given unit for all entries.73 unit
204 unmodified lines