diff --git a/src/export/csv.rs b/src/export/csv.rs index bbb55ff..df94758 100644 --- a/src/export/csv.rs +++ b/src/export/csv.rs @@ -5,7 +5,7 @@ use csv::WriterBuilder; use super::Exporter; use crate::benchmark::benchmark_result::BenchmarkResult; use crate::options::SortOrder; -use crate::quantity::{second, TimeQuantity, TimeUnit}; +use crate::quantity::{TimeQuantity, TimeUnit}; use anyhow::Result; @@ -52,7 +52,9 @@ impl Exporter for CsvExporter { res.measurements.min(), res.measurements.max(), ] { - fields.push(Cow::Owned(f.value_in(second).to_string().into_bytes())) + fields.push(Cow::Owned( + f.format(CSV_UNIT, Some(CSV_PRECISION)).into_bytes(), + )) } for v in res.parameters.values() { fields.push(Cow::Borrowed(v.value.as_bytes())) @@ -68,7 +70,7 @@ impl Exporter for CsvExporter { fn test_csv() { use crate::benchmark::benchmark_result::Parameter; use crate::benchmark::measurement::{Measurement, Measurements}; - use crate::quantity::{byte, Information, Time, TimeQuantity}; + use crate::quantity::{byte, second, Information, Time, TimeQuantity}; use std::collections::BTreeMap; use std::process::ExitStatus; @@ -175,7 +177,7 @@ fn test_csv() { insta::assert_snapshot!(actual, @r#" command,mean,stddev,median,user,system,min,max,parameter_bar,parameter_foo - command_a,9,2.6457513110645907,8,9,0,7,12,two,one - command_b,18,1,18,18,0,17,19,seven,one + command_a,9.000000,2.645751,8.000000,9.000000,0.000000,7.000000,12.000000,two,one + command_b,18.000000,1.000000,18.000000,18.000000,0.000000,17.000000,19.000000,seven,one "#); } diff --git a/src/quantity/mod.rs b/src/quantity/mod.rs index ad3e236..b8e3f87 100644 --- a/src/quantity/mod.rs +++ b/src/quantity/mod.rs @@ -34,15 +34,11 @@ pub trait TimeQuantity { fn suitable_unit(&self) -> TimeUnit; + fn format(&self, u: TimeUnit, precision: Option) -> String; fn format_auto(&self, time_unit: Option) -> String; fn format_value_auto(&self, time_unit: Option) -> String; } -fn format_value_in(t: Time, u: TimeUnit, precision: Option) -> String { - let precision = precision.unwrap_or_else(|| if u == TimeUnit::Second { 3 } else { 1 }); - u.format(t, precision) -} - impl TimeQuantity for Time { fn value_in(&self, _u: U) -> f64 where @@ -61,12 +57,18 @@ impl TimeQuantity for Time { } } - /// Format the given duration as a string. The output-unit can be enforced by setting `unit` to + /// Format the given time duration as a string. + fn format(&self, u: TimeUnit, precision: Option) -> String { + let precision = precision.unwrap_or_else(|| if u == TimeUnit::Second { 3 } else { 1 }); + u.format(*self, precision) + } + + /// Format the given time duration as a string. The output-unit can be enforced by setting `unit` to /// `Some(target_unit)`. If `unit` is `None`, it will be determined automatically. fn format_auto(&self, time_unit: Option) -> String { let time_unit = time_unit.unwrap_or_else(|| self.suitable_unit()); - let value = format_value_in(*self, time_unit, None); + let value = self.format(time_unit, None); format!("{} {}", value, time_unit.short_name()) } @@ -75,7 +77,7 @@ impl TimeQuantity for Time { fn format_value_auto(&self, time_unit: Option) -> String { let time_unit = time_unit.unwrap_or_else(|| self.suitable_unit()); - format_value_in(*self, time_unit, None) + self.format(time_unit, None) } }