diff --git a/src/hyperfine/app.rs b/src/hyperfine/app.rs index 5863553..9e2aa9c 100644 --- a/src/hyperfine/app.rs +++ b/src/hyperfine/app.rs @@ -129,10 +129,9 @@ fn build_app() -> App<'static, 'static> { .long("time-unit") .short("u") .takes_value(true) - .value_name("millisecond|second") + .value_name("UNIT") .possible_values(&["millisecond", "second"]) - .help( "Set the time unit used for CLI output and Markdown export.", - ), + .help("Set the time unit used. Possible values: millisecond, second."), ) .arg( Arg::with_name("export-csv") diff --git a/src/hyperfine/benchmark.rs b/src/hyperfine/benchmark.rs index 6e89c53..3be7bb6 100644 --- a/src/hyperfine/benchmark.rs +++ b/src/hyperfine/benchmark.rs @@ -11,8 +11,8 @@ use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD}; use hyperfine::shell::execute_and_time; use hyperfine::timer::wallclocktimer::WallClockTimer; use hyperfine::timer::{TimerStart, TimerStop}; -use hyperfine::types::{BenchmarkResult, Command}; -use hyperfine::types::{CmdFailureAction, HyperfineOptions, OutputStyleOption, Second}; +use hyperfine::types::{BenchmarkResult, Command, CmdFailureAction, HyperfineOptions, OutputStyleOption}; +use hyperfine::units::Second; use hyperfine::warnings::Warnings; /// Results from timing a single shell command diff --git a/src/hyperfine/export/csv.rs b/src/hyperfine/export/csv.rs index 7be8b46..57c0bef 100644 --- a/src/hyperfine/export/csv.rs +++ b/src/hyperfine/export/csv.rs @@ -1,6 +1,7 @@ use super::Exporter; -use hyperfine::types::{BenchmarkResult, Unit}; +use hyperfine::types::BenchmarkResult; +use hyperfine::units::Unit; use std::io::{Error, ErrorKind, Result}; diff --git a/src/hyperfine/export/json.rs b/src/hyperfine/export/json.rs index 4e6e311..62a3f50 100644 --- a/src/hyperfine/export/json.rs +++ b/src/hyperfine/export/json.rs @@ -1,5 +1,6 @@ use super::Exporter; -use hyperfine::types::{BenchmarkResult, Unit}; +use hyperfine::types::BenchmarkResult; +use hyperfine::units::Unit; use std::io::{Error, ErrorKind, Result}; diff --git a/src/hyperfine/export/markdown.rs b/src/hyperfine/export/markdown.rs index 00b22c0..d008c5a 100644 --- a/src/hyperfine/export/markdown.rs +++ b/src/hyperfine/export/markdown.rs @@ -1,7 +1,8 @@ use super::Exporter; use hyperfine::format::format_duration_value; -use hyperfine::types::{BenchmarkResult, Unit}; +use hyperfine::types::BenchmarkResult; +use hyperfine::units::Unit; use std::io::Result; @@ -10,9 +11,9 @@ pub struct MarkdownExporter {} impl Exporter for MarkdownExporter { fn serialize(&self, results: &Vec, unit: Option) -> Result> { - let unit = if unit.is_some() { + let unit = if let Some(unit) = unit { // Use the given unit for all entries. - unit.unwrap() + unit } else if let Some(first_result) = results.first() { // Use the first BenchmarkResult entry to determine the unit for all entries. format_duration_value(first_result.mean, None).1 diff --git a/src/hyperfine/export/mod.rs b/src/hyperfine/export/mod.rs index 852c4fd..b7ec0c9 100644 --- a/src/hyperfine/export/mod.rs +++ b/src/hyperfine/export/mod.rs @@ -9,7 +9,8 @@ use self::markdown::MarkdownExporter; use std::fs::File; use std::io::{Result, Write}; -use hyperfine::types::{BenchmarkResult, Unit}; +use hyperfine::types::BenchmarkResult; +use hyperfine::units::Unit; /// The desired form of exporter to use for a given file. #[derive(Clone)] diff --git a/src/hyperfine/format.rs b/src/hyperfine/format.rs index 7f7471c..08f0c61 100644 --- a/src/hyperfine/format.rs +++ b/src/hyperfine/format.rs @@ -1,4 +1,4 @@ -use hyperfine::types::{Second, Unit}; +use hyperfine::units::{Second, Unit}; /// Format the given 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. diff --git a/src/hyperfine/internal.rs b/src/hyperfine/internal.rs index 24f766c..51ea152 100644 --- a/src/hyperfine/internal.rs +++ b/src/hyperfine/internal.rs @@ -1,7 +1,8 @@ use colored::*; use indicatif::{ProgressBar, ProgressStyle}; -use hyperfine::types::{BenchmarkResult, OutputStyleOption, Second}; +use hyperfine::types::{BenchmarkResult, OutputStyleOption}; +use hyperfine::units::Second; use std::cmp::Ordering; use std::iter::Iterator; diff --git a/src/hyperfine/mod.rs b/src/hyperfine/mod.rs index 0c7f34a..e6af195 100644 --- a/src/hyperfine/mod.rs +++ b/src/hyperfine/mod.rs @@ -8,4 +8,5 @@ pub mod outlier_detection; pub mod shell; pub mod timer; pub mod types; +pub mod units; pub mod warnings; diff --git a/src/hyperfine/timer/internal.rs b/src/hyperfine/timer/internal.rs index 04cc2a7..49ef3ce 100644 --- a/src/hyperfine/timer/internal.rs +++ b/src/hyperfine/timer/internal.rs @@ -1,4 +1,4 @@ -use hyperfine::types::Second; +use hyperfine::units::Second; #[derive(Debug, Copy, Clone)] pub struct CPUTimes { diff --git a/src/hyperfine/timer/unix_timer.rs b/src/hyperfine/timer/unix_timer.rs index 33457c2..8d940dd 100644 --- a/src/hyperfine/timer/unix_timer.rs +++ b/src/hyperfine/timer/unix_timer.rs @@ -2,7 +2,7 @@ use super::internal::{CPUInterval, CPUTimes}; use hyperfine::timer::{TimerStart, TimerStop}; -use hyperfine::types::Second; +use hyperfine::units::Second; use std::mem; use std::process::Child; diff --git a/src/hyperfine/timer/wallclocktimer.rs b/src/hyperfine/timer/wallclocktimer.rs index 3780ecb..b28a24e 100644 --- a/src/hyperfine/timer/wallclocktimer.rs +++ b/src/hyperfine/timer/wallclocktimer.rs @@ -2,7 +2,7 @@ use std::process::Child; use std::time::Instant; use hyperfine::timer::{TimerStart, TimerStop}; -use hyperfine::types::Second; +use hyperfine::units::Second; pub struct WallClockTimer { start: Instant, diff --git a/src/hyperfine/timer/windows_timer.rs b/src/hyperfine/timer/windows_timer.rs index 512600e..4471e8d 100644 --- a/src/hyperfine/timer/windows_timer.rs +++ b/src/hyperfine/timer/windows_timer.rs @@ -1,8 +1,8 @@ #![cfg(windows)] use super::internal::CPUTimes; -use hyperfine::types::Second; use hyperfine::timer::{TimerStart, TimerStop}; +use hyperfine::units::Second; use winapi::um::processthreadsapi::GetProcessTimes; use winapi::um::winnt::HANDLE; diff --git a/src/hyperfine/types.rs b/src/hyperfine/types.rs index dd2547d..1568a5e 100644 --- a/src/hyperfine/types.rs +++ b/src/hyperfine/types.rs @@ -1,40 +1,14 @@ /// This module contains common internal types. use std::fmt; +use hyperfine::units::{Second, Unit}; + #[cfg(not(windows))] pub const DEFAULT_SHELL: &str = "sh"; #[cfg(windows)] pub const DEFAULT_SHELL: &str = "cmd.exe"; -/// Type alias for unit of time -pub type Second = f64; - -/// Supported time units -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum Unit { - Second, - MilliSecond, -} - -impl Unit { - /// The abbreviation of the Unit. - pub fn short_name(&self) -> String { - match *self { - Unit::Second => String::from("s"), - Unit::MilliSecond => String::from("ms"), - } - } - - /// Returns the Second value formatted for the Unit. - pub fn format(&self, value: Second) -> String { - match *self { - Unit::Second => format!("{:.3}", value), - Unit::MilliSecond => format!("{:.1}", value * 1e3), - } - } -} - /// A command that should be benchmarked. #[derive(Debug, Clone)] pub struct Command<'a> { @@ -221,17 +195,3 @@ impl BenchmarkResult { } } } - -#[test] -fn test_unit_short_name() { - assert_eq!("s", Unit::Second.short_name()); - assert_eq!("ms", Unit::MilliSecond.short_name()); -} - -// Note - the values are rounded when formatted. -#[test] -fn test_unit_format() { - let value: Second = 123.456789; - assert_eq!("123.457", Unit::Second.format(value)); - assert_eq!("123456.8", Unit::MilliSecond.format(value)); -} diff --git a/src/hyperfine/units.rs b/src/hyperfine/units.rs new file mode 100644 index 0000000..7da1843 --- /dev/null +++ b/src/hyperfine/units.rs @@ -0,0 +1,43 @@ +/// This module contains common units. + +/// Type alias for unit of time +pub type Second = f64; + +/// Supported time units +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Unit { + Second, + MilliSecond, +} + +impl Unit { + /// The abbreviation of the Unit. + pub fn short_name(&self) -> String { + match *self { + Unit::Second => String::from("s"), + Unit::MilliSecond => String::from("ms"), + } + } + + /// Returns the Second value formatted for the Unit. + pub fn format(&self, value: Second) -> String { + match *self { + Unit::Second => format!("{:.3}", value), + Unit::MilliSecond => format!("{:.1}", value * 1e3), + } + } +} + +#[test] +fn test_unit_short_name() { + assert_eq!("s", Unit::Second.short_name()); + assert_eq!("ms", Unit::MilliSecond.short_name()); +} + +// Note - the values are rounded when formatted. +#[test] +fn test_unit_format() { + let value: Second = 123.456789; + assert_eq!("123.457", Unit::Second.format(value)); + assert_eq!("123456.8", Unit::MilliSecond.format(value)); +} diff --git a/src/hyperfine/warnings.rs b/src/hyperfine/warnings.rs index 7032ed4..d14df60 100644 --- a/src/hyperfine/warnings.rs +++ b/src/hyperfine/warnings.rs @@ -2,7 +2,7 @@ use std::fmt; use hyperfine::format::format_duration; use hyperfine::internal::MIN_EXECUTION_TIME; -use hyperfine::types::Second; +use hyperfine::units::Second; /// A list of all possible warnings pub enum Warnings { diff --git a/src/main.rs b/src/main.rs index 7940e6b..9a0c49c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,8 +46,9 @@ use hyperfine::error::{OptionsError, ParameterScanError}; use hyperfine::export::{ExportManager, ExportType}; use hyperfine::internal::write_benchmark_comparison; use hyperfine::types::{ - BenchmarkResult, Unit, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, + BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, }; +use hyperfine::units::Unit; /// Print error message to stderr and terminate pub fn error(message: &str) -> ! { @@ -95,9 +96,9 @@ fn main() { let export_manager = build_export_manager(&matches); let commands = build_commands(&matches); - let res = match &options { - &Ok(ref opts) => run(&commands, &opts), - &Err(ref e) => error(e.description()), + let res = match options { + Ok(ref opts) => run(&commands, &opts), + Err(ref e) => error(e.description()), }; match res {