1use anyhow::Context as _;
2
3use crate::BenchmarkFile;
4
5const FILE_PREFIX: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../target/bench/");
6pub const PREVIOUS: &str = "previous";
7
8pub(crate) fn get_comparison_file(baseline: Option<&str>) -> Option<BenchmarkFile> {
9 let file_name = baseline.unwrap_or(PREVIOUS);
10 let path = format!("{FILE_PREFIX}{file_name}.json");
11
12 let file = std::fs::read_to_string(path).ok()?;
13 let benchmark_file: BenchmarkFile = serde_json::from_str(&file).ok()?;
14 Some(benchmark_file)
15}
16
17pub(crate) fn write_results_file(
18 file_name: &str,
19 output_file: &BenchmarkFile,
20) -> anyhow::Result<()> {
21 let path = format!("{FILE_PREFIX}{file_name}.json");
22 let json = serde_json::to_string_pretty(output_file)?;
23 std::fs::create_dir_all(FILE_PREFIX)
24 .with_context(|| format!("Trying to create directory {FILE_PREFIX}"))?;
25 std::fs::write(&path, json).with_context(|| format!("Trying to write file {path}"))?;
26 Ok(())
27}