Skip to content
Myr

Change the structure of everything to remove Manager trait.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Manager is now a simple struct, that no longer stores the results, but instead takes in a Vec<ExportEntry> and has all exporters write from this collection. Benchmark and run both return their results, no more mut manager. Change f64 in ExportEntry to hyperfine::internal::Second. Remove multiple export files for a single type.

authored by Steve Pentland · committed by David Peter · parent c4688a7 · Browse files · 3 files+40 added−61 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+40 added−61 removed

  • other:+40 added−61 removed

3 files+40 added−61 removed

modified:src/hyperfine/export/mod.rs+14 added-30 removed
2 unmodified lines
3use self::csv::CsvExporter;45use std::io::Result;67use hyperfine::internal::Second;89/// The ExportEntry is the main set of values that will10/// be exported to files when requested.11#[derive(Debug, Default, Clone, Serialize)]12pub struct ExportEntry {13 /// The command that was run14 command: String,15 /// The mean run time14 mean: f64,16 mean: Second,17 /// The standard deviation of all run times16 stddev: f64,18 stddev: Second,19 /// Time spend in user space18 user: f64,20 user: Second,21 /// Time spent in system space20 system: f64,22 system: Second,23}2425impl ExportEntry {26 /// Create a new ExportEntry with the given values
21 unmodified lines
48 /// Write all entries to the target destination49 fn write(&self, values: &Vec<ExportEntry>) -> Result<()>;50}5150/// The ExportManager handles coordination of multiple ResultExporters51pub trait ExportManager {52 /// Add a new exporter to this manager for the given type53 fn add_exporter(&mut self, for_type: &ResultExportType);5455 /// Add a new result to all exporters contained in the manager56 fn add_result(&mut self, result: ExportEntry);5758 /// Trigger writes from all exporters59 fn write_results(&self) -> Result<()>;60}6152/// Create a new ExportManager63pub fn create_export_manager() -> Box<ExportManager> {64 Box::new(Exporter {53pub fn create_export_manager() -> ExportManager {54 ExportManager {55 exporters: Vec::new(),66 results: Vec::new(),67 })56 }57}5859/// The Exporter is the internal implementation of the ExportManager71struct Exporter {60pub struct ExportManager {61 exporters: Vec<Box<ResultExporter>>,73 results: Vec<ExportEntry>,62}6376impl ExportManager for Exporter {77 fn add_exporter(&mut self, for_type: &ResultExportType) {64impl ExportManager {65 pub fn add_exporter(&mut self, for_type: &ResultExportType) {66 match for_type {67 &ResultExportType::Csv(ref file_name) => self.exporters68 .push(Box::from(CsvExporter::new(file_name.clone()))),69 };70 }7184 fn add_result(&mut self, result: ExportEntry) {85 self.results.push(result);86 }8788 fn write_results(&self) -> Result<()> {72 pub fn write_results(&self, to_write: Vec<ExportEntry>) -> Result<()> {73 for exp in &self.exporters {90 exp.write(&self.results)?;74 exp.write(&to_write)?;75 }76 Ok(())77 }78}
modified:src/hyperfine/benchmark.rs+9 added-9 removed
9 unmodified lines
10use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};11use hyperfine::timer::{TimerStart, TimerStop};12use hyperfine::timer::wallclocktimer::WallClockTimer;13use hyperfine::shell::execute_and_time;14use hyperfine::export::{ExportEntry, ExportManager};14use hyperfine::export::ExportEntry;1516/// Results from timing a single shell command17#[derive(Debug, Default, Copy, Clone)]18pub struct TimingResult {
117 unmodified lines
136 num: usize,137 cmd: &str,138 shell_spawning_time: TimingResult,139 options: &HyperfineOptions,140 exporter: &mut Option<Box<ExportManager>>,141) -> io::Result<()> {140) -> io::Result<ExportEntry> {141 println!(142 "{}{}: {}",143 "Benchmark #".bold(),144 (num + 1).to_string().bold(),
120 unmodified lines
265266 // Warnings267 let mut warnings = vec![];268270 if let &mut Some(ref mut mgr) = exporter {271 let entry = ExportEntry::new(cmd.to_string(), t_mean, t_stddev, user_mean, system_mean);272 mgr.add_result(entry);273 }274269 // Check execution time270 if times_real.iter().any(|&t| t < MIN_EXECUTION_TIME) {271 warnings.push(Warnings::FastExecutionTime);272 }
20 unmodified lines
293 }294295 println!(" ");296303 Ok(())297 Ok(ExportEntry::new(298 cmd.to_string(),299 t_mean,300 t_stddev,301 user_mean,302 system_mean,303 ))304}
modified:src/main.rs+17 added-22 removed
36 unmodified lines
37mod hyperfine;3839use hyperfine::internal::{CmdFailureAction, HyperfineOptions, OutputStyleOption};40use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};41use hyperfine::export::{create_export_manager, ExportManager, ResultExportType};41use hyperfine::export::{create_export_manager, ExportEntry, ResultExportType};4243/// Print error message to stderr and terminate44pub fn error(message: &str) -> ! {45 eprintln!("{} {}", "Error:".red(), message);46 std::process::exit(1);47}4849/// Runs the benchmark for the given commands50fn run(51 commands: &Vec<&str>,52 options: &HyperfineOptions,53 exporter: &mut Option<Box<ExportManager>>,54) -> io::Result<()> {50fn run(commands: &Vec<&str>, options: &HyperfineOptions) -> io::Result<Vec<ExportEntry>> {51 let shell_spawning_time = mean_shell_spawning_time(&options.output_style)?;5253 let mut timing_results = vec![];5455 // Run the benchmarks56 for (num, cmd) in commands.iter().enumerate() {59 run_benchmark(num, cmd, shell_spawning_time, options, exporter)?;57 timing_results.push(run_benchmark(num, cmd, shell_spawning_time, options)?);58 }5962 Ok(())60 Ok(timing_results)61}6263fn main() {64 // Process command line options
75 unmodified lines
140 .arg(141 Arg::with_name("export-csv")142 .long("export-csv")143 .takes_value(true)146 .multiple(true)147 .number_of_values(1)144 .value_name("FILE")145 .help("Export the timing results to the given file in csv format."),146 )147 .help_message("Print this help message.")
27 unmodified lines
175176 // Initial impl since we're only doing csv files, expand once we have multiple177 // export types. Simplest probably to do the same for each, but check for178 // None manager on later additions (JSON, Markdown, etc.)183 let mut export_manager = match matches.values_of("export-csv") {184 Some(filenames) => {179 let export_manager = match matches.value_of("export-csv") {180 Some(filename) => {181 let mut export_manager = create_export_manager();186 for filename in filenames {187 export_manager.add_exporter(&ResultExportType::Csv(filename.to_string()));182 export_manager.add_exporter(&ResultExportType::Csv(filename.to_string()));188 }183 Some(export_manager)184 }185 None => None,186 };
11 unmodified lines
198 options.failure_action = CmdFailureAction::Ignore;199 }200201 let commands = matches.values_of("command").unwrap().collect();208 let res = run(&commands, &options, &mut export_manager);202 let res = run(&commands, &options);203210 if let Err(e) = res {211 error(e.description());204 match res {205 Ok(timing_results) => {206 if let Some(mut exporter) = export_manager {207 exporter.write_results(timing_results).unwrap();208 }209 }210 Err(e) => error(e.description()),211 }213214 if let Some(exporter) = export_manager {215 exporter.write_results().unwrap();216 }212}