Skip to content
Myr

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

4 files+50 added−22 removed

modified:src/benchmark/scheduler.rs+7 added-4 removed
42 unmodified lines
43 for (number, cmd) in self.commands.iter().enumerate() {44 self.results45 .push(Benchmark::new(number, cmd, self.options, &*executor).run()?);4647 // 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 }5152 Ok(())53 }
38 unmodified lines
92 "Note".bold().red()93 );94 }95 }9697 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 lines
9pub enum Alignment {10 Left,11 Right,12}1314pub 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 lines
modified:src/export/mod.rs+40 added-17 removed
43 unmodified lines
44 /// Export the given entries in the serialized form.45 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>>;46}4748pub enum ExportTarget {49 File(String),50 Stdout,51}5248struct ExporterWithFilename {53struct ExporterWithTarget {54 exporter: Box<dyn Exporter>,50 filename: String,55 target: ExportTarget,56}5758/// Handles the management of multiple file exporters.59#[derive(Default)]60pub struct ExportManager {56 exporters: Vec<ExporterWithFilename>,61 exporters: Vec<ExporterWithTarget>,62 time_unit: Option<Unit>,63}6465impl 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 lines
95 ExportType::Markdown => Box::<MarkdownExporter>::default(),96 ExportType::Orgmode => Box::<OrgmodeExporter>::default(),97 };9890 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 });109110 Ok(())111 }112108 /// 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);122123 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 lines
modified:src/main.rs+2 added-1 removed
32 unmodified lines
3334 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)?;3839 options.validate_against_command_list(&commands)?;4041 let mut scheduler = Scheduler::new(&commands, &options, &export_manager);42 scheduler.run_benchmarks()?;43 scheduler.print_relative_speed_comparison();44 scheduler.final_export()?;4546 Ok(())47}48
9 unmodified lines