Moved `types::Second` and `types::Unit` to new `unit` module
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Also review feedback.
authored by Jason Peacock · committed by David Peter · parent f0b6f13 · Browse files · 17 files·+73 added−64 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
17 files+73 added−64 removed
- other:+73 added−64 removed
Provenance
- Commit
3aa26a3- Tree
0d093e8- Author
Jason Peacock <jason@jasonpeacock.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
f0b6f13
17 files changed+73 added−64 removed
modified:src/hyperfine/export/csv.rs+2 added-1 removed
1 use super::Exporter;2 3-use hyperfine::types::{BenchmarkResult, Unit};3+use hyperfine::types::BenchmarkResult;4+use hyperfine::units::Unit;5 6 use std::io::{Error, ErrorKind, Result};7 8 use csv::WriterBuilder;20 unmodified linesmodified:src/hyperfine/export/json.rs+2 added-1 removed
1 use super::Exporter;2-use hyperfine::types::{BenchmarkResult, Unit};2+use hyperfine::types::BenchmarkResult;3+use hyperfine::units::Unit;4 5 use std::io::{Error, ErrorKind, Result};6 7 use serde_json::to_vec_pretty;19 unmodified linesmodified:src/hyperfine/export/markdown.rs+4 added-3 removed
1 use super::Exporter;2 3 use hyperfine::format::format_duration_value;4-use hyperfine::types::{BenchmarkResult, Unit};4+use hyperfine::types::BenchmarkResult;5+use hyperfine::units::Unit;6 7 use std::io::Result;8 9 #[derive(Default)]10 pub struct MarkdownExporter {}11 12 impl Exporter for MarkdownExporter {13 fn serialize(&self, results: &Vec<BenchmarkResult>, unit: Option<Unit>) -> Result<Vec<u8>> {13- let unit = if unit.is_some() {14+ let unit = if let Some(unit) = unit {15 // Use the given unit for all entries.15- unit.unwrap()16+ unit17 } else if let Some(first_result) = results.first() {18 // Use the first BenchmarkResult entry to determine the unit for all entries.19 format_duration_value(first_result.mean, None).120 } else {206 unmodified linesmodified:src/hyperfine/export/mod.rs+2 added-1 removed
7 unmodified lines8 9 use std::fs::File;10 use std::io::{Result, Write};11 12-use hyperfine::types::{BenchmarkResult, Unit};12+use hyperfine::types::BenchmarkResult;13+use hyperfine::units::Unit;14 15 /// The desired form of exporter to use for a given file.16 #[derive(Clone)]17 pub enum ExportType {62 unmodified linesmodified:src/hyperfine/timer/internal.rs+1 added-1 removed
1-use hyperfine::types::Second;1+use hyperfine::units::Second;2 3 #[derive(Debug, Copy, Clone)]4 pub struct CPUTimes {5 /// Total amount of time spent executing in user mode14 unmodified linesmodified:src/hyperfine/timer/unix_timer.rs+1 added-1 removed
1 #![cfg(not(windows))]2 3 use super::internal::{CPUInterval, CPUTimes};4 use hyperfine::timer::{TimerStart, TimerStop};5-use hyperfine::types::Second;5+use hyperfine::units::Second;6 7 use std::mem;8 use std::process::Child;9 83 unmodified linesmodified:src/hyperfine/timer/wallclocktimer.rs+1 added-1 removed
1 use std::process::Child;2 use std::time::Instant;3 4 use hyperfine::timer::{TimerStart, TimerStop};5-use hyperfine::types::Second;5+use hyperfine::units::Second;6 7 pub struct WallClockTimer {8 start: Instant,9 }21 unmodified linesmodified:src/hyperfine/timer/windows_timer.rs+1 added-1 removed
1 #![cfg(windows)]2 3 use super::internal::CPUTimes;4-use hyperfine::types::Second;4 use hyperfine::timer::{TimerStart, TimerStop};5+use hyperfine::units::Second;6 7 use winapi::um::processthreadsapi::GetProcessTimes;8 use winapi::um::winnt::HANDLE;9 73 unmodified linesmodified:src/hyperfine/app.rs+2 added-3 removed
127 unmodified lines128 Arg::with_name("time-unit")129 .long("time-unit")130 .short("u")131 .takes_value(true)132- .value_name("millisecond|second")132+ .value_name("UNIT")133 .possible_values(&["millisecond", "second"])134- .help( "Set the time unit used for CLI output and Markdown export.",135- ),134+ .help("Set the time unit used. Possible values: millisecond, second."),135 )136 .arg(137 Arg::with_name("export-csv")138 .long("export-csv")32 unmodified linesmodified:src/hyperfine/benchmark.rs+2 added-2 removed
9 unmodified lines10 use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};11 use hyperfine::shell::execute_and_time;12 use hyperfine::timer::wallclocktimer::WallClockTimer;13 use hyperfine::timer::{TimerStart, TimerStop};14-use hyperfine::types::{BenchmarkResult, Command};15-use hyperfine::types::{CmdFailureAction, HyperfineOptions, OutputStyleOption, Second};14+use hyperfine::types::{BenchmarkResult, Command, CmdFailureAction, HyperfineOptions, OutputStyleOption};15+use hyperfine::units::Second;16 use hyperfine::warnings::Warnings;17 18 /// Results from timing a single shell command19 #[derive(Debug, Default, Copy, Clone)]338 unmodified linesmodified:src/hyperfine/format.rs+1 added-1 removed
1-use hyperfine::types::{Second, Unit};1+use hyperfine::units::{Second, Unit};2 3 /// Format the given duration as a string. The output-unit can be enforced by setting `unit` to4 /// `Some(target_unit)`. If `unit` is `None`, it will be determined automatically.5 pub fn format_duration(duration: Second, unit: Option<Unit>) -> String {61 unmodified linesmodified:src/hyperfine/internal.rs+2 added-1 removed
1 use colored::*;2 use indicatif::{ProgressBar, ProgressStyle};3 4-use hyperfine::types::{BenchmarkResult, OutputStyleOption, Second};4+use hyperfine::types::{BenchmarkResult, OutputStyleOption};5+use hyperfine::units::Second;6 7 use std::cmp::Ordering;8 use std::iter::Iterator;9 86 unmodified linesmodified:src/hyperfine/mod.rs+1 added
6 unmodified lines7 pub mod outlier_detection;8 pub mod shell;9 pub mod timer;10 pub mod types;11+pub mod units;12 pub mod warnings;modified:src/hyperfine/types.rs+2 added-42 removed
1 /// This module contains common internal types.2 use std::fmt;3 4+use hyperfine::units::{Second, Unit};5+6 #[cfg(not(windows))]7 pub const DEFAULT_SHELL: &str = "sh";8 9 #[cfg(windows)]10 pub const DEFAULT_SHELL: &str = "cmd.exe";11 10-/// Type alias for unit of time11-pub type Second = f64;12-13-/// Supported time units14-#[derive(Debug, Clone, Copy, PartialEq)]15-pub enum Unit {16- Second,17- MilliSecond,18-}19-20-impl Unit {21- /// The abbreviation of the Unit.22- pub fn short_name(&self) -> String {23- match *self {24- Unit::Second => String::from("s"),25- Unit::MilliSecond => String::from("ms"),26- }27- }28-29- /// Returns the Second value formatted for the Unit.30- pub fn format(&self, value: Second) -> String {31- match *self {32- Unit::Second => format!("{:.3}", value),33- Unit::MilliSecond => format!("{:.1}", value * 1e3),34- }35- }36-}37-12 /// A command that should be benchmarked.13 #[derive(Debug, Clone)]14 pub struct Command<'a> {15 /// The command that should be executed (without parameter substitution)178 unmodified lines194 times: Some(times),195 }196 }197 }224-225-#[test]226-fn test_unit_short_name() {227- assert_eq!("s", Unit::Second.short_name());228- assert_eq!("ms", Unit::MilliSecond.short_name());229-}230-231-// Note - the values are rounded when formatted.232-#[test]233-fn test_unit_format() {234- let value: Second = 123.456789;235- assert_eq!("123.457", Unit::Second.format(value));236- assert_eq!("123456.8", Unit::MilliSecond.format(value));237-}added:src/hyperfine/units.rs+43 added
1+/// This module contains common units.2+3+/// Type alias for unit of time4+pub type Second = f64;5+6+/// Supported time units7+#[derive(Debug, Clone, Copy, PartialEq)]8+pub enum Unit {9+ Second,10+ MilliSecond,11+}12+13+impl Unit {14+ /// The abbreviation of the Unit.15+ pub fn short_name(&self) -> String {16+ match *self {17+ Unit::Second => String::from("s"),18+ Unit::MilliSecond => String::from("ms"),19+ }20+ }21+22+ /// Returns the Second value formatted for the Unit.23+ pub fn format(&self, value: Second) -> String {24+ match *self {25+ Unit::Second => format!("{:.3}", value),26+ Unit::MilliSecond => format!("{:.1}", value * 1e3),27+ }28+ }29+}30+31+#[test]32+fn test_unit_short_name() {33+ assert_eq!("s", Unit::Second.short_name());34+ assert_eq!("ms", Unit::MilliSecond.short_name());35+}36+37+// Note - the values are rounded when formatted.38+#[test]39+fn test_unit_format() {40+ let value: Second = 123.456789;41+ assert_eq!("123.457", Unit::Second.format(value));42+ assert_eq!("123456.8", Unit::MilliSecond.format(value));43+}modified:src/hyperfine/warnings.rs+1 added-1 removed
1 use std::fmt;2 3 use hyperfine::format::format_duration;4 use hyperfine::internal::MIN_EXECUTION_TIME;5-use hyperfine::types::Second;5+use hyperfine::units::Second;6 7 /// A list of all possible warnings8 pub enum Warnings {9 FastExecutionTime,32 unmodified linesmodified:src/main.rs+5 added-4 removed
44 unmodified lines45 use hyperfine::error::{OptionsError, ParameterScanError};46 use hyperfine::export::{ExportManager, ExportType};47 use hyperfine::internal::write_benchmark_comparison;48 use hyperfine::types::{49- BenchmarkResult, Unit, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,49+ BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,50 };51+use hyperfine::units::Unit;52 53 /// Print error message to stderr and terminate54 pub fn error(message: &str) -> ! {55 eprintln!("{} {}", "Error:".red(), message);39 unmodified lines95 let options = build_hyperfine_options(&matches);96 let export_manager = build_export_manager(&matches);97 let commands = build_commands(&matches);98 98- let res = match &options {99- &Ok(ref opts) => run(&commands, &opts),100- &Err(ref e) => error(e.description()),99+ let res = match options {100+ Ok(ref opts) => run(&commands, &opts),101+ Err(ref e) => error(e.description()),102 };103 104 match res {105 Ok(timing_results) => {142 unmodified lines