Skip to content
Myr

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

6 files+118 added−84 removed

modified:src/benchmark/mod.rs+1 added-1 removed
1pub mod relative_speed;2pub mod result;3pub mod schedule;3pub mod scheduler;45use std::cmp;6use std::process::{ExitStatus, Stdio};7
513 unmodified lines
deleted:src/benchmark/schedule.rs-81 removed
1use crate::command::Commands;2use crate::export::ExportManager;3use crate::options::{Options, OutputStyleOption};45use super::{mean_shell_spawning_time, relative_speed, result::BenchmarkResult, run_benchmark};67use anyhow::{bail, Result};8use colored::*;910pub fn write_benchmark_comparison(results: &[BenchmarkResult]) {11 if results.len() < 2 {12 return;13 }1415 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));1718 let fastest = &annotated_results[0];19 let others = &annotated_results[1..];2021 println!("{}", "Summary".bold());22 println!(" '{}' ran", fastest.result.command.cyan());2324 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}4748pub 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)?;5556 let mut timing_results = vec![];5758 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 }6667 // Run the benchmarks68 for (num, cmd) in commands.iter().enumerate() {69 timing_results.push(run_benchmark(num, cmd, shell_spawning_time, options)?);7071 // Export (intermediate) results72 export_manager.write_results(&timing_results, options.time_unit)?;73 }7475 // Print relative speed comparison76 if options.output_style != OutputStyleOption::Disabled {77 write_benchmark_comparison(&timing_results);78 }7980 Ok(())81}
added:src/benchmark/scheduler.rs+92 added
1use crate::command::Commands;2use crate::export::ExportManager;3use crate::options::{Options, OutputStyleOption};45use super::{mean_shell_spawning_time, relative_speed, result::BenchmarkResult, run_benchmark};67use anyhow::Result;8use colored::*;910pub struct Scheduler<'a> {11 commands: &'a Commands<'a>,12 options: &'a Options,13 export_manager: &'a ExportManager,14 results: Vec<BenchmarkResult>,15}1617impl<'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 }3031 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 )?;3738 // Run the benchmarks39 for (num, cmd) in self.commands.iter().enumerate() {40 self.results41 .push(run_benchmark(num, cmd, shell_spawning_time, self.options)?);4243 // Export (intermediate) results44 self.export_manager45 .write_results(&self.results, self.options.time_unit)?;46 }4748 Ok(())49 }5051 pub fn print_relative_speed_comparison(&self) {52 if self.options.output_style == OutputStyleOption::Disabled {53 return;54 }5556 if self.results.len() < 2 {57 return;58 }5960 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));6263 let fastest = &annotated_results[0];64 let others = &annotated_results[1..];6566 println!("{}", "Summary".bold());67 println!(" '{}' ran", fastest.result.command.cyan());6869 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
1use std::env;23use app::get_cli_arguments;4use benchmark::schedule::run_benchmarks_and_print_comparison;4use benchmark::scheduler::Scheduler;5use command::Commands;6use export::ExportManager;7use options::Options;8
22 unmodified lines
31 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)?;3435 run_benchmarks_and_print_comparison(&commands, &options, &export_manager)35 options.validate_against_command_list(&commands)?;3637 let mut scheduler = Scheduler::new(&commands, &options, &export_manager);38 scheduler.run_benchmarks()?;39 scheduler.print_relative_speed_comparison();4041 Ok(())42}4344fn main() {45 match run() {
7 unmodified lines
modified:src/options.rs+16 added
1use std::process::Command;2use std::{cmp, fmt};34use anyhow::ensure;5use atty::Stream;6use clap::ArgMatches;78use crate::command::Commands;9use crate::error::OptionsError;10use crate::util::units::{Second, Unit};1112use anyhow::Result;1314#[cfg(not(windows))]15pub const DEFAULT_SHELL: &str = "sh";1617#[cfg(windows)]
241 unmodified lines
259 };260261 Ok(options)262 }263264 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 }272273 Ok(())274 }275}276277#[test]278fn test_shell_default_command() {
44 unmodified lines
modified:tests/integration_tests.rs+1 added
42 unmodified lines
43 .assert()44 .success();4546 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