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
Provenance
- Commit
7c72777- Tree
7a52b10- Author
Steve Pentland <stevepentland@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
c4688a7
3 files changed+40 added−61 removed
modified:src/hyperfine/export/mod.rs+14 added-30 removed
2 unmodified lines3 use self::csv::CsvExporter;4 5 use std::io::Result;6 7+use hyperfine::internal::Second;8+9 /// The ExportEntry is the main set of values that will10 /// be exported to files when requested.11 #[derive(Debug, Default, Clone, Serialize)]12 pub 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 }24 25 impl ExportEntry {26 /// Create a new ExportEntry with the given values21 unmodified lines48 /// Write all entries to the target destination49 fn write(&self, values: &Vec<ExportEntry>) -> Result<()>;50 }51 50-/// The ExportManager handles coordination of multiple ResultExporters51-pub trait ExportManager {52- /// Add a new exporter to this manager for the given type53- fn add_exporter(&mut self, for_type: &ResultExportType);54-55- /// Add a new result to all exporters contained in the manager56- fn add_result(&mut self, result: ExportEntry);57-58- /// Trigger writes from all exporters59- fn write_results(&self) -> Result<()>;60-}61-52 /// Create a new ExportManager63-pub fn create_export_manager() -> Box<ExportManager> {64- Box::new(Exporter {53+pub fn create_export_manager() -> ExportManager {54+ ExportManager {55 exporters: Vec::new(),66- results: Vec::new(),67- })56+ }57 }58 59 /// The Exporter is the internal implementation of the ExportManager71-struct Exporter {60+pub struct ExportManager {61 exporters: Vec<Box<ResultExporter>>,73- results: Vec<ExportEntry>,62 }63 76-impl ExportManager for Exporter {77- fn add_exporter(&mut self, for_type: &ResultExportType) {64+impl 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 }71 84- fn add_result(&mut self, result: ExportEntry) {85- self.results.push(result);86- }87-88- 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 lines10 use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};11 use hyperfine::timer::{TimerStart, TimerStop};12 use hyperfine::timer::wallclocktimer::WallClockTimer;13 use hyperfine::shell::execute_and_time;14-use hyperfine::export::{ExportEntry, ExportManager};14+use hyperfine::export::ExportEntry;15 16 /// Results from timing a single shell command17 #[derive(Debug, Default, Copy, Clone)]18 pub struct TimingResult {117 unmodified lines136 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 lines265 266 // Warnings267 let mut warnings = vec![];268 270- 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- }274-269 // Check execution time270 if times_real.iter().any(|&t| t < MIN_EXECUTION_TIME) {271 warnings.push(Warnings::FastExecutionTime);272 }20 unmodified lines293 }294 295 println!(" ");296 303- 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 lines37 mod hyperfine;38 39 use hyperfine::internal::{CmdFailureAction, HyperfineOptions, OutputStyleOption};40 use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};41-use hyperfine::export::{create_export_manager, ExportManager, ResultExportType};41+use hyperfine::export::{create_export_manager, ExportEntry, ResultExportType};42 43 /// Print error message to stderr and terminate44 pub fn error(message: &str) -> ! {45 eprintln!("{} {}", "Error:".red(), message);46 std::process::exit(1);47 }48 49 /// Runs the benchmark for the given commands50-fn run(51- commands: &Vec<&str>,52- options: &HyperfineOptions,53- exporter: &mut Option<Box<ExportManager>>,54-) -> io::Result<()> {50+fn run(commands: &Vec<&str>, options: &HyperfineOptions) -> io::Result<Vec<ExportEntry>> {51 let shell_spawning_time = mean_shell_spawning_time(&options.output_style)?;52 53+ let mut timing_results = vec![];54+55 // 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 }59 62- Ok(())60+ Ok(timing_results)61 }62 63 fn main() {64 // Process command line options75 unmodified lines140 .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 lines175 176 // 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 lines198 options.failure_action = CmdFailureAction::Ignore;199 }200 201 let commands = matches.values_of("command").unwrap().collect();208- let res = run(&commands, &options, &mut export_manager);202+ let res = run(&commands, &options);203 210- 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 }213-214- if let Some(exporter) = export_manager {215- exporter.write_results().unwrap();216- }212 }