Addressed review feedback
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jason Peacock · committed by David Peter · parent 29804c5 · Browse files · 2 files·+19 added−67 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+19 added−67 removed
- other:+19 added−67 removed
Provenance
- Commit
0b18446- Tree
f61d20e- Author
Jason Peacock <jason@jasonpeacock.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
29804c5
2 files changed+19 added−67 removed
modified:src/hyperfine/export/markdown.rs+13 added-29 removed
3 unmodified lines4 use hyperfine::types::BenchmarkResult;5 6 use std::io::Result;7 8-macro_rules! TABLE_HEADER {9- ($unit:expr) => {10- format!("| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n", unit=$unit) };11-}12-8 #[derive(Default)]9 pub struct MarkdownExporter {}10 11 impl Exporter for MarkdownExporter {12 fn serialize(&self, results: &Vec<BenchmarkResult>) -> Result<Vec<u8>> {18- // Default to `Second`.19- let mut unit = Unit::Second;20-21- if !results.is_empty() {13+ let unit = if let Some(first_result) = results.first() {14 // Use the first BenchmarkResult entry to determine the unit for all entries.23- unit = format_duration_value(results[0].mean, None).1;24- }15+ format_duration_value(first_result.mean, None).116+ } else {17+ // Default to `Second`.18+ Unit::Second19+ };20 21 let mut destination = start_table(unit);22 23 for result in results {3 unmodified lines27 Ok(destination)28 }29 }30 31+fn table_header(unit_short_name: String) -> String {32+ format!("| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n", unit=unit_short_name)33+}34+35 fn start_table(unit: Unit) -> Vec<u8> {37- TABLE_HEADER!(unit.short_name()).bytes().collect()36+ table_header(unit.short_name()).bytes().collect()37 }38 39 fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResult, unit: Unit) {40 let mean_str = format_duration_value(entry.mean, Some(unit)).0;52 unmodified lines93 let formatted_expected = format!(94 "{}\95 | `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 |96 | `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 |98-", TABLE_HEADER!("ms"));97+", table_header("ms".to_string()));98 99 assert_eq!(formatted_expected, formatted);100 }101 32 unmodified lines134 let formatted_expected = format!(135 "{}\136 | `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 |137 | `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 |139-", TABLE_HEADER!("s"));138+", table_header("s".to_string()));139 140 assert_eq!(formatted_expected, formatted);141 }143-144-/// An empty list of benchmark results will only include the table header145-/// in the markdown output, using the default `Seconds` unit.146-#[test]147-fn test_markdown_format_empty_results() {148- let exporter = MarkdownExporter::default();149-150- let timing_results = vec![];151-152- let formatted = String::from_utf8(exporter.serialize(&timing_results).unwrap()).unwrap();153-154- let formatted_expected = TABLE_HEADER!("s");155-156- assert_eq!(formatted_expected, formatted);157-}modified:src/hyperfine/format.rs+6 added-38 removed
13 unmodified lines14 Unit::MilliSecond => String::from("ms"),15 }16 }17 18- /// The multiplier value to convert from `from_unit` to the Unit.19- pub fn multiplier_from(&self, from_unit: Unit) -> f64 {20- match (*self, from_unit) {21- (Unit::Second, Unit::Second) => 1.0,22- (Unit::MilliSecond, Unit::MilliSecond) => 1.0,23- (Unit::Second, Unit::MilliSecond) => 1e-3,24- (Unit::MilliSecond, Unit::Second) => 1e3,25- }26- }27-18 /// Returns the Second value formatted for the Unit.19 pub fn format(&self, value: Second) -> String {20 match *self {31- Unit::Second => format!("{:.3}", value * self.multiplier_from(Unit::Second)),32- Unit::MilliSecond => format!("{:.1}", value * self.multiplier_from(Unit::Second)),21+ Unit::Second => format!("{:.3}", value),22+ Unit::MilliSecond => format!("{:.1}", value * 1e3),23 }24 }25 }26 13 unmodified lines40 }41 42 /// Like `format_duration`, but returns the target unit as well.43 pub fn format_duration_value(duration: Second, unit: Option<Unit>) -> (String, Unit) {54-55- // Default to `Second` until proven otherwise.56- let mut duration_unit = Unit::Second;57-58- match unit {59- Some(unit_option) => {60- // Use user-supplied unit.61- duration_unit = unit_option;62- },63- None => {64- if duration < 1.0 {65- // It's a small value, use `Millisecond` instead.66- duration_unit = Unit::MilliSecond;67- }68- },44+ if (duration < 1.0 && unit.is_none()) || unit == Some(Unit::MilliSecond) {45+ (Unit::MilliSecond.format(duration), Unit::MilliSecond)46+ } else {47+ (Unit::Second.format(duration), Unit::Second)48 }70-71- (duration_unit.format(duration), duration_unit)49 }50 51 #[test]52 fn test_unit_short_name() {53 assert_eq!("s", Unit::Second.short_name());54 assert_eq!("ms", Unit::MilliSecond.short_name());55 }56 80-#[test]81-fn test_unit_multiplier_from() {82- assert_eq!(1.0, Unit::Second.multiplier_from(Unit::Second));83- assert_eq!(1.0, Unit::MilliSecond.multiplier_from(Unit::MilliSecond));84-85- assert_eq!(0.001, Unit::Second.multiplier_from(Unit::MilliSecond));86- assert_eq!(1000.0, Unit::MilliSecond.multiplier_from(Unit::Second));87-}88-57 // Note - the values are rounded when formatted.58 #[test]59 fn test_unit_format() {60 let value: Second = 123.456789;44 unmodified lines