New Scheduler struct
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent 39984a3 · Browse files · 6 files·+118 added−84 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+118 added−84 removed
- tests:+1 added−0 removed
- other:+117 added−84 removed
Provenance
- Commit
72d2772- Tree
c8772db- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
39984a3
6 files changed+118 added−84 removed
modified:src/benchmark/mod.rs+1 added-1 removed
1 pub mod relative_speed;2 pub mod result;3-pub mod schedule;3+pub mod scheduler;4 5 use std::cmp;6 use std::process::{ExitStatus, Stdio};7 513 unmodified linesdeleted:src/benchmark/schedule.rs-81 removed
1-use crate::command::Commands;2-use crate::export::ExportManager;3-use crate::options::{Options, OutputStyleOption};4-5-use super::{mean_shell_spawning_time, relative_speed, result::BenchmarkResult, run_benchmark};6-7-use anyhow::{bail, Result};8-use colored::*;9-10-pub fn write_benchmark_comparison(results: &[BenchmarkResult]) {11- if results.len() < 2 {12- return;13- }14-15- if let Some(mut annotated_results) = relative_speed::compute(results) {16- annotated_results.sort_by(|l, r| relative_speed::compare_mean_time(l.result, r.result));17-18- let fastest = &annotated_results[0];19- let others = &annotated_results[1..];20-21- println!("{}", "Summary".bold());22- println!(" '{}' ran", fastest.result.command.cyan());23-24- for item in others {25- println!(26- "{}{} times faster than '{}'",27- format!("{:8.2}", item.relative_speed).bold().green(),28- if let Some(stddev) = item.relative_speed_stddev {29- format!(" ± {}", format!("{:.2}", stddev).green())30- } else {31- "".into()32- },33- &item.result.command.magenta()34- );35- }36- } else {37- eprintln!(38- "{}: The benchmark comparison could not be computed as some benchmark times are zero. \39- This could be caused by background interference during the initial calibration phase \40- of hyperfine, in combination with very fast commands (faster than a few milliseconds). \41- Try to re-run the benchmark on a quiet system. If it does not help, you command is \42- most likely too fast to be accurately benchmarked by hyperfine.",43- "Note".bold().red()44- );45- }46-}47-48-pub fn run_benchmarks_and_print_comparison(49- commands: &Commands,50- options: &Options,51- export_manager: &ExportManager,52-) -> Result<()> {53- let shell_spawning_time =54- mean_shell_spawning_time(&options.shell, options.output_style, options.show_output)?;55-56- let mut timing_results = vec![];57-58- if let Some(preparation_command) = &options.preparation_command {59- if preparation_command.len() > 1 && commands.len() != preparation_command.len() {60- bail!(61- "The '--prepare' option has to be provided just once or N times, where N is the \62- number of benchmark commands."63- );64- }65- }66-67- // Run the benchmarks68- for (num, cmd) in commands.iter().enumerate() {69- timing_results.push(run_benchmark(num, cmd, shell_spawning_time, options)?);70-71- // Export (intermediate) results72- export_manager.write_results(&timing_results, options.time_unit)?;73- }74-75- // Print relative speed comparison76- if options.output_style != OutputStyleOption::Disabled {77- write_benchmark_comparison(&timing_results);78- }79-80- Ok(())81-}added:src/benchmark/scheduler.rs+92 added
1+use crate::command::Commands;2+use crate::export::ExportManager;3+use crate::options::{Options, OutputStyleOption};4+5+use super::{mean_shell_spawning_time, relative_speed, result::BenchmarkResult, run_benchmark};6+7+use anyhow::Result;8+use colored::*;9+10+pub struct Scheduler<'a> {11+ commands: &'a Commands<'a>,12+ options: &'a Options,13+ export_manager: &'a ExportManager,14+ results: Vec<BenchmarkResult>,15+}16+17+impl<'a> Scheduler<'a> {18+ pub fn new(19+ commands: &'a Commands,20+ options: &'a Options,21+ export_manager: &'a ExportManager,22+ ) -> Self {23+ Self {24+ commands,25+ options,26+ export_manager,27+ results: vec![],28+ }29+ }30+31+ pub fn run_benchmarks(&mut self) -> Result<()> {32+ let shell_spawning_time = mean_shell_spawning_time(33+ &self.options.shell,34+ self.options.output_style,35+ self.options.show_output,36+ )?;37+38+ // Run the benchmarks39+ for (num, cmd) in self.commands.iter().enumerate() {40+ self.results41+ .push(run_benchmark(num, cmd, shell_spawning_time, self.options)?);42+43+ // Export (intermediate) results44+ self.export_manager45+ .write_results(&self.results, self.options.time_unit)?;46+ }47+48+ Ok(())49+ }50+51+ pub fn print_relative_speed_comparison(&self) {52+ if self.options.output_style == OutputStyleOption::Disabled {53+ return;54+ }55+56+ if self.results.len() < 2 {57+ return;58+ }59+60+ if let Some(mut annotated_results) = relative_speed::compute(&self.results) {61+ annotated_results.sort_by(|l, r| relative_speed::compare_mean_time(l.result, r.result));62+63+ let fastest = &annotated_results[0];64+ let others = &annotated_results[1..];65+66+ println!("{}", "Summary".bold());67+ println!(" '{}' ran", fastest.result.command.cyan());68+69+ for item in others {70+ println!(71+ "{}{} times faster than '{}'",72+ format!("{:8.2}", item.relative_speed).bold().green(),73+ if let Some(stddev) = item.relative_speed_stddev {74+ format!(" ± {}", format!("{:.2}", stddev).green())75+ } else {76+ "".into()77+ },78+ &item.result.command.magenta()79+ );80+ }81+ } else {82+ eprintln!(83+ "{}: The benchmark comparison could not be computed as some benchmark times are zero. \84+ This could be caused by background interference during the initial calibration phase \85+ of hyperfine, in combination with very fast commands (faster than a few milliseconds). \86+ Try to re-run the benchmark on a quiet system. If it does not help, you command is \87+ most likely too fast to be accurately benchmarked by hyperfine.",88+ "Note".bold().red()89+ );90+ }91+ }92+}modified:src/main.rs+8 added-2 removed
1 use std::env;2 3 use app::get_cli_arguments;4-use benchmark::schedule::run_benchmarks_and_print_comparison;4+use benchmark::scheduler::Scheduler;5 use command::Commands;6 use export::ExportManager;7 use options::Options;8 22 unmodified lines31 let options = Options::from_cli_arguments(&cli_arguments)?;32 let commands = Commands::from_cli_arguments(&cli_arguments)?;33 let export_manager = ExportManager::from_cli_arguments(&cli_arguments)?;34 35- run_benchmarks_and_print_comparison(&commands, &options, &export_manager)35+ options.validate_against_command_list(&commands)?;36+37+ let mut scheduler = Scheduler::new(&commands, &options, &export_manager);38+ scheduler.run_benchmarks()?;39+ scheduler.print_relative_speed_comparison();40+41+ Ok(())42 }43 44 fn main() {45 match run() {7 unmodified linesmodified:src/options.rs+16 added
1 use std::process::Command;2 use std::{cmp, fmt};3 4+use anyhow::ensure;5 use atty::Stream;6 use clap::ArgMatches;7 8+use crate::command::Commands;9 use crate::error::OptionsError;10 use crate::util::units::{Second, Unit};11 12+use anyhow::Result;13+14 #[cfg(not(windows))]15 pub const DEFAULT_SHELL: &str = "sh";16 17 #[cfg(windows)]241 unmodified lines259 };260 261 Ok(options)262 }263+264+ pub fn validate_against_command_list(&self, commands: &Commands) -> Result<()> {265+ if let Some(preparation_command) = &self.preparation_command {266+ ensure!(267+ preparation_command.len() <= 1 || commands.len() == preparation_command.len(),268+ "The '--prepare' option has to be provided just once or N times, where N is the \269+ number of benchmark commands."270+ );271+ }272+273+ Ok(())274+ }275 }276 277 #[test]278 fn test_shell_default_command() {44 unmodified linesmodified:tests/integration_tests.rs+1 added
42 unmodified lines43 .assert()44 .success();45 46 hyperfine()47+ .arg("--runs=1")48 .arg("--prepare=echo a")49 .arg("--prepare=echo b")50 .arg("echo a")51 .arg("echo b")52 unmodified lines