Skip to content
Myr

Refactor MarkupExporter to omit intermediate table result creation and simplify MarkupFormatter emitting

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

* see: https://github.com/sharkdp/hyperfine/pull/490#pullrequestreview-932424652

authored by Philipp Paulweber · committed by David Peter · parent 9dc87ac · Browse files · 2 files+27 added−48 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+27 added−48 removed

  • other:+27 added−48 removed

2 files+27 added−48 removed

modified:src/export/markdown.rs+1 added-7 removed
35 unmodified lines
36 ));37 }3839 let formatter = MarkdownFormatter::default();40 let data_vvs = formatter.results(&entries.unwrap(), unit);41 let data_vas: Vec<Vec<&str>> = data_vvs42 .iter()43 .map(|list| list.iter().map(AsRef::as_ref).collect::<Vec<&str>>())44 .collect();45 let data: Vec<&[&str]> = data_vas.iter().map(AsRef::as_ref).collect();46 let table = formatter.table(&data);40 let table = formatter.table_results(&entries.unwrap(), unit);41 Ok(table.as_bytes().to_vec())42 }43}44
238 unmodified lines
modified:src/export/markup.rs+26 added-41 removed
1 unmodified line
2use crate::output::format::format_duration_value;3use crate::util::units::Unit;45pub trait MarkupFormatter {6 fn table(&self, data: &[&[&str]]) -> String {7 let head: &[&str] = data.first().unwrap();8 let tail = &data[1..];6 fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {7 // prepare table header strings8 let notation = format!("[{}]", unit.short_name());9 let head: [&str; 5] = [10 "Command",11 &format!("Mean {}", notation),12 &format!("Min {}", notation),13 &format!("Max {}", notation),14 "Relative",15 ];1617 // emit header11 let table = &mut self.table_data(head);18 let mut table = self.table_data(&head);1920 // emit horizontal line21 table.push_str(&self.table_line(head.len()));2216 // emit data rows17 for row in tail {18 table.push_str(&self.table_data(row))19 }2021 table.to_string()22 }2324 fn table_data(&self, data: &[&str]) -> String;2526 fn table_line(&self, size: usize) -> String;2728 fn command(&self, size: &str) -> String;2930 fn results(31 &self,32 entries: &[BenchmarkResultWithRelativeSpeed],33 unit: Unit,34 ) -> Vec<Vec<String>> {35 // prepare table header strings36 let notation = format!("[{}]", unit.short_name());37 let mut data: Vec<Vec<_>> = vec![vec![38 "Command".to_string(),39 format!("Mean {}", notation),40 format!("Min {}", notation),41 format!("Max {}", notation),42 "Relative".to_string(),43 ]];4423 for entry in entries {24 let measurement = &entry.result;25 // prepare data row strings26 let cmd_str = measurement.command.replace('|', "\\|");
13 unmodified lines
40 } else {41 "".into()42 };43 // prepare table row entries66 data.push(vec![67 self.command(&cmd_str),68 format!("{}{}", mean_str, stddev_str),69 min_str,70 max_str,71 format!("{}{}", rel_str, rel_stddev_str),72 ])44 let data: [&str; 5] = [45 &self.command(&cmd_str),46 &format!("{}{}", mean_str, stddev_str),47 &min_str,48 &max_str,49 &format!("{}{}", rel_str, rel_stddev_str),50 ];51 table.push_str(&self.table_data(&data))52 }5375 data54 table55 }5657 fn table_data(&self, data: &[&str]) -> String;5859 fn table_line(&self, size: usize) -> String;6061 fn command(&self, size: &str) -> String;62}