Add export manager and CSV export capability
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Steve Pentland · committed by David Peter · parent ab4dbe9 · Browse files · 7 files·+260 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+260 added−3 removed
- other:+260 added−3 removed
Provenance
- Commit
a91912f- Tree
7f7803b- Author
Steve Pentland <stevepentland@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
ab4dbe9
7 files changed+260 added−3 removed
added:src/hyperfine/export/csv.rs+32 added
1+use super::{ExportEntry, ResultExporter};2+3+use csv::WriterBuilder;4+use std::io::Result;5+6+pub struct CsvExporter {7+ results: Vec<ExportEntry>,8+ out_file: String,9+}10+11+impl ResultExporter for CsvExporter {12+ fn add_entry(&mut self, entry: ExportEntry) {13+ self.results.push(entry);14+ }15+16+ fn write(&self) -> Result<()> {17+ let mut writer = WriterBuilder::new().from_path(&self.out_file)?;18+ for res in &self.results {19+ writer.serialize(res)?;20+ }21+ Ok(())22+ }23+}24+25+impl CsvExporter {26+ pub fn new(file_name: String) -> Self {27+ CsvExporter {28+ results: Vec::new(),29+ out_file: file_name,30+ }31+ }32+}added:src/hyperfine/export/mod.rs+96 added
1+mod csv;2+3+use self::csv::CsvExporter;4+5+use std::io::Result;6+7+/// The ExportEntry is the main set of values that will8+/// be exported to files when requested.9+#[derive(Debug, Default, Clone, Serialize)]10+pub struct ExportEntry {11+ /// The command that was run12+ command: String,13+ /// The mean run time14+ mean: f64,15+ /// The standard deviation of all run times16+ stddev: f64,17+ /// Time spend in user space18+ user: f64,19+ /// Time spent in system space20+ system: f64,21+}22+23+impl ExportEntry {24+ /// Create a new ExportEntry with the given values25+ pub fn new(command: String, mean: f64, stddev: f64, user: f64, system: f64) -> Self {26+ ExportEntry {27+ command,28+ mean,29+ stddev,30+ user,31+ system,32+ }33+ }34+}35+36+/// The ResultExportType enum is used to denote the desired form37+/// of exporter to use for a given file.38+pub enum ResultExportType {39+ /// Export to a csv file with the provided name40+ Csv(String),41+}42+43+/// A ResultExporter is responsible for writing all results to the44+/// appropriate file45+pub trait ResultExporter {46+ /// Add an additional entry to the exporter for writing47+ fn add_entry(&mut self, entry: ExportEntry);48+ /// Write all entries to the target destination49+ fn write(&self) -> Result<()>;50+}51+52+/// The ExportManager handles coordination of multiple ResultExporters53+pub trait ExportManager {54+ /// Add a new exporter to this manager for the given type55+ fn add_exporter(&mut self, for_type: &ResultExportType);56+57+ /// Add a new result to all exporters contained in the manager58+ fn add_result(&mut self, result: ExportEntry);59+60+ /// Trigger writes from all exporters61+ fn write_results(&self) -> Result<()>;62+}63+64+/// Create a new ExportManager65+pub fn create_export_manager() -> Box<ExportManager> {66+ Box::new(Exporter {67+ exporters: Vec::new(),68+ })69+}70+71+/// The Exporter is the internal implementation of the ExportManager72+struct Exporter {73+ exporters: Vec<Box<ResultExporter>>,74+}75+76+impl ExportManager for Exporter {77+ fn add_exporter(&mut self, for_type: &ResultExportType) {78+ match for_type {79+ &ResultExportType::Csv(ref file_name) => self.exporters80+ .push(Box::from(CsvExporter::new(file_name.clone()))),81+ };82+ }83+84+ fn add_result(&mut self, result: ExportEntry) {85+ for exp in self.exporters.iter_mut() {86+ exp.add_entry(result.clone());87+ }88+ }89+90+ fn write_results(&self) -> Result<()> {91+ for exp in &self.exporters {92+ exp.write()?;93+ }94+ Ok(())95+ }96+}modified:src/hyperfine/benchmark.rs+7 added
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};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<()> {142 println!(143 "{}{}: {}",144 "Benchmark #".bold(),121 unmodified lines266 267 // Warnings268 let mut warnings = vec![];269 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+275 // Check execution time276 if times_real.iter().any(|&t| t < MIN_EXECUTION_TIME) {277 warnings.push(Warnings::FastExecutionTime);278 }26 unmodified linesmodified:src/hyperfine/mod.rs+1 added
3 unmodified lines4 pub mod outlier_detection;5 pub mod warnings;6 pub mod timer;7 pub mod shell;8+pub mod export;modified:src/main.rs+36 added-3 removed
4 unmodified lines5 6 #[macro_use]7 extern crate clap;8 extern crate colored;9+extern crate csv;10 extern crate indicatif;11+extern crate serde;12+13+#[macro_use]14+extern crate serde_derive;15 extern crate statistical;16 17 cfg_if! {18 if #[cfg(windows)] {18 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};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 commands44-fn run(commands: &Vec<&str>, options: &HyperfineOptions) -> io::Result<()> {50+fn run(51+ commands: &Vec<&str>,52+ options: &HyperfineOptions,53+ exporter: &mut Option<Box<ExportManager>>,54+) -> io::Result<()> {55 let shell_spawning_time = mean_shell_spawning_time(&options.output_style)?;56 57 // Run the benchmarks58 for (num, cmd) in commands.iter().enumerate() {49- run_benchmark(num, cmd, shell_spawning_time, options)?;59+ run_benchmark(num, cmd, shell_spawning_time, options, exporter)?;60 }61 62 Ok(())63 }74 unmodified lines138 .long("ignore-failure")139 .short("i")140 .help("Ignore non-zero exit codes."),141 )142+ .arg(143+ Arg::with_name("export-csv")144+ .long("export-csv")145+ .takes_value(true)146+ .value_name("FILE")147+ .help("Export the timing results to the given file in csv format."),148+ )149 .help_message("Print this help message.")150 .version_message("Show version information.")151 .get_matches();152 21 unmodified lines174 OutputStyleOption::Basic175 },176 };177 178+ // Initial impl since we're only doing csv files, expand once we have multiple179+ // export types. Simplest probably to do the same for each, but check for180+ // None manager on later additions (JSON, Markdown, etc.)181+ let mut export_manager = match matches.value_of("export-csv") {182+ Some(filename) => {183+ let mut export_manager = create_export_manager();184+ export_manager.add_exporter(&ResultExportType::Csv(filename.to_string()));185+ Some(export_manager)186+ }187+ None => None,188+ };189+190 // We default Windows to NoColor if full had been specified.191 if cfg!(windows) && options.output_style == OutputStyleOption::Full {192 options.output_style = OutputStyleOption::NoColor;193 }6 unmodified lines200 options.failure_action = CmdFailureAction::Ignore;201 }202 203 let commands = matches.values_of("command").unwrap().collect();175- let res = run(&commands, &options);204+ let res = run(&commands, &options, &mut export_manager);205 206 if let Err(e) = res {207 error(e.description());208 }209+210+ if let Some(exporter) = export_manager {211+ exporter.write_results().unwrap();212+ }213 }modified:Cargo.lock+85 added
83 unmodified lines84 "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",85 ]86 87 [[package]]88+name = "csv"89+version = "1.0.0-beta.5"90+source = "registry+https://github.com/rust-lang/crates.io-index"91+dependencies = [92+ "csv-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",93+ "serde 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",94+]95+96+[[package]]97+name = "csv-core"98+version = "0.1.4"99+source = "registry+https://github.com/rust-lang/crates.io-index"100+dependencies = [101+ "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",102+]103+104+[[package]]105 name = "fuchsia-zircon"106 version = "0.3.3"107 source = "registry+https://github.com/rust-lang/crates.io-index"108 dependencies = [14 unmodified lines123 "atty 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",124 "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",125 "clap 2.30.0 (registry+https://github.com/rust-lang/crates.io-index)",126 "colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)",127+ "csv 1.0.0-beta.5 (registry+https://github.com/rust-lang/crates.io-index)",128 "indicatif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",129 "libc 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",130+ "serde 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",131+ "serde_derive 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",132 "statistical 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",133 "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",134 ]135 134 unmodified lines270 "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",271 ]272 273 [[package]]274+name = "proc-macro2"275+version = "0.2.3"276+source = "registry+https://github.com/rust-lang/crates.io-index"277+dependencies = [278+ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",279+]280+281+[[package]]282+name = "quote"283+version = "0.4.2"284+source = "registry+https://github.com/rust-lang/crates.io-index"285+dependencies = [286+ "proc-macro2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",287+]288+289+[[package]]290 name = "rand"291 version = "0.3.22"292 source = "registry+https://github.com/rust-lang/crates.io-index"293 dependencies = [47 unmodified lines341 version = "0.3.24"342 source = "registry+https://github.com/rust-lang/crates.io-index"343 344 [[package]]345+name = "serde"346+version = "1.0.33"347+source = "registry+https://github.com/rust-lang/crates.io-index"348+349+[[package]]350+name = "serde_derive"351+version = "1.0.33"352+source = "registry+https://github.com/rust-lang/crates.io-index"353+dependencies = [354+ "proc-macro2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",355+ "quote 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",356+ "serde_derive_internals 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)",357+ "syn 0.12.14 (registry+https://github.com/rust-lang/crates.io-index)",358+]359+360+[[package]]361+name = "serde_derive_internals"362+version = "0.21.0"363+source = "registry+https://github.com/rust-lang/crates.io-index"364+dependencies = [365+ "proc-macro2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",366+ "syn 0.12.14 (registry+https://github.com/rust-lang/crates.io-index)",367+]368+369+[[package]]370 name = "smallvec"371 version = "0.6.0"372 source = "registry+https://github.com/rust-lang/crates.io-index"373 16 unmodified lines390 version = "0.7.0"391 source = "registry+https://github.com/rust-lang/crates.io-index"392 393 [[package]]394+name = "syn"395+version = "0.12.14"396+source = "registry+https://github.com/rust-lang/crates.io-index"397+dependencies = [398+ "proc-macro2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",399+ "quote 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",400+ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",401+]402+403+[[package]]404 name = "term_size"405 version = "0.3.1"406 source = "registry+https://github.com/rust-lang/crates.io-index"407 dependencies = [43 unmodified lines451 version = "0.1.4"452 source = "registry+https://github.com/rust-lang/crates.io-index"453 454 [[package]]455+name = "unicode-xid"456+version = "0.1.0"457+source = "registry+https://github.com/rust-lang/crates.io-index"458+459+[[package]]460 name = "unreachable"461 version = "1.0.0"462 source = "registry+https://github.com/rust-lang/crates.io-index"463 dependencies = [49 unmodified lines513 "checksum clap 2.30.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1c07b9257a00f3fc93b7f3c417fc15607ec7a56823bc2c37ec744e266387de5b"514 "checksum clicolors-control 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1f84dec9bc083ce2503908cd305af98bd363da6f54bf8d4bf0ac14ee749ad5d1"515 "checksum colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b0aa3473e85a3161b59845d6096b289bb577874cafeaf75ea1b1beaa6572c7fc"516 "checksum console 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7649ca90478264b9686aac8d269fcb014f14c2bed7c79a7e51b9f6afd4d783eb"517+"checksum csv 1.0.0-beta.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e7a9e063dcebdb56c306f23e672bfd31df3da8ec5f6d696b35f2c29c2a9572f0"518+"checksum csv-core 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4dd8e6d86f7ba48b4276ef1317edc8cc36167546d8972feb4a2b5fec0b374105"519 "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"520 "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"521 "checksum indicatif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a29b2fa6f00010c268bface64c18bb0310aaa70d46a195d5382d288c477fb016"522 "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"10 unmodified lines533 "checksum num-traits 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3c2bd9b9d21e48e956b763c9f37134dc62d9e95da6edb3f672cacb6caf3cd3"534 "checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"535 "checksum parking_lot 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "9fd9d732f2de194336fb02fe11f9eed13d9e76f13f4315b4d88a14ca411750cd"536 "checksum parking_lot_core 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "538ef00b7317875071d5e00f603f24d16f0b474c1a5fc0ccb8b454ca72eafa79"537+"checksum proc-macro2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cd07deb3c6d1d9ff827999c7f9b04cdfd66b1b17ae508e14fe47b620f2282ae0"538+"checksum quote 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1eca14c727ad12702eb4b6bfb5a232287dcf8385cb8ca83a3eeaf6519c44c408"539 "checksum rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "15a732abf9d20f0ad8eeb6f909bf6868722d9a06e1e50802b6a70351f40b4eb1"540 "checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5"541 "checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd"542 "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"543 "checksum regex 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "5be5347bde0c48cfd8c3fdc0766cdfe9d8a755ef84d620d6794c778c91de8b2b"544 "checksum regex-syntax 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8e931c58b93d86f080c734bfd2bce7dd0079ae2331235818133c8be7f422e20e"545 "checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"546+"checksum serde 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)" = "4fe95aa0d46f04ce5c3a88bdcd4114ecd6144ed0b2725ebca2f1127744357807"547+"checksum serde_derive 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)" = "23b163a6ce7e1aa897919f9d8e40bd1f8a6f95342ed57727ae31387a01a7a356"548+"checksum serde_derive_internals 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)" = "370aa477297975243dc914d0b0e1234927520ec311de507a560fbd1c80f7ab8c"549 "checksum smallvec 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44db0ecb22921ef790d17ae13a3f6d15784183ff5f2a01aa32098c7498d2b4b9"550 "checksum stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "15132e0e364248108c5e2c02e3ab539be8d6f5d52a01ca9bbf27ed657316f02b"551 "checksum statistical 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c139942f46d96c53b28420a2cdfb374629f122656bd9daef7fc221ed4d8ec228"552 "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"553+"checksum syn 0.12.14 (registry+https://github.com/rust-lang/crates.io-index)" = "8c5bc2d6ff27891209efa5f63e9de78648d7801f085e4653701a692ce938d6fd"554 "checksum term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9e5b9a66db815dcfd2da92db471106457082577c3c278d4138ab3e3b4e189327"555 "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"556 "checksum termios 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d9cf598a6d7ce700a4e6a9199da127e6819a61e64b68609683cc9a01b5683a"557 "checksum textwrap 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c0b59b6b4b44d867f1370ef1bd91bfb262bf07bf0ae65c202ea2fbc16153b693"558 "checksum thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "279ef31c19ededf577bfd12dfae728040a21f635b06a24cd670ff510edd38963"559 "checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"560+"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"561 "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"562 "checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"563 "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"564 "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"4 unmodified linesmodified:Cargo.toml+3 added
13 unmodified lines141516171819202122232410 unmodified lines13 unmodified linesindicatif = "0.9"statistical = "0.1"atty = "0.2.2"cfg-if = "0.1.2"csv = "1.0.0-beta.5"serde = "1.0.33"serde_derive = "1.0.33"
[target.'cfg(not(windows))'.dependencies]libc = "0.2"
10 unmodified lines