Skip to content
Myr

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

17 files+73 added−64 removed

modified:src/hyperfine/export/csv.rs+2 added-1 removed
1use super::Exporter;23use hyperfine::types::{BenchmarkResult, Unit};3use hyperfine::types::BenchmarkResult;4use hyperfine::units::Unit;56use std::io::{Error, ErrorKind, Result};78use csv::WriterBuilder;
20 unmodified lines
modified:src/hyperfine/export/json.rs+2 added-1 removed
1use super::Exporter;2use hyperfine::types::{BenchmarkResult, Unit};2use hyperfine::types::BenchmarkResult;3use hyperfine::units::Unit;45use std::io::{Error, ErrorKind, Result};67use serde_json::to_vec_pretty;
19 unmodified lines
modified:src/hyperfine/export/markdown.rs+4 added-3 removed
1use super::Exporter;23use hyperfine::format::format_duration_value;4use hyperfine::types::{BenchmarkResult, Unit};4use hyperfine::types::BenchmarkResult;5use hyperfine::units::Unit;67use std::io::Result;89#[derive(Default)]10pub struct MarkdownExporter {}1112impl 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 lines
modified:src/hyperfine/export/mod.rs+2 added-1 removed
7 unmodified lines
89use std::fs::File;10use std::io::{Result, Write};1112use hyperfine::types::{BenchmarkResult, Unit};12use hyperfine::types::BenchmarkResult;13use hyperfine::units::Unit;1415/// The desired form of exporter to use for a given file.16#[derive(Clone)]17pub enum ExportType {
62 unmodified lines
modified:src/hyperfine/timer/internal.rs+1 added-1 removed
1use hyperfine::types::Second;1use hyperfine::units::Second;23#[derive(Debug, Copy, Clone)]4pub struct CPUTimes {5 /// Total amount of time spent executing in user mode
14 unmodified lines
modified:src/hyperfine/timer/unix_timer.rs+1 added-1 removed
1#![cfg(not(windows))]23use super::internal::{CPUInterval, CPUTimes};4use hyperfine::timer::{TimerStart, TimerStop};5use hyperfine::types::Second;5use hyperfine::units::Second;67use std::mem;8use std::process::Child;9
83 unmodified lines
modified:src/hyperfine/timer/wallclocktimer.rs+1 added-1 removed
1use std::process::Child;2use std::time::Instant;34use hyperfine::timer::{TimerStart, TimerStop};5use hyperfine::types::Second;5use hyperfine::units::Second;67pub struct WallClockTimer {8 start: Instant,9}
21 unmodified lines
modified:src/hyperfine/timer/windows_timer.rs+1 added-1 removed
1#![cfg(windows)]23use super::internal::CPUTimes;4use hyperfine::types::Second;4use hyperfine::timer::{TimerStart, TimerStop};5use hyperfine::units::Second;67use winapi::um::processthreadsapi::GetProcessTimes;8use winapi::um::winnt::HANDLE;9
73 unmodified lines
modified:src/hyperfine/app.rs+2 added-3 removed
127 unmodified lines
128 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 lines
modified:src/hyperfine/benchmark.rs+2 added-2 removed
9 unmodified lines
10use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};11use hyperfine::shell::execute_and_time;12use hyperfine::timer::wallclocktimer::WallClockTimer;13use hyperfine::timer::{TimerStart, TimerStop};14use hyperfine::types::{BenchmarkResult, Command};15use hyperfine::types::{CmdFailureAction, HyperfineOptions, OutputStyleOption, Second};14use hyperfine::types::{BenchmarkResult, Command, CmdFailureAction, HyperfineOptions, OutputStyleOption};15use hyperfine::units::Second;16use hyperfine::warnings::Warnings;1718/// Results from timing a single shell command19#[derive(Debug, Default, Copy, Clone)]
338 unmodified lines
modified:src/hyperfine/format.rs+1 added-1 removed
1use hyperfine::types::{Second, Unit};1use hyperfine::units::{Second, Unit};23/// 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.5pub fn format_duration(duration: Second, unit: Option<Unit>) -> String {
61 unmodified lines
modified:src/hyperfine/internal.rs+2 added-1 removed
1use colored::*;2use indicatif::{ProgressBar, ProgressStyle};34use hyperfine::types::{BenchmarkResult, OutputStyleOption, Second};4use hyperfine::types::{BenchmarkResult, OutputStyleOption};5use hyperfine::units::Second;67use std::cmp::Ordering;8use std::iter::Iterator;9
86 unmodified lines
modified:src/hyperfine/mod.rs+1 added
6 unmodified lines
7pub mod outlier_detection;8pub mod shell;9pub mod timer;10pub mod types;11pub mod units;12pub mod warnings;
modified:src/hyperfine/types.rs+2 added-42 removed
1/// This module contains common internal types.2use std::fmt;34use hyperfine::units::{Second, Unit};56#[cfg(not(windows))]7pub const DEFAULT_SHELL: &str = "sh";89#[cfg(windows)]10pub const DEFAULT_SHELL: &str = "cmd.exe";1110/// Type alias for unit of time11pub type Second = f64;1213/// Supported time units14#[derive(Debug, Clone, Copy, PartialEq)]15pub enum Unit {16 Second,17 MilliSecond,18}1920impl 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 }2829 /// 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}3712/// A command that should be benchmarked.13#[derive(Debug, Clone)]14pub struct Command<'a> {15 /// The command that should be executed (without parameter substitution)
178 unmodified lines
194 times: Some(times),195 }196 }197}224225#[test]226fn test_unit_short_name() {227 assert_eq!("s", Unit::Second.short_name());228 assert_eq!("ms", Unit::MilliSecond.short_name());229}230231// Note - the values are rounded when formatted.232#[test]233fn 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.23/// Type alias for unit of time4pub type Second = f64;56/// Supported time units7#[derive(Debug, Clone, Copy, PartialEq)]8pub enum Unit {9 Second,10 MilliSecond,11}1213impl 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 }2122 /// 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}3031#[test]32fn test_unit_short_name() {33 assert_eq!("s", Unit::Second.short_name());34 assert_eq!("ms", Unit::MilliSecond.short_name());35}3637// Note - the values are rounded when formatted.38#[test]39fn 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
1use std::fmt;23use hyperfine::format::format_duration;4use hyperfine::internal::MIN_EXECUTION_TIME;5use hyperfine::types::Second;5use hyperfine::units::Second;67/// A list of all possible warnings8pub enum Warnings {9 FastExecutionTime,
32 unmodified lines
modified:src/main.rs+5 added-4 removed
44 unmodified lines
45use hyperfine::error::{OptionsError, ParameterScanError};46use hyperfine::export::{ExportManager, ExportType};47use hyperfine::internal::write_benchmark_comparison;48use hyperfine::types::{49 BenchmarkResult, Unit, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,49 BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,50};51use hyperfine::units::Unit;5253/// Print error message to stderr and terminate54pub fn error(message: &str) -> ! {55 eprintln!("{} {}", "Error:".red(), message);
39 unmodified lines
95 let options = build_hyperfine_options(&matches);96 let export_manager = build_export_manager(&matches);97 let commands = build_commands(&matches);9898 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 };103104 match res {105 Ok(timing_results) => {
142 unmodified lines