Do not use value_in for CSV export

This commit is contained in:
David Peter authored and David Peter committed 2026-10-05 16:00:08 +02:00
1 parent ebf127338c
commit 27ad0fb65e
2 files changed
+17 -13

No files matched your search

+7 -5
View File
@@ -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
"#);
}
+10 -8
View File
@@ -34,15 +34,11 @@ pub trait TimeQuantity {
fn suitable_unit(&self) -> TimeUnit;
fn format(&self, u: TimeUnit, precision: Option<usize>) -> String;
fn format_auto(&self, time_unit: Option<TimeUnit>) -> String;
fn format_value_auto(&self, time_unit: Option<TimeUnit>) -> String;
}
fn format_value_in(t: Time, u: TimeUnit, precision: Option<usize>) -> 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<U>(&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<usize>) -> 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<TimeUnit>) -> 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<TimeUnit>) -> String {
let time_unit = time_unit.unwrap_or_else(|| self.suitable_unit());
format_value_in(*self, time_unit, None)
self.format(time_unit, None)
}
}