Skip to content

Commit

Permalink
Moved types::Second and types::Unit to new unit module
Browse files Browse the repository at this point in the history
Also review feedback.
  • Loading branch information
jasonpeacock authored and sharkdp committed Nov 4, 2018
1 parent f0b6f13 commit 3aa26a3
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 64 deletions.
5 changes: 2 additions & 3 deletions src/hyperfine/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/hyperfine/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/hyperfine/export/csv.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
3 changes: 2 additions & 1 deletion src/hyperfine/export/json.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
7 changes: 4 additions & 3 deletions src/hyperfine/export/markdown.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -10,9 +11,9 @@ pub struct MarkdownExporter {}

impl Exporter for MarkdownExporter {
fn serialize(&self, results: &Vec<BenchmarkResult>, unit: Option<Unit>) -> Result<Vec<u8>> {
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
Expand Down
3 changes: 2 additions & 1 deletion src/hyperfine/export/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/hyperfine/format.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/hyperfine/internal.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/hyperfine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ pub mod outlier_detection;
pub mod shell;
pub mod timer;
pub mod types;
pub mod units;
pub mod warnings;
2 changes: 1 addition & 1 deletion src/hyperfine/timer/internal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use hyperfine::types::Second;
use hyperfine::units::Second;

#[derive(Debug, Copy, Clone)]
pub struct CPUTimes {
Expand Down
2 changes: 1 addition & 1 deletion src/hyperfine/timer/unix_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/hyperfine/timer/wallclocktimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/hyperfine/timer/windows_timer.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
44 changes: 2 additions & 42 deletions src/hyperfine/types.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down Expand Up @@ -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));
}
43 changes: 43 additions & 0 deletions src/hyperfine/units.rs
Original file line number Diff line number Diff line change
@@ -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));
}
2 changes: 1 addition & 1 deletion src/hyperfine/warnings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> ! {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3aa26a3

Please sign in to comment.