Create export file before first benchmark (#340)

This commit is contained in:
Martin Junghanns authored and GitHub committed 2020-10-18 09:15:44 +02:00
1 parent 2547342644
commit 990ed45c3a
3 files changed
+22 -12

No files matched your search

+2
View File
@@ -4,6 +4,8 @@
## Changes ## Changes
- When `--export-*` commands are used, result files are created before benchmark execution
to fail early in case of, e.g., wrong permissions. See #306 (@s1ck).
- When `--export-*` options are used, result files are written after each individual - When `--export-*` options are used, result files are written after each individual
benchmark command instead of writing after all benchmarks have finished. See #306 (@s1ck). benchmark command instead of writing after all benchmarks have finished. See #306 (@s1ck).
+7 -3
View File
@@ -8,7 +8,7 @@ use self::csv::CsvExporter;
use self::json::JsonExporter; use self::json::JsonExporter;
use self::markdown::MarkdownExporter; use self::markdown::MarkdownExporter;
use std::fs::File; use std::fs::{File, OpenOptions};
use std::io::{Result, Write}; use std::io::{Result, Write};
use crate::hyperfine::types::BenchmarkResult; use crate::hyperfine::types::BenchmarkResult;
@@ -55,7 +55,9 @@ impl ExportManager {
} }
/// Add an additional exporter to the ExportManager /// Add an additional exporter to the ExportManager
pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) { pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {
let _ = File::create(filename)?;
let exporter: Box<dyn Exporter> = match export_type { let exporter: Box<dyn Exporter> = match export_type {
ExportType::Asciidoc => Box::new(AsciidocExporter::default()), ExportType::Asciidoc => Box::new(AsciidocExporter::default()),
ExportType::Csv => Box::new(CsvExporter::default()), ExportType::Csv => Box::new(CsvExporter::default()),
@@ -66,6 +68,8 @@ impl ExportManager {
exporter, exporter,
filename: filename.to_string(), filename: filename.to_string(),
}); });
Ok(())
} }
/// Write the given results to all Exporters contained within this manager /// Write the given results to all Exporters contained within this manager
@@ -80,6 +84,6 @@ impl ExportManager {
/// Write the given content to a file with the specified name /// Write the given content to a file with the specified name
fn write_to_file(filename: &str, content: &[u8]) -> Result<()> { fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {
let mut file = File::create(filename)?; let mut file = OpenOptions::new().write(true).open(filename)?;
file.write_all(content) file.write_all(content)
} }
+13 -9
View File
@@ -71,8 +71,11 @@ fn run(
fn main() { fn main() {
let matches = get_arg_matches(env::args_os()); let matches = get_arg_matches(env::args_os());
let options = build_hyperfine_options(&matches); let options = build_hyperfine_options(&matches);
let export_manager = build_export_manager(&matches);
let commands = build_commands(&matches); let commands = build_commands(&matches);
let export_manager = match build_export_manager(&matches) {
Ok(export_manager) => export_manager,
Err(ref e) => error(&e.to_string())
};
let res = match options { let res = match options {
Ok(ref opts) => run(&commands, &opts, &export_manager), Ok(ref opts) => run(&commands, &opts, &export_manager),
@@ -202,20 +205,21 @@ fn build_hyperfine_options<'a>(
/// Build the ExportManager that will export the results specified /// Build the ExportManager that will export the results specified
/// in the given ArgMatches /// in the given ArgMatches
fn build_export_manager(matches: &ArgMatches<'_>) -> ExportManager { fn build_export_manager(matches: &ArgMatches<'_>) -> io::Result<ExportManager> {
let mut export_manager = ExportManager::new(); let mut export_manager = ExportManager::new();
{ {
let mut add_exporter = |flag, exporttype| { let mut add_exporter = |flag, exporttype| -> io::Result<()> {
if let Some(filename) = matches.value_of(flag) { if let Some(filename) = matches.value_of(flag) {
export_manager.add_exporter(exporttype, filename); export_manager.add_exporter(exporttype, filename)?;
} }
Ok(())
}; };
add_exporter("export-asciidoc", ExportType::Asciidoc); add_exporter("export-asciidoc", ExportType::Asciidoc)?;
add_exporter("export-json", ExportType::Json); add_exporter("export-json", ExportType::Json)?;
add_exporter("export-csv", ExportType::Csv); add_exporter("export-csv", ExportType::Csv)?;
add_exporter("export-markdown", ExportType::Markdown); add_exporter("export-markdown", ExportType::Markdown)?;
} }
export_manager Ok(export_manager)
} }
/// Build the commands to benchmark /// Build the commands to benchmark