Do not export intermediate results to stdout, closes #640
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent d2fb7d4 · Browse files · 4 files·+50 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+50 added−22 removed
- other:+50 added−22 removed
Provenance
- Commit
2793d7b- Tree
ef78540- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
d2fb7d4
4 files changed+50 added−22 removed
modified:src/benchmark/scheduler.rs+7 added-4 removed
42 unmodified lines43 for (number, cmd) in self.commands.iter().enumerate() {44 self.results45 .push(Benchmark::new(number, cmd, self.options, &*executor).run()?);46 47- // We export (all results so far) after each individual benchmark, because48- // we would risk losing all results if a later benchmark fails.49- self.export_manager50- .write_results(&self.results, self.options.time_unit)?;47+ // We export results after each individual benchmark, because48+ // we would risk losing them if a later benchmark fails.49+ self.export_manager.write_results(&self.results, true)?;50 }51 52 Ok(())53 }38 unmodified lines92 "Note".bold().red()93 );94 }95 }96+97+ pub fn final_export(&self) -> Result<()> {98+ self.export_manager.write_results(&self.results, false)99+ }100 }modified:src/export/markup.rs+1 added
8 unmodified lines9 pub enum Alignment {10 Left,11 Right,12 }13+14 pub trait MarkupExporter {15 fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {16 // prepare table header strings17 let notation = format!("[{}]", unit.short_name());273 unmodified linesmodified:src/export/mod.rs+40 added-17 removed
43 unmodified lines44 /// Export the given entries in the serialized form.45 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>>;46 }47 48+pub enum ExportTarget {49+ File(String),50+ Stdout,51+}52+48-struct ExporterWithFilename {53+struct ExporterWithTarget {54 exporter: Box<dyn Exporter>,50- filename: String,55+ target: ExportTarget,56 }57 58 /// Handles the management of multiple file exporters.59 #[derive(Default)]60 pub struct ExportManager {56- exporters: Vec<ExporterWithFilename>,61+ exporters: Vec<ExporterWithTarget>,62+ time_unit: Option<Unit>,63 }64 65 impl ExportManager {66 /// Build the ExportManager that will export the results specified67 /// in the given ArgMatches62- pub fn from_cli_arguments(matches: &ArgMatches) -> Result<Self> {63- let mut export_manager = Self::default();68+ pub fn from_cli_arguments(matches: &ArgMatches, time_unit: Option<Unit>) -> Result<Self> {69+ let mut export_manager = Self {70+ exporters: vec![],71+ time_unit,72+ };73 {74 let mut add_exporter = |flag, exporttype| -> Result<()> {75 if let Some(filename) = matches.get_one::<String>(flag) {76 export_manager.add_exporter(exporttype, filename)?;18 unmodified lines95 ExportType::Markdown => Box::<MarkdownExporter>::default(),96 ExportType::Orgmode => Box::<OrgmodeExporter>::default(),97 };98 90- self.exporters.push(ExporterWithFilename {99+ self.exporters.push(ExporterWithTarget {100 exporter,92- filename: if filename == "-" {93- if cfg!(windows) {94- "con:".to_string()95- } else {96- "/dev/stdout".to_string()97- }101+ target: if filename == "-" {102+ ExportTarget::Stdout103 } else {104 let _ = File::create(filename)105 .with_context(|| format!("Could not create export file '{}'", filename))?;101- filename.to_string()106+ ExportTarget::File(filename.to_string())107 },108 });109 110 Ok(())111 }112 108- /// Write the given results to all Exporters contained within this manager109- pub fn write_results(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<()> {113+ /// Write the given results to all Exporters. The 'intermediate' flag specifies114+ /// whether this is being called while still performing benchmarks, or if this115+ /// is the final call after all benchmarks have been finished. In the former case,116+ /// results are written to all file targets (to always have them up to date, even117+ /// if a benchmark fails). In the latter case, we only print to stdout targets (in118+ /// order not to clutter the output of hyperfine with intermediate results).119+ pub fn write_results(&self, results: &[BenchmarkResult], intermediate: bool) -> Result<()> {120 for e in &self.exporters {111- let file_content = e.exporter.serialize(results, unit)?;112- write_to_file(&e.filename, &file_content)?;121+ let content = || e.exporter.serialize(results, self.time_unit);122+123+ match e.target {124+ ExportTarget::File(ref filename) => {125+ if intermediate {126+ write_to_file(filename, &content()?)?127+ }128+ }129+ ExportTarget::Stdout => {130+ if !intermediate {131+ println!();132+ println!("{}", String::from_utf8(content()?).unwrap());133+ }134+ }135+ }136 }137 Ok(())138 }139 }7 unmodified linesmodified:src/main.rs+2 added-1 removed
32 unmodified lines33 34 let cli_arguments = get_cli_arguments(env::args_os());35 let options = Options::from_cli_arguments(&cli_arguments)?;36 let commands = Commands::from_cli_arguments(&cli_arguments)?;37- let export_manager = ExportManager::from_cli_arguments(&cli_arguments)?;37+ let export_manager = ExportManager::from_cli_arguments(&cli_arguments, options.time_unit)?;38 39 options.validate_against_command_list(&commands)?;40 41 let mut scheduler = Scheduler::new(&commands, &options, &export_manager);42 scheduler.run_benchmarks()?;43 scheduler.print_relative_speed_comparison();44+ scheduler.final_export()?;45 46 Ok(())47 }48 9 unmodified lines