|
| 1 | +use std::{ |
| 2 | + io::Write, |
| 3 | + path::{Path, PathBuf}, |
| 4 | +}; |
| 5 | + |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | + |
| 8 | +#[derive(Debug, Serialize, Deserialize)] |
| 9 | +pub struct BenchmarkMetadata { |
| 10 | + pub name: String, |
| 11 | + pub uri: String, |
| 12 | +} |
| 13 | + |
| 14 | +#[derive(Debug, Serialize, Deserialize)] |
| 15 | +pub struct RawWallTimeData { |
| 16 | + #[serde(flatten)] |
| 17 | + pub metadata: BenchmarkMetadata, |
| 18 | + pub iter_per_round: u32, |
| 19 | + pub max_time_ns: Option<u128>, |
| 20 | + pub times_ns: Vec<u128>, |
| 21 | +} |
| 22 | + |
| 23 | +impl RawWallTimeData { |
| 24 | + fn from_runtime_data( |
| 25 | + name: String, |
| 26 | + uri: String, |
| 27 | + iter_per_round: u32, |
| 28 | + max_time_ns: Option<u128>, |
| 29 | + times_ns: Vec<u128>, |
| 30 | + ) -> Self { |
| 31 | + RawWallTimeData { |
| 32 | + metadata: BenchmarkMetadata { name, uri }, |
| 33 | + iter_per_round, |
| 34 | + max_time_ns, |
| 35 | + times_ns, |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + fn dump_to_results(&self, workspace_root: &Path, scope: &str) { |
| 40 | + let output_dir = get_raw_result_dir_from_workspace_root(workspace_root).join(scope); |
| 41 | + std::fs::create_dir_all(&output_dir).unwrap(); |
| 42 | + let bench_id = uuid::Uuid::new_v4().to_string(); |
| 43 | + let output_path = output_dir.join(format!("{}.json", bench_id)); |
| 44 | + let mut writer = std::fs::File::create(&output_path).expect("Failed to create the file"); |
| 45 | + serde_json::to_writer_pretty(&mut writer, self).expect("Failed to write the data"); |
| 46 | + writer.flush().expect("Failed to flush the writer"); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// Entry point called in patched integration to harvest raw walltime data |
| 51 | +pub fn collect_raw_walltime_results( |
| 52 | + scope: &str, |
| 53 | + name: String, |
| 54 | + uri: String, |
| 55 | + iter_per_round: u32, |
| 56 | + max_time_ns: Option<u128>, |
| 57 | + times_ns: Vec<u128>, |
| 58 | +) { |
| 59 | + let workspace_root = std::env::var("CODSPEED_CARGO_WORKSPACE_ROOT").map(PathBuf::from); |
| 60 | + let Ok(workspace_root) = workspace_root else { |
| 61 | + eprintln!("codspeed failed to get workspace root. skipping"); |
| 62 | + return; |
| 63 | + }; |
| 64 | + let data = RawWallTimeData::from_runtime_data(name, uri, iter_per_round, max_time_ns, times_ns); |
| 65 | + data.dump_to_results(&workspace_root, scope); |
| 66 | +} |
| 67 | + |
| 68 | +pub fn get_raw_result_dir_from_workspace_root(workspace_root: &Path) -> PathBuf { |
| 69 | + workspace_root |
| 70 | + .join("target") |
| 71 | + .join("codspeed") |
| 72 | + .join("raw_results") |
| 73 | +} |
0 commit comments