Skip to content
Myr

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

7 files+260 added−3 removed

added:src/hyperfine/export/csv.rs+32 added
1use super::{ExportEntry, ResultExporter};23use csv::WriterBuilder;4use std::io::Result;56pub struct CsvExporter {7 results: Vec<ExportEntry>,8 out_file: String,9}1011impl ResultExporter for CsvExporter {12 fn add_entry(&mut self, entry: ExportEntry) {13 self.results.push(entry);14 }1516 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}2425impl 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
1mod csv;23use self::csv::CsvExporter;45use std::io::Result;67/// The ExportEntry is the main set of values that will8/// be exported to files when requested.9#[derive(Debug, Default, Clone, Serialize)]10pub 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}2223impl 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}3536/// The ResultExportType enum is used to denote the desired form37/// of exporter to use for a given file.38pub enum ResultExportType {39 /// Export to a csv file with the provided name40 Csv(String),41}4243/// A ResultExporter is responsible for writing all results to the44/// appropriate file45pub 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}5152/// The ExportManager handles coordination of multiple ResultExporters53pub trait ExportManager {54 /// Add a new exporter to this manager for the given type55 fn add_exporter(&mut self, for_type: &ResultExportType);5657 /// Add a new result to all exporters contained in the manager58 fn add_result(&mut self, result: ExportEntry);5960 /// Trigger writes from all exporters61 fn write_results(&self) -> Result<()>;62}6364/// Create a new ExportManager65pub fn create_export_manager() -> Box<ExportManager> {66 Box::new(Exporter {67 exporters: Vec::new(),68 })69}7071/// The Exporter is the internal implementation of the ExportManager72struct Exporter {73 exporters: Vec<Box<ResultExporter>>,74}7576impl 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 }8384 fn add_result(&mut self, result: ExportEntry) {85 for exp in self.exporters.iter_mut() {86 exp.add_entry(result.clone());87 }88 }8990 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 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};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<()> {142 println!(143 "{}{}: {}",144 "Benchmark #".bold(),
121 unmodified lines
266267 // Warnings268 let mut warnings = vec![];269270 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 }274275 // Check execution time276 if times_real.iter().any(|&t| t < MIN_EXECUTION_TIME) {277 warnings.push(Warnings::FastExecutionTime);278 }
26 unmodified lines
modified:src/hyperfine/mod.rs+1 added
3 unmodified lines
4pub mod outlier_detection;5pub mod warnings;6pub mod timer;7pub mod shell;8pub mod export;
modified:src/main.rs+36 added-3 removed
4 unmodified lines
56#[macro_use]7extern crate clap;8extern crate colored;9extern crate csv;10extern crate indicatif;11extern crate serde;1213#[macro_use]14extern crate serde_derive;15extern crate statistical;1617cfg_if! {18 if #[cfg(windows)] {
18 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};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 commands44fn run(commands: &Vec<&str>, options: &HyperfineOptions) -> io::Result<()> {50fn 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)?;5657 // 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 }6162 Ok(())63}
74 unmodified lines
138 .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 lines
174 OutputStyleOption::Basic175 },176 };177178 // 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 };189190 // 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 lines
200 options.failure_action = CmdFailureAction::Ignore;201 }202203 let commands = matches.values_of("command").unwrap().collect();175 let res = run(&commands, &options);204 let res = run(&commands, &options, &mut export_manager);205206 if let Err(e) = res {207 error(e.description());208 }209210 if let Some(exporter) = export_manager {211 exporter.write_results().unwrap();212 }213}
modified:Cargo.lock+85 added
83 unmodified lines
84 "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",85]8687[[package]]88name = "csv"89version = "1.0.0-beta.5"90source = "registry+https://github.com/rust-lang/crates.io-index"91dependencies = [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]9596[[package]]97name = "csv-core"98version = "0.1.4"99source = "registry+https://github.com/rust-lang/crates.io-index"100dependencies = [101 "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",102]103104[[package]]105name = "fuchsia-zircon"106version = "0.3.3"107source = "registry+https://github.com/rust-lang/crates.io-index"108dependencies = [
14 unmodified lines
123 "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 lines
270 "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",271]272273[[package]]274name = "proc-macro2"275version = "0.2.3"276source = "registry+https://github.com/rust-lang/crates.io-index"277dependencies = [278 "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",279]280281[[package]]282name = "quote"283version = "0.4.2"284source = "registry+https://github.com/rust-lang/crates.io-index"285dependencies = [286 "proc-macro2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",287]288289[[package]]290name = "rand"291version = "0.3.22"292source = "registry+https://github.com/rust-lang/crates.io-index"293dependencies = [
47 unmodified lines
341version = "0.3.24"342source = "registry+https://github.com/rust-lang/crates.io-index"343344[[package]]345name = "serde"346version = "1.0.33"347source = "registry+https://github.com/rust-lang/crates.io-index"348349[[package]]350name = "serde_derive"351version = "1.0.33"352source = "registry+https://github.com/rust-lang/crates.io-index"353dependencies = [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]359360[[package]]361name = "serde_derive_internals"362version = "0.21.0"363source = "registry+https://github.com/rust-lang/crates.io-index"364dependencies = [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]368369[[package]]370name = "smallvec"371version = "0.6.0"372source = "registry+https://github.com/rust-lang/crates.io-index"373
16 unmodified lines
390version = "0.7.0"391source = "registry+https://github.com/rust-lang/crates.io-index"392393[[package]]394name = "syn"395version = "0.12.14"396source = "registry+https://github.com/rust-lang/crates.io-index"397dependencies = [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]402403[[package]]404name = "term_size"405version = "0.3.1"406source = "registry+https://github.com/rust-lang/crates.io-index"407dependencies = [
43 unmodified lines
451version = "0.1.4"452source = "registry+https://github.com/rust-lang/crates.io-index"453454[[package]]455name = "unicode-xid"456version = "0.1.0"457source = "registry+https://github.com/rust-lang/crates.io-index"458459[[package]]460name = "unreachable"461version = "1.0.0"462source = "registry+https://github.com/rust-lang/crates.io-index"463dependencies = [
49 unmodified lines
513"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 lines
533"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 lines
modified:Cargo.toml+3 added