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
Provenance
- Commit
990ed45- Tree
1ea24bf- Author
Martin Junghanns <martin.junghanns@neo4j.com>- Committer
GitHub <noreply@github.com>- Parent
2547342
3 files changed+22 added−12 removed
modified:src/hyperfine/export/mod.rs+7 added-3 removed
6 unmodified lines7 use self::csv::CsvExporter;8 use self::json::JsonExporter;9 use self::markdown::MarkdownExporter;10 11-use std::fs::File;11+use std::fs::{File, OpenOptions};12 use std::io::{Result, Write};13 14 use crate::hyperfine::types::BenchmarkResult;15 use crate::hyperfine::units::Unit;38 unmodified lines54 }55 }56 57 /// 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)?;60+61 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 lines67 self.exporters.push(ExporterWithFilename {68 exporter,69 filename: filename.to_string(),70 });71+72+ Ok(())73 }74 75 /// Write the given results to all Exporters contained within this manager76 pub fn write_results(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<()> {6 unmodified lines83 }84 85 /// Write the given content to a file with the specified name86 fn 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 lines70 71 fn 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+ };79 80 let res = match options {81 Ok(ref opts) => run(&commands, &opts, &export_manager),82 Err(ref e) => error(&e.to_string()),121 unmodified lines204 }205 206 /// Build the ExportManager that will export the results specified207 /// in the given ArgMatches205-fn build_export_manager(matches: &ArgMatches<'_>) -> ExportManager {208+fn 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 }224 225 /// Build the commands to benchmark226 fn build_commands<'a>(matches: &'a ArgMatches<'_>) -> Vec<Command<'a>> {118 unmodified linesmodified:CHANGELOG.md+2 added
2 unmodified lines3 ## Features4 5 ## Changes6 7+- 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).11 12 ## Bugfixes250 unmodified lines