Do not use value_in for CSV export
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent ebf1273 · Browse files · 2 files·+17 added−13 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+17 added−13 removed
- other:+17 added−13 removed
Provenance
- Commit
27ad0fb- Tree
73784f1- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
David Peter <mail@david-peter.de>- Parent
ebf1273
2 files changed+17 added−13 removed
modified:src/export/csv.rs+7 added-5 removed
3 unmodified lines4 5 use super::Exporter;6 use crate::benchmark::benchmark_result::BenchmarkResult;7 use crate::options::SortOrder;8-use crate::quantity::{second, TimeQuantity, TimeUnit};8+use crate::quantity::{TimeQuantity, TimeUnit};9 10 use anyhow::Result;11 12 #[derive(Default)]38 unmodified lines51 res.measurements.time_system_mean(),52 res.measurements.min(),53 res.measurements.max(),54 ] {55- fields.push(Cow::Owned(f.value_in(second).to_string().into_bytes()))55+ fields.push(Cow::Owned(56+ f.format(CSV_UNIT, Some(CSV_PRECISION)).into_bytes(),57+ ))58 }59 for v in res.parameters.values() {60 fields.push(Cow::Borrowed(v.value.as_bytes()))61 }7 unmodified lines69 #[test]70 fn test_csv() {71 use crate::benchmark::benchmark_result::Parameter;72 use crate::benchmark::measurement::{Measurement, Measurements};71- use crate::quantity::{byte, Information, Time, TimeQuantity};73+ use crate::quantity::{byte, second, Information, Time, TimeQuantity};74 75 use std::collections::BTreeMap;76 use std::process::ExitStatus;77 98 unmodified lines176 .unwrap();177 178 insta::assert_snapshot!(actual, @r#"179 command,mean,stddev,median,user,system,min,max,parameter_bar,parameter_foo178- command_a,9,2.6457513110645907,8,9,0,7,12,two,one179- command_b,18,1,18,18,0,17,19,seven,one180+ command_a,9.000000,2.645751,8.000000,9.000000,0.000000,7.000000,12.000000,two,one181+ command_b,18.000000,1.000000,18.000000,18.000000,0.000000,17.000000,19.000000,seven,one182 "#);183 }modified:src/quantity/mod.rs+10 added-8 removed
32 unmodified lines33 U: si::time::Unit + Conversion<f64, T = f64>;34 35 fn suitable_unit(&self) -> TimeUnit;36 37+ fn format(&self, u: TimeUnit, precision: Option<usize>) -> String;38 fn format_auto(&self, time_unit: Option<TimeUnit>) -> String;39 fn format_value_auto(&self, time_unit: Option<TimeUnit>) -> String;40 }41 41-fn format_value_in(t: Time, u: TimeUnit, precision: Option<usize>) -> String {42- let precision = precision.unwrap_or_else(|| if u == TimeUnit::Second { 3 } else { 1 });43- u.format(t, precision)44-}45-42 impl TimeQuantity for Time {43 fn value_in<U>(&self, _u: U) -> f6444 where45 U: si::time::Unit + Conversion<f64, T = f64>,10 unmodified lines56 TimeUnit::Second57 }58 }59 60+ /// Format the given time duration as a string.61+ fn format(&self, u: TimeUnit, precision: Option<usize>) -> String {62+ let precision = precision.unwrap_or_else(|| if u == TimeUnit::Second { 3 } else { 1 });63+ u.format(*self, precision)64+ }65+64- /// Format the given duration as a string. The output-unit can be enforced by setting `unit` to66+ /// Format the given time duration as a string. The output-unit can be enforced by setting `unit` to67 /// `Some(target_unit)`. If `unit` is `None`, it will be determined automatically.68 fn format_auto(&self, time_unit: Option<TimeUnit>) -> String {69 let time_unit = time_unit.unwrap_or_else(|| self.suitable_unit());70 69- let value = format_value_in(*self, time_unit, None);71+ let value = self.format(time_unit, None);72 73 format!("{} {}", value, time_unit.short_name())74 }75 76 /// Like `format_duration`, but returns the target unit as well.77 fn format_value_auto(&self, time_unit: Option<TimeUnit>) -> String {78 let time_unit = time_unit.unwrap_or_else(|| self.suitable_unit());79 78- format_value_in(*self, time_unit, None)80+ self.format(time_unit, None)81 }82 }83 84 pub const fn const_time_from_seconds(value: f64) -> Time {157 unmodified lines