|
| 1 | +use std::fs; |
| 2 | +use std::path::{Path, PathBuf}; |
| 3 | +use std::process::Command; |
| 4 | + |
| 5 | +use apollo_infra_utils::path::project_path; |
| 6 | +use rstest::{fixture, rstest}; |
| 7 | + |
| 8 | +use crate::types::estimates::Estimates; |
| 9 | + |
| 10 | +/// Returns the bench_tools crate directory. |
| 11 | +#[fixture] |
| 12 | +fn bench_tools_crate_dir() -> PathBuf { |
| 13 | + std::env::var("CARGO_MANIFEST_DIR") |
| 14 | + .map(PathBuf::from) |
| 15 | + .unwrap_or_else(|_| std::env::current_dir().unwrap()) |
| 16 | +} |
| 17 | + |
| 18 | +/// Returns the directory where dummy benchmark estimate results are stored. |
| 19 | +#[fixture] |
| 20 | +fn dummy_bench_results_dir(bench_tools_crate_dir: PathBuf) -> PathBuf { |
| 21 | + bench_tools_crate_dir.join("data/dummy_benches_result") |
| 22 | +} |
| 23 | + |
| 24 | +/// Returns the workspace root directory. |
| 25 | +#[fixture] |
| 26 | +fn workspace_root() -> PathBuf { |
| 27 | + project_path().expect("Failed to get project path") |
| 28 | +} |
| 29 | + |
| 30 | +/// Returns the list of dummy benchmark names. |
| 31 | +#[fixture] |
| 32 | +fn dummy_bench_names() -> &'static [&'static str] { |
| 33 | + &["dummy_sum_100", "dummy_sum_1000"] |
| 34 | +} |
| 35 | + |
| 36 | +/// Helper function to deserialize dummy bench estimates JSON files in a directory. |
| 37 | +fn assert_deserialize_dummy_bench_estimates(results_dir: &Path, bench_names: &[&str]) { |
| 38 | + for bench_name in bench_names { |
| 39 | + let path = results_dir.join(format!("{}_estimates.json", bench_name)); |
| 40 | + let data = fs::read_to_string(&path) |
| 41 | + .unwrap_or_else(|e| panic!("Failed to read {}: {}", path.display(), e)); |
| 42 | + |
| 43 | + let _est: Estimates = serde_json::from_str(&data).unwrap_or_else(|e| { |
| 44 | + panic!("Failed to deserialize {}: {}\nContent: {}", path.display(), e, data) |
| 45 | + }); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[rstest] |
| 50 | +#[ignore] |
| 51 | +/// Run dummy benchmark and deserialize the results. |
| 52 | +fn run_dummy_bench_and_deserialize_estimates( |
| 53 | + workspace_root: PathBuf, |
| 54 | + dummy_bench_results_dir: PathBuf, |
| 55 | + dummy_bench_names: &[&str], |
| 56 | +) { |
| 57 | + // 1) Run dummy benchmark. |
| 58 | + let status = Command::new("cargo") |
| 59 | + .args(["bench", "-p", "bench_tools", "--bench", "dummy_bench"]) |
| 60 | + .status() |
| 61 | + .expect("Failed to spawn `cargo bench`"); |
| 62 | + assert!(status.success(), "`cargo bench` did not exit successfully"); |
| 63 | + |
| 64 | + // 2) Collect and save dummy_bench estimates.json files |
| 65 | + fs::create_dir_all(&dummy_bench_results_dir).expect("Failed to create results directory"); |
| 66 | + |
| 67 | + for bench_name in dummy_bench_names { |
| 68 | + let source_path = |
| 69 | + workspace_root.join("target/criterion").join(bench_name).join("new/estimates.json"); |
| 70 | + let dest_path = dummy_bench_results_dir.join(format!("{}_estimates.json", bench_name)); |
| 71 | + |
| 72 | + // Read, parse, and write the result to the results directory. |
| 73 | + let data = fs::read_to_string(&source_path) |
| 74 | + .unwrap_or_else(|e| panic!("Failed to read {}: {}", source_path.display(), e)); |
| 75 | + let json: serde_json::Value = serde_json::from_str(&data).expect("Failed to parse JSON"); |
| 76 | + let pretty_json = serde_json::to_string_pretty(&json).expect("Failed to serialize JSON"); |
| 77 | + fs::write(&dest_path, pretty_json).expect("Failed to write benchmark result"); |
| 78 | + } |
| 79 | + |
| 80 | + // 3) Deserialize and validate the saved results |
| 81 | + assert_deserialize_dummy_bench_estimates(&dummy_bench_results_dir, dummy_bench_names); |
| 82 | +} |
| 83 | + |
| 84 | +#[rstest] |
| 85 | +/// Test that Estimates can be deserialized from the saved results. |
| 86 | +fn deserialize_dummy_bench_estimates(dummy_bench_results_dir: PathBuf, dummy_bench_names: &[&str]) { |
| 87 | + assert_deserialize_dummy_bench_estimates(&dummy_bench_results_dir, dummy_bench_names); |
| 88 | +} |
0 commit comments