From 418e43a652f1d31d639270c13d15656116ff189c Mon Sep 17 00:00:00 2001 From: David Peter Date: Mon, 5 Oct 2026 14:18:33 +0200 Subject: [PATCH] Implement TimeUnit functions via uom --- src/benchmark/measurement.rs | 2 +- src/cli.rs | 6 ++---- src/export/csv.rs | 6 +++--- src/export/json.rs | 4 ++-- src/export/markup.rs | 12 ++++++------ src/export/mod.rs | 5 ++--- src/export/tests.rs | 6 +++--- src/options.rs | 4 +--- 8 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/benchmark/measurement.rs b/src/benchmark/measurement.rs index d80e940..bab0182 100644 --- a/src/benchmark/measurement.rs +++ b/src/benchmark/measurement.rs @@ -138,7 +138,7 @@ impl Measurements { &self .wall_clock_times() .iter() - .map(|t| t.value_in::()) // TODO + .map(|t| t.value_in(second)) // TODO .collect::>(), ) } diff --git a/src/cli.rs b/src/cli.rs index 0a38675..9a4aeeb 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -291,15 +291,13 @@ fn build_command() -> Command { .short('u') .action(ArgAction::Set) .value_name("UNIT") - .value_parser(["µs", "us", "microsecond", "microseconds", "ms", "millisecond", "milliseconds", "s", "second", "seconds", "min", "minute", "minutes", "h", "hour", "hours"]) + .value_parser(["µs", "us", "microsecond", "microseconds", "ms", "millisecond", "milliseconds", "s", "second", "seconds"]) .help("Set the time unit to be used. If the option is not given, the time unit is determined automatically. \ This option affects the standard output as well as all export formats except for CSV and JSON.\n\ Possible values:\n \ * 'µs', 'us', 'microsecond', 'microseconds'\n \ * 'ms', 'millisecond', 'milliseconds'\n \ - * 's', 'second', 'seconds'\n \ - * 'min', 'minute', 'minutes'\n \ - * 'h', 'hour', 'hours'"), + * 's', 'second', 'seconds'"), ) .arg( Arg::new("export-asciidoc") diff --git a/src/export/csv.rs b/src/export/csv.rs index 8fb49c5..d5978db 100644 --- a/src/export/csv.rs +++ b/src/export/csv.rs @@ -6,7 +6,7 @@ use super::Exporter; use crate::benchmark::benchmark_result::BenchmarkResult; use crate::options::SortOrder; use crate::quantity::{second, TimeQuantity}; -use crate::util::units::Unit; +use crate::util::units::TimeUnit; use anyhow::Result; @@ -17,7 +17,7 @@ impl Exporter for CsvExporter { fn serialize( &self, results: &[BenchmarkResult], - _unit: Option, + _time_unit: Option, _sort_order: SortOrder, _reference_index: Option, ) -> Result> { @@ -53,7 +53,7 @@ impl Exporter for CsvExporter { res.measurements.min(), res.measurements.max(), ] { - fields.push(Cow::Owned(f.value_in::().to_string().into_bytes())) + fields.push(Cow::Owned(f.value_in(second).to_string().into_bytes())) } for v in res.parameters.values() { fields.push(Cow::Borrowed(v.value.as_bytes())) diff --git a/src/export/json.rs b/src/export/json.rs index c5b6a84..a3bf18b 100644 --- a/src/export/json.rs +++ b/src/export/json.rs @@ -4,7 +4,7 @@ use serde_json::to_vec_pretty; use super::Exporter; use crate::benchmark::benchmark_result::BenchmarkResult; use crate::options::SortOrder; -use crate::quantity::TimeUnit; +use crate::util::units::TimeUnit; use anyhow::Result; @@ -20,7 +20,7 @@ impl Exporter for JsonExporter { fn serialize( &self, results: &[BenchmarkResult], - _unit: Option, + _time_unit: Option, _sort_order: SortOrder, _reference_index: Option, ) -> Result> { diff --git a/src/export/markup.rs b/src/export/markup.rs index 2248767..576d3f1 100644 --- a/src/export/markup.rs +++ b/src/export/markup.rs @@ -16,12 +16,12 @@ pub trait MarkupExporter { fn table_results( &self, entries: &[BenchmarkResultWithRelativeSpeed], - unit: TimeUnit, + time_unit: TimeUnit, reference_pending: bool, explicit_reference: bool, ) -> String { // prepare table header strings - let notation = format!("[{}]", unit.short_name()); + let notation = format!("[{}]", time_unit.short_name()); // prepare table cells alignment let cells_alignment = [ @@ -51,14 +51,14 @@ pub trait MarkupExporter { let result = &entry.result; // prepare data row strings let cmd_str = result.command_with_unused_parameters().replace('|', "\\|"); - let mean_str = format_duration_value(result.mean_wall_clock_time(), Some(unit)).0; + let mean_str = format_duration_value(result.mean_wall_clock_time(), Some(time_unit)).0; let stddev_str = if let Some(stddev) = result.measurements.stddev() { - format!(" ± {}", format_duration_value(stddev, Some(unit)).0) + format!(" ± {}", format_duration_value(stddev, Some(time_unit)).0) } else { "".into() }; - let min_str = result.measurements.min().format_value(unit); - let max_str = result.measurements.max().format_value(unit); + let min_str = result.measurements.min().format_value(time_unit); + let max_str = result.measurements.max().format_value(time_unit); // The ratio of two zero times is undefined, even if they compare equal. let relative_unavailable = reference_pending || (explicit_reference diff --git a/src/export/mod.rs b/src/export/mod.rs index c8dd9a0..87cb681 100644 --- a/src/export/mod.rs +++ b/src/export/mod.rs @@ -18,8 +18,7 @@ use self::orgmode::OrgmodeExporter; use crate::benchmark::benchmark_result::BenchmarkResult; use crate::options::SortOrder; -use crate::output::console_writeln; -use crate::quantity::TimeUnit; +use crate::util::units::TimeUnit; use anyhow::{Context, Result}; use clap::ArgMatches; @@ -49,7 +48,7 @@ trait Exporter { fn serialize( &self, results: &[BenchmarkResult], - unit: Option, + time_unit: Option, sort_order: SortOrder, reference_index: Option, ) -> Result>; diff --git a/src/export/tests.rs b/src/export/tests.rs index 72358e8..e8fa096 100644 --- a/src/export/tests.rs +++ b/src/export/tests.rs @@ -4,7 +4,7 @@ use crate::benchmark::measurement::{Measurement, Measurements}; use crate::export::asciidoc::AsciidocExporter; use crate::export::orgmode::OrgmodeExporter; use crate::quantity::{Information, InformationQuantity, Time, TimeQuantity}; -use crate::util::units::Unit; +use crate::util::units::TimeUnit; use crate::{export::markdown::MarkdownExporter, options::SortOrder}; use std::collections::BTreeMap; use std::process::ExitStatus; @@ -379,14 +379,14 @@ fn test_markup_export_manual_s() { }, ]; - insta::assert_snapshot!(get_output::(&results, Some(Unit::Second), SortOrder::Command), @r#" + insta::assert_snapshot!(get_output::(&results, Some(TimeUnit::Second), SortOrder::Command), @r#" | Command | Mean [s] | Min [s] | Max [s] | Relative | |:---|---:|---:|---:|---:| | `sleep 2` | 2.020 ± 0.010 | 2.010 | 2.030 | 16.83 ± 1.41 | | `sleep 0.1` | 0.120 ± 0.010 | 0.110 | 0.130 | 1.00 | "#); - insta::assert_snapshot!(get_output::(&results, Some(Unit::Second), SortOrder::MeanTime), @r#" + insta::assert_snapshot!(get_output::(&results, Some(TimeUnit::Second), SortOrder::MeanTime), @r#" | Command | Mean [s] | Min [s] | Max [s] | Relative | |:---|---:|---:|---:|---:| | `sleep 0.1` | 0.120 ± 0.010 | 0.110 | 0.130 | 1.00 | diff --git a/src/options.rs b/src/options.rs index 0c77a3e..634e2b5 100644 --- a/src/options.rs +++ b/src/options.rs @@ -10,7 +10,7 @@ use clap::ArgMatches; use crate::command::Commands; use crate::error::OptionsError; use crate::quantity::{Time, TimeQuantity}; -use crate::util::units::Unit; +use crate::util::units::TimeUnit; use anyhow::Result; @@ -437,8 +437,6 @@ impl Options { Some("µs" | "us" | "microsecond" | "microseconds") => Some(TimeUnit::MicroSecond), Some("ms" | "millisecond" | "milliseconds") => Some(TimeUnit::MilliSecond), Some("s" | "second" | "seconds") => Some(TimeUnit::Second), - Some("min" | "minute" | "minutes") => Some(TimeUnit::Minute), - Some("h" | "hour" | "hours") => Some(TimeUnit::Hour), _ => None, };