scheduler.rs
This file
Path
src/benchmark/scheduler.rs
156 lines · 4.2 KiB · Rust
Last changed by
Export selected metrics side by side in CSVDavid Peterbefore adoption067ee70
History of this fileTake it
1use super::benchmark_result::BenchmarkResult;2use super::executor::{Executor, MockExecutor, RawExecutor, ShellExecutor};3use super::Benchmark;45use crate::command::Commands;6use crate::export::ExportManager;7use crate::options::{ExecutorKind, Options};89use anyhow::Result;1011pub struct Scheduler<'a> {12 commands: &'a Commands<'a>,13 options: &'a Options,14 export_manager: &'a ExportManager,15 results: Vec<BenchmarkResult>,16}1718impl<'a> Scheduler<'a> {19 pub fn new(20 commands: &'a Commands,21 options: &'a Options,22 export_manager: &'a ExportManager,23 ) -> Self {24 Self {25 commands,26 options,27 export_manager,28 results: vec![],29 }30 }3132 pub fn run_benchmarks(&mut self) -> Result<()> {33 let mut executor: Box<dyn Executor> = match self.options.executor_kind {34 ExecutorKind::Raw => Box::new(RawExecutor::new(self.options)),35 ExecutorKind::Mock(ref shell) => Box::new(MockExecutor::new(shell.clone())),36 ExecutorKind::Shell(ref shell) => Box::new(ShellExecutor::new(shell, self.options)),37 };3839 executor.calibrate()?;4041 for (number, cmd) in self.commands.iter().enumerate() {42 self.results.push(43 Benchmark::new(number, cmd, self.options, &*executor).run(self.results.first())?,44 );4546 // We export results after each individual benchmark, because47 // we would risk losing them if a later benchmark fails.48 self.export_manager.write_results(&self.results, true)?;49 }5051 Ok(())52 }5354 pub fn final_export(&self) -> Result<()> {55 self.export_manager.write_results(&self.results, false)56 }57}5859#[cfg(test)]60fn generate_results(args: &[&'static str]) -> Result<Vec<BenchmarkResult>> {61 use crate::cli::get_cli_arguments;6263 let args = ["hyperfine", "--debug-mode", "--style=none"]64 .iter()65 .chain(args);66 let cli_arguments = get_cli_arguments(args);67 let mut options = Options::from_cli_arguments(&cli_arguments)?;6869 assert_eq!(options.executor_kind, ExecutorKind::Mock(None));7071 let commands = Commands::from_cli_arguments(&cli_arguments)?;72 let export_manager = ExportManager::from_cli_arguments(&cli_arguments, &options.metrics)?;7374 options.validate_against_command_list(&commands)?;7576 let mut scheduler = Scheduler::new(&commands, &options, &export_manager);7778 scheduler.run_benchmarks()?;79 Ok(scheduler.results)80}8182#[test]83fn scheduler_basic() -> Result<()> {84 insta::assert_yaml_snapshot!(generate_results(&["--runs=2", "sleep 0.123", "sleep 0.456"])?, @"85 - command: sleep 0.12386 measurements:87 - time_wall_clock:88 value: 0.12389 unit: second90 time_cpu:91 value: 092 unit: second93 time_user:94 value: 095 unit: second96 time_system:97 value: 098 unit: second99 memory_peak_resident:100 value: 0101 unit: byte102 exit_code: 0103 - time_wall_clock:104 value: 0.123105 unit: second106 time_cpu:107 value: 0108 unit: second109 time_user:110 value: 0111 unit: second112 time_system:113 value: 0114 unit: second115 memory_peak_resident:116 value: 0117 unit: byte118 exit_code: 0119 - command: sleep 0.456120 measurements:121 - time_wall_clock:122 value: 0.456123 unit: second124 time_cpu:125 value: 0126 unit: second127 time_user:128 value: 0129 unit: second130 time_system:131 value: 0132 unit: second133 memory_peak_resident:134 value: 0135 unit: byte136 exit_code: 0137 - time_wall_clock:138 value: 0.456139 unit: second140 time_cpu:141 value: 0142 unit: second143 time_user:144 value: 0145 unit: second146 time_system:147 value: 0148 unit: second149 memory_peak_resident:150 value: 0151 unit: byte152 exit_code: 0153 ");154155 Ok(())156}