Moved types::Second and types::Unit to new unit module

Also review feedback.
This commit is contained in:
Jason Peacock authored and David Peter committed 2018-11-04 10:15:05 +01:00
1 parent f0b6f130ea
commit 3aa26a3e27
17 files changed
+73 -64

No files matched your search

+2 -3
View File
@@ -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")
+2 -2
View File
@@ -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
+2 -1
View File
@@ -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};
+2 -1
View File
@@ -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};
+4 -3
View File
@@ -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<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
+2 -1
View File
@@ -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)]
+1 -1
View File
@@ -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.
+2 -1
View File
@@ -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;
+1
View File
@@ -8,4 +8,5 @@ pub mod outlier_detection;
pub mod shell;
pub mod timer;
pub mod types;
pub mod units;
pub mod warnings;
+1 -1
View File
@@ -1,4 +1,4 @@
use hyperfine::types::Second;
use hyperfine::units::Second;
#[derive(Debug, Copy, Clone)]
pub struct CPUTimes {
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -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;
+2 -42
View File
@@ -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));
}
+43
View File
@@ -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));
}
+1 -1
View File
@@ -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 {
+5 -4
View File
@@ -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 {