Skip to content
Myr

Create export file before first benchmark (#340)

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

authored by Martin Junghanns · committed by GitHub · parent 2547342 · Browse files · 3 files+22 added−12 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+22 added−12 removed

  • other:+22 added−12 removed

3 files+22 added−12 removed

modified:src/hyperfine/export/mod.rs+7 added-3 removed
6 unmodified lines
7use self::csv::CsvExporter;8use self::json::JsonExporter;9use self::markdown::MarkdownExporter;1011use std::fs::File;11use std::fs::{File, OpenOptions};12use std::io::{Result, Write};1314use crate::hyperfine::types::BenchmarkResult;15use crate::hyperfine::units::Unit;
38 unmodified lines
54 }55 }5657 /// Add an additional exporter to the ExportManager58 pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) {58 pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {59 let _ = File::create(filename)?;6061 let exporter: Box<dyn Exporter> = match export_type {62 ExportType::Asciidoc => Box::new(AsciidocExporter::default()),63 ExportType::Csv => Box::new(CsvExporter::default()),64 ExportType::Json => Box::new(JsonExporter::default()),
2 unmodified lines
67 self.exporters.push(ExporterWithFilename {68 exporter,69 filename: filename.to_string(),70 });7172 Ok(())73 }7475 /// Write the given results to all Exporters contained within this manager76 pub fn write_results(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<()> {
6 unmodified lines
83}8485/// Write the given content to a file with the specified name86fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {83 let mut file = File::create(filename)?;87 let mut file = OpenOptions::new().write(true).open(filename)?;88 file.write_all(content)89}
modified:src/main.rs+13 added-9 removed
69 unmodified lines
7071fn main() {72 let matches = get_arg_matches(env::args_os());73 let options = build_hyperfine_options(&matches);74 let export_manager = build_export_manager(&matches);74 let commands = build_commands(&matches);75 let export_manager = match build_export_manager(&matches) {76 Ok(export_manager) => export_manager,77 Err(ref e) => error(&e.to_string())78 };7980 let res = match options {81 Ok(ref opts) => run(&commands, &opts, &export_manager),82 Err(ref e) => error(&e.to_string()),
121 unmodified lines
204}205206/// Build the ExportManager that will export the results specified207/// in the given ArgMatches205fn build_export_manager(matches: &ArgMatches<'_>) -> ExportManager {208fn build_export_manager(matches: &ArgMatches<'_>) -> io::Result<ExportManager> {209 let mut export_manager = ExportManager::new();210 {208 let mut add_exporter = |flag, exporttype| {211 let mut add_exporter = |flag, exporttype| -> io::Result<()> {212 if let Some(filename) = matches.value_of(flag) {210 export_manager.add_exporter(exporttype, filename);213 export_manager.add_exporter(exporttype, filename)?;214 }215 Ok(())216 };213 add_exporter("export-asciidoc", ExportType::Asciidoc);214 add_exporter("export-json", ExportType::Json);215 add_exporter("export-csv", ExportType::Csv);216 add_exporter("export-markdown", ExportType::Markdown);217 add_exporter("export-asciidoc", ExportType::Asciidoc)?;218 add_exporter("export-json", ExportType::Json)?;219 add_exporter("export-csv", ExportType::Csv)?;220 add_exporter("export-markdown", ExportType::Markdown)?;221 }218 export_manager222 Ok(export_manager)223}224225/// Build the commands to benchmark226fn build_commands<'a>(matches: &'a ArgMatches<'_>) -> Vec<Command<'a>> {
118 unmodified lines
modified:CHANGELOG.md+2 added
2 unmodified lines
3## Features45## Changes67- When `--export-*` commands are used, result files are created before benchmark execution8 to fail early in case of, e.g., wrong permissions. See #306 (@s1ck). 9- When `--export-*` options are used, result files are written after each individual 10 benchmark command instead of writing after all benchmarks have finished. See #306 (@s1ck).1112## Bugfixes
250 unmodified lines