Files
hyperfine/src/export/mod.rs
T

175 lines
5.4 KiB
Rust
Raw Normal View History

2021-08-24 21:33:48 +02:00
use std::fs::{File, OpenOptions};
use std::io::{self, Write};
2021-08-24 21:33:48 +02:00
2019-01-29 18:02:19 -05:00
mod asciidoc;
mod csv;
mod json;
2018-03-20 17:00:24 -04:00
mod markdown;
mod markup;
2022-03-21 21:45:24 +01:00
mod orgmode;
2024-12-29 12:46:37 +01:00
#[cfg(test)]
mod tests;
2019-01-29 18:02:19 -05:00
use self::asciidoc::AsciidocExporter;
use self::csv::CsvExporter;
use self::json::JsonExporter;
2018-03-20 17:00:24 -04:00
use self::markdown::MarkdownExporter;
2022-03-21 21:45:24 +01:00
use self::orgmode::OrgmodeExporter;
2022-02-20 13:44:34 +01:00
use crate::benchmark::benchmark_result::BenchmarkResult;
2023-06-03 21:06:50 +02:00
use crate::options::SortOrder;
2026-10-05 14:19:34 +02:00
use crate::quantity::TimeUnit;
2022-02-06 17:44:28 +01:00
use anyhow::{Context, Result};
2022-02-06 18:22:55 +01:00
use clap::ArgMatches;
2022-02-06 17:44:28 +01:00
2018-03-20 20:26:28 +01:00
/// The desired form of exporter to use for a given file.
#[derive(Clone)]
2018-03-20 20:26:28 +01:00
pub enum ExportType {
2019-01-29 18:02:19 -05:00
/// Asciidoc Table
Asciidoc,
2018-03-20 20:26:28 +01:00
/// CSV (comma separated values) format
2018-03-20 21:09:56 +01:00
Csv,
2018-03-20 20:26:28 +01:00
/// JSON format
2018-03-20 21:09:56 +01:00
Json,
2018-03-20 17:00:24 -04:00
/// Markdown table
Markdown,
2022-03-21 21:45:24 +01:00
/// Emacs org-mode tables
Orgmode,
}
2018-03-20 20:26:28 +01:00
/// Interface for different exporters.
trait Exporter {
/// Export the given entries in the serialized form.
2023-06-03 21:06:50 +02:00
fn serialize(
&self,
results: &[BenchmarkResult],
2026-10-05 14:18:33 +02:00
time_unit: Option<TimeUnit>,
2023-06-03 21:06:50 +02:00
sort_order: SortOrder,
2026-10-05 14:59:18 +02:00
reference_index: Option<usize>,
2023-06-03 21:06:50 +02:00
) -> Result<Vec<u8>>;
}
pub enum ExportTarget {
File(String),
Stdout,
}
struct ExporterWithTarget {
2019-05-07 11:55:38 +02:00
exporter: Box<dyn Exporter>,
target: ExportTarget,
2018-03-20 21:09:56 +01:00
}
2018-03-20 20:28:57 +01:00
/// Handles the management of multiple file exporters.
pub struct ExportManager {
exporters: Vec<ExporterWithTarget>,
time_unit: Option<TimeUnit>,
2024-12-29 13:57:18 +01:00
sort_order: SortOrder,
}
2021-10-04 23:07:38 +09:00
impl ExportManager {
2022-02-06 18:22:55 +01:00
/// Build the ExportManager that will export the results specified
/// in the given ArgMatches
2024-12-29 13:57:18 +01:00
pub fn from_cli_arguments(
matches: &ArgMatches,
time_unit: Option<TimeUnit>,
2024-12-29 13:57:18 +01:00
sort_order: SortOrder,
) -> Result<Self> {
let mut export_manager = Self {
exporters: vec![],
time_unit,
2024-12-29 13:57:18 +01:00
sort_order,
};
2022-02-06 18:22:55 +01:00
{
let mut add_exporter = |flag, exporttype| -> Result<()> {
2022-10-27 20:42:33 +02:00
if let Some(filename) = matches.get_one::<String>(flag) {
2022-02-06 18:22:55 +01:00
export_manager.add_exporter(exporttype, filename)?;
}
Ok(())
};
add_exporter("export-asciidoc", ExportType::Asciidoc)?;
add_exporter("export-json", ExportType::Json)?;
add_exporter("export-csv", ExportType::Csv)?;
add_exporter("export-markdown", ExportType::Markdown)?;
2022-03-21 21:45:24 +01:00
add_exporter("export-orgmode", ExportType::Orgmode)?;
2022-02-06 18:22:55 +01:00
}
Ok(export_manager)
}
/// Add an additional exporter to the ExportManager
pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {
2019-05-07 11:55:38 +02:00
let exporter: Box<dyn Exporter> = match export_type {
2023-03-15 09:49:43 +01:00
ExportType::Asciidoc => Box::<AsciidocExporter>::default(),
ExportType::Csv => Box::<CsvExporter>::default(),
ExportType::Json => Box::<JsonExporter>::default(),
ExportType::Markdown => Box::<MarkdownExporter>::default(),
ExportType::Orgmode => Box::<OrgmodeExporter>::default(),
2018-03-20 21:09:56 +01:00
};
2023-03-07 18:02:05 +05:00
self.exporters.push(ExporterWithTarget {
2018-03-20 21:09:56 +01:00
exporter,
target: if filename == "-" {
ExportTarget::Stdout
2023-03-07 18:02:05 +05:00
} else {
2023-03-15 09:41:48 +01:00
let _ = File::create(filename)
2024-05-31 13:26:35 -07:00
.with_context(|| format!("Could not create export file '{filename}'"))?;
ExportTarget::File(filename.to_string())
2023-03-07 18:02:05 +05:00
},
2018-03-20 21:09:56 +01:00
});
Ok(())
}
/// Write the given results to all Exporters. The 'intermediate' flag specifies
/// whether this is being called while still performing benchmarks, or if this
/// is the final call after all benchmarks have been finished. In the former case,
/// results are written to all file targets (to always have them up to date, even
/// if a benchmark fails). In the latter case, we only print to stdout targets (in
/// order not to clutter the output of hyperfine with intermediate results).
2026-10-05 14:59:18 +02:00
/// `reference_index` refers to the complete benchmark sequence, so it can
/// point past `results` while that benchmark is still pending.
pub fn write_results(
&self,
results: &[BenchmarkResult],
intermediate: bool,
reference_index: Option<usize>,
) -> Result<()> {
2018-03-20 21:09:56 +01:00
for e in &self.exporters {
2024-12-29 13:57:18 +01:00
let content = || {
e.exporter
2026-10-05 14:59:18 +02:00
.serialize(results, self.time_unit, self.sort_order, reference_index)
2024-12-29 13:57:18 +01:00
};
match e.target {
ExportTarget::File(ref filename) => {
if intermediate {
write_to_file(filename, &content()?)?
}
}
ExportTarget::Stdout => {
if !intermediate {
let mut stdout = io::stdout().lock();
console_writeln!(stdout)?;
console_writeln!(stdout, "{}", String::from_utf8(content()?).unwrap())?;
}
}
}
}
Ok(())
}
}
/// Write the given content to a file with the specified name
2019-07-13 10:29:05 +02:00
fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {
let mut file = OpenOptions::new()
.write(true)
// Avoid leaving trailing bytes when the new export is shorter.
.truncate(true)
.open(filename)?;
2018-03-20 17:00:24 -04:00
file.write_all(content)
2024-05-31 13:26:35 -07:00
.with_context(|| format!("Failed to export results to '{filename}'"))
}