Skip to content
Myr

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

2 files+19 added−67 removed

modified:src/hyperfine/export/markdown.rs+13 added-29 removed
3 unmodified lines
4use hyperfine::types::BenchmarkResult;56use std::io::Result;78macro_rules! TABLE_HEADER {9 ($unit:expr) => {10 format!("| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n", unit=$unit) };11}128#[derive(Default)]9pub struct MarkdownExporter {}1011impl Exporter for MarkdownExporter {12 fn serialize(&self, results: &Vec<BenchmarkResult>) -> Result<Vec<u8>> {18 // Default to `Second`.19 let mut unit = Unit::Second;2021 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 };2021 let mut destination = start_table(unit);2223 for result in results {
3 unmodified lines
27 Ok(destination)28 }29}3031fn table_header(unit_short_name: String) -> String {32 format!("| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n", unit=unit_short_name)33}3435fn start_table(unit: Unit) -> Vec<u8> {37 TABLE_HEADER!(unit.short_name()).bytes().collect()36 table_header(unit.short_name()).bytes().collect()37}3839fn 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 lines
93 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()));9899 assert_eq!(formatted_expected, formatted);100}101
32 unmodified lines
134 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()));139140 assert_eq!(formatted_expected, formatted);141}143144/// An empty list of benchmark results will only include the table header145/// in the markdown output, using the default `Seconds` unit.146#[test]147fn test_markdown_format_empty_results() {148 let exporter = MarkdownExporter::default();149150 let timing_results = vec![];151152 let formatted = String::from_utf8(exporter.serialize(&timing_results).unwrap()).unwrap();153154 let formatted_expected = TABLE_HEADER!("s");155156 assert_eq!(formatted_expected, formatted);157}
modified:src/hyperfine/format.rs+6 added-38 removed
13 unmodified lines
14 Unit::MilliSecond => String::from("ms"),15 }16 }1718 /// 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 }2718 /// 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 lines
40}4142/// Like `format_duration`, but returns the target unit as well.43pub fn format_duration_value(duration: Second, unit: Option<Unit>) -> (String, Unit) {5455 // Default to `Second` until proven otherwise.56 let mut duration_unit = Unit::Second;5758 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 }7071 (duration_unit.format(duration), duration_unit)49}5051#[test]52fn test_unit_short_name() {53 assert_eq!("s", Unit::Second.short_name());54 assert_eq!("ms", Unit::MilliSecond.short_name());55}5680#[test]81fn 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));8485 assert_eq!(0.001, Unit::Second.multiplier_from(Unit::MilliSecond));86 assert_eq!(1000.0, Unit::MilliSecond.multiplier_from(Unit::Second));87}8857// Note - the values are rounded when formatted.58#[test]59fn test_unit_format() {60 let value: Second = 123.456789;
44 unmodified lines