diff --git a/src/hyperfine/benchmark.rs b/src/hyperfine/benchmark.rs index 76dac51..e984633 100644 --- a/src/hyperfine/benchmark.rs +++ b/src/hyperfine/benchmark.rs @@ -8,13 +8,12 @@ use statistical::{mean, median, standard_deviation}; use crate::hyperfine::command::Command; use crate::hyperfine::format::{format_duration, format_duration_unit}; use crate::hyperfine::internal::{get_progress_bar, max, min, MIN_EXECUTION_TIME}; +use crate::hyperfine::options::{CmdFailureAction, HyperfineOptions, OutputStyleOption}; use crate::hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD}; use crate::hyperfine::shell::execute_and_time; use crate::hyperfine::timer::wallclocktimer::WallClockTimer; use crate::hyperfine::timer::{TimerStart, TimerStop}; -use crate::hyperfine::types::{ - BenchmarkResult, CmdFailureAction, HyperfineOptions, OutputStyleOption, -}; +use crate::hyperfine::types::BenchmarkResult; use crate::hyperfine::units::Second; use crate::hyperfine::warnings::Warnings; diff --git a/src/hyperfine/internal.rs b/src/hyperfine/internal.rs index 26ea2d7..7c93f56 100644 --- a/src/hyperfine/internal.rs +++ b/src/hyperfine/internal.rs @@ -1,7 +1,8 @@ use colored::*; use indicatif::{ProgressBar, ProgressStyle}; -use crate::hyperfine::types::{BenchmarkResult, OutputStyleOption}; +use crate::hyperfine::options::OutputStyleOption; +use crate::hyperfine::types::BenchmarkResult; use crate::hyperfine::units::{Scalar, Second}; use std::cmp::Ordering; diff --git a/src/hyperfine/mod.rs b/src/hyperfine/mod.rs index 5a9f5b0..7ec7636 100644 --- a/src/hyperfine/mod.rs +++ b/src/hyperfine/mod.rs @@ -5,6 +5,7 @@ pub mod error; pub mod export; pub mod format; pub mod internal; +pub mod options; pub mod outlier_detection; pub mod parameter_range; pub mod shell; diff --git a/src/hyperfine/options.rs b/src/hyperfine/options.rs new file mode 100644 index 0000000..fdb7e7b --- /dev/null +++ b/src/hyperfine/options.rs @@ -0,0 +1,104 @@ +use super::{ + types::DEFAULT_SHELL, + units::{Second, Unit}, +}; + +/// Action to take when an executed command fails. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum CmdFailureAction { + /// Exit with an error message + RaiseError, + + /// Simply ignore the non-zero exit code + Ignore, +} + +/// Output style type option +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum OutputStyleOption { + /// Do not output with colors or any special formatting + Basic, + + /// Output with full color and formatting + Full, + + /// Keep elements such as progress bar, but use no coloring + NoColor, + + /// Keep coloring, but use no progress bar + Color, + + /// Disable all the output + Disabled, +} + +/// Number of runs for a benchmark +pub struct Runs { + /// Minimum number of benchmark runs + pub min: u64, + + /// Maximum number of benchmark runs + pub max: Option, +} + +impl Default for Runs { + fn default() -> Runs { + Runs { min: 10, max: None } + } +} + +/// A set of options for hyperfine +pub struct HyperfineOptions { + /// Number of warmup runs + pub warmup_count: u64, + + /// Number of benchmark runs + pub runs: Runs, + + /// Minimum benchmarking time + pub min_time_sec: Second, + + /// Whether or not to ignore non-zero exit codes + pub failure_action: CmdFailureAction, + + /// Command to run before each timing run + pub preparation_command: Option>, + + /// Command to run after each benchmark + pub cleanup_command: Option, + + /// What color mode to use for output + pub output_style: OutputStyleOption, + + /// The shell to use for executing commands. + pub shell: String, + + /// Forward benchmark's stdout to hyperfine's stdout + pub show_output: bool, + + /// Which time unit to use for CLI & Markdown output + pub time_unit: Option, + + /// A list of custom command names that, if defined, + /// will be used instead of the command itself in + /// benchmark outputs. + pub names: Option>, +} + +impl Default for HyperfineOptions { + fn default() -> HyperfineOptions { + HyperfineOptions { + names: None, + warmup_count: 0, + runs: Runs::default(), + min_time_sec: 3.0, + failure_action: CmdFailureAction::RaiseError, + preparation_command: None, + cleanup_command: None, + output_style: OutputStyleOption::Full, + shell: DEFAULT_SHELL.to_string(), + show_output: false, + time_unit: None, + } + } +} diff --git a/src/hyperfine/types.rs b/src/hyperfine/types.rs index 1259b88..8089f2b 100644 --- a/src/hyperfine/types.rs +++ b/src/hyperfine/types.rs @@ -6,7 +6,7 @@ use std::fmt; use rust_decimal::prelude::ToPrimitive; use rust_decimal::Decimal; -use crate::hyperfine::units::{Second, Unit}; +use crate::hyperfine::units::Second; #[cfg(not(windows))] pub const DEFAULT_SHELL: &str = "sh"; @@ -71,106 +71,6 @@ impl<'a> ToString for ParameterValue { } } -/// Action to take when an executed command fails. -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum CmdFailureAction { - /// Exit with an error message - RaiseError, - - /// Simply ignore the non-zero exit code - Ignore, -} - -/// Output style type option -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum OutputStyleOption { - /// Do not output with colors or any special formatting - Basic, - - /// Output with full color and formatting - Full, - - /// Keep elements such as progress bar, but use no coloring - NoColor, - - /// Keep coloring, but use no progress bar - Color, - - /// Disable all the output - Disabled, -} - -/// Number of runs for a benchmark -pub struct Runs { - /// Minimum number of benchmark runs - pub min: u64, - - /// Maximum number of benchmark runs - pub max: Option, -} - -impl Default for Runs { - fn default() -> Runs { - Runs { min: 10, max: None } - } -} - -/// A set of options for hyperfine -pub struct HyperfineOptions { - /// Number of warmup runs - pub warmup_count: u64, - - /// Number of benchmark runs - pub runs: Runs, - - /// Minimum benchmarking time - pub min_time_sec: Second, - - /// Whether or not to ignore non-zero exit codes - pub failure_action: CmdFailureAction, - - /// Command to run before each timing run - pub preparation_command: Option>, - - /// Command to run after each benchmark - pub cleanup_command: Option, - - /// What color mode to use for output - pub output_style: OutputStyleOption, - - /// The shell to use for executing commands. - pub shell: String, - - /// Forward benchmark's stdout to hyperfine's stdout - pub show_output: bool, - - /// Which time unit to use for CLI & Markdown output - pub time_unit: Option, - - /// A list of custom command names that, if defined, - /// will be used instead of the command itself in - /// benchmark outputs. - pub names: Option>, -} - -impl Default for HyperfineOptions { - fn default() -> HyperfineOptions { - HyperfineOptions { - names: None, - warmup_count: 0, - runs: Runs::default(), - min_time_sec: 3.0, - failure_action: CmdFailureAction::RaiseError, - preparation_command: None, - cleanup_command: None, - output_style: OutputStyleOption::Full, - shell: DEFAULT_SHELL.to_string(), - show_output: false, - time_unit: None, - } - } -} - /// Set of values that will be exported. // NOTE: `serde` is used for JSON serialization, but not for CSV serialization due to the // `parameters` map. Update `src/hyperfine/export/csv.rs` with new fields, as appropriate. diff --git a/src/main.rs b/src/main.rs index 0f14941..a18189d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,10 +15,9 @@ use crate::hyperfine::command::Command; use crate::hyperfine::error::OptionsError; use crate::hyperfine::export::{ExportManager, ExportType}; use crate::hyperfine::internal::{tokenize, write_benchmark_comparison}; +use crate::hyperfine::options::{CmdFailureAction, HyperfineOptions, OutputStyleOption}; use crate::hyperfine::parameter_range::get_parameterized_commands; -use crate::hyperfine::types::{ - CmdFailureAction, HyperfineOptions, OutputStyleOption, ParameterValue, -}; +use crate::hyperfine::types::ParameterValue; use crate::hyperfine::units::Unit; /// Print error message to stderr and terminate