2021-08-24 21:33:48 +02:00
|
|
|
use std::fs::{File, OpenOptions};
|
2026-10-02 17:18:56 +08:00
|
|
|
use std::io::{self, Write};
|
2021-08-24 21:33:48 +02:00
|
|
|
|
2019-01-29 18:02:19 -05:00
|
|
|
mod asciidoc;
|
2018-03-17 18:59:28 -04:00
|
|
|
mod csv;
|
2018-03-19 17:37:56 -04:00
|
|
|
mod json;
|
2018-03-20 17:00:24 -04:00
|
|
|
mod markdown;
|
2022-03-21 19:57:57 +01:00
|
|
|
mod markup;
|
2022-03-21 21:45:24 +01:00
|
|
|
mod orgmode;
|
2024-12-29 12:46:37 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
2018-03-17 18:59:28 -04:00
|
|
|
|
2019-01-29 18:02:19 -05:00
|
|
|
use self::asciidoc::AsciidocExporter;
|
2018-03-17 18:59:28 -04:00
|
|
|
use self::csv::CsvExporter;
|
2018-03-19 17:37:56 -04:00
|
|
|
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;
|
2018-03-17 18:59:28 -04:00
|
|
|
|
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-02 17:18:56 +08:00
|
|
|
use crate::output::console_writeln;
|
2026-10-05 10:46:51 +02:00
|
|
|
use crate::quantity::TimeUnit;
|
2018-03-17 18:59:28 -04:00
|
|
|
|
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.
|
2018-03-19 17:37:56 -04:00
|
|
|
#[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-17 18:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
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 10:46:51 +02:00
|
|
|
unit: Option<TimeUnit>,
|
2023-06-03 21:06:50 +02:00
|
|
|
sort_order: SortOrder,
|
|
|
|
|
) -> Result<Vec<u8>>;
|
2018-03-17 18:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-20 12:24:10 +02:00
|
|
|
pub enum ExportTarget {
|
|
|
|
|
File(String),
|
|
|
|
|
Stdout,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ExporterWithTarget {
|
2019-05-07 11:55:38 +02:00
|
|
|
exporter: Box<dyn Exporter>,
|
2023-04-20 12:24:10 +02:00
|
|
|
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.
|
2018-03-18 15:13:24 -04:00
|
|
|
pub struct ExportManager {
|
2023-04-20 12:24:10 +02:00
|
|
|
exporters: Vec<ExporterWithTarget>,
|
2026-10-05 10:46:51 +02:00
|
|
|
time_unit: Option<TimeUnit>,
|
2024-12-29 13:57:18 +01:00
|
|
|
sort_order: SortOrder,
|
2018-03-17 18:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
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,
|
2026-10-05 10:46:51 +02:00
|
|
|
time_unit: Option<TimeUnit>,
|
2024-12-29 13:57:18 +01:00
|
|
|
sort_order: SortOrder,
|
|
|
|
|
) -> Result<Self> {
|
2023-04-20 12:24:10 +02:00
|
|
|
let mut export_manager = Self {
|
|
|
|
|
exporters: vec![],
|
|
|
|
|
time_unit,
|
2024-12-29 13:57:18 +01:00
|
|
|
sort_order,
|
2023-04-20 12:24:10 +02:00
|
|
|
};
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-19 17:37:56 -04:00
|
|
|
/// Add an additional exporter to the ExportManager
|
2020-10-18 09:15:44 +02:00
|
|
|
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
|
|
|
|
2023-04-20 12:24:10 +02:00
|
|
|
self.exporters.push(ExporterWithTarget {
|
2018-03-20 21:09:56 +01:00
|
|
|
exporter,
|
2023-04-20 12:24:10 +02:00
|
|
|
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}'"))?;
|
2023-04-20 12:24:10 +02:00
|
|
|
ExportTarget::File(filename.to_string())
|
2023-03-07 18:02:05 +05:00
|
|
|
},
|
2018-03-20 21:09:56 +01:00
|
|
|
});
|
2020-10-18 09:15:44 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
2018-03-17 18:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-20 12:24:10 +02:00
|
|
|
/// 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).
|
2024-12-29 13:57:18 +01:00
|
|
|
pub fn write_results(&self, results: &[BenchmarkResult], intermediate: bool) -> 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
|
|
|
|
|
.serialize(results, self.time_unit, self.sort_order)
|
|
|
|
|
};
|
2023-04-20 12:24:10 +02:00
|
|
|
|
|
|
|
|
match e.target {
|
|
|
|
|
ExportTarget::File(ref filename) => {
|
|
|
|
|
if intermediate {
|
|
|
|
|
write_to_file(filename, &content()?)?
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ExportTarget::Stdout => {
|
|
|
|
|
if !intermediate {
|
2026-10-02 17:18:56 +08:00
|
|
|
let mut stdout = io::stdout().lock();
|
|
|
|
|
console_writeln!(stdout)?;
|
|
|
|
|
console_writeln!(stdout, "{}", String::from_utf8(content()?).unwrap())?;
|
2023-04-20 12:24:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-17 18:59:28 -04:00
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-19 17:37:56 -04:00
|
|
|
|
|
|
|
|
/// 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<()> {
|
2026-10-05 12:48:52 +02:00
|
|
|
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}'"))
|
2018-03-19 17:37:56 -04:00
|
|
|
}
|