|
| 1 | +use crate::prelude::*; |
| 2 | +use std::fs; |
| 3 | +use std::path::PathBuf; |
| 4 | + |
| 5 | +/// Manages a counter file to track upload index within a CI job. |
| 6 | +/// |
| 7 | +/// This is used to differentiate multiple uploads in the same CI job execution |
| 8 | +/// (e.g., running both simulation and memory benchmarks in the same job). |
| 9 | +/// |
| 10 | +/// State is stored at: `{repository_root}/.codspeed/run-state/{run_id}/{run_part_id_hash}` |
| 11 | +/// |
| 12 | +/// When a job is retried, it gets a fresh environment, so the counter resets to 0, |
| 13 | +/// which ensures the `run_part_id` remains the same for each upload position. |
| 14 | +pub struct RunIndexState { |
| 15 | + state_file_path: PathBuf, |
| 16 | +} |
| 17 | + |
| 18 | +impl RunIndexState { |
| 19 | + /// Creates a new `RunIndexState` for the given run and run part. |
| 20 | + /// |
| 21 | + /// # Arguments |
| 22 | + /// * `repository_root_path` - The root path of the repository |
| 23 | + /// * `run_id` - The CI run identifier (e.g., GitHub Actions run ID) |
| 24 | + /// * `run_part_id` - The run part identifier (job name + matrix info) |
| 25 | + pub fn new(repository_root_path: &str, run_id: &str, run_part_id: &str) -> Self { |
| 26 | + // Hash the run_part_id to avoid filesystem-unsafe characters |
| 27 | + // (run_part_id can contain JSON with colons, braces, quotes, etc.) |
| 28 | + let run_part_id_hash = sha256::digest(run_part_id); |
| 29 | + let state_file_path = PathBuf::from(repository_root_path) |
| 30 | + .join(".codspeed") |
| 31 | + .join("run-state") |
| 32 | + .join(run_id) |
| 33 | + .join(&run_part_id_hash[..16]); // Use first 16 chars of hash for brevity |
| 34 | + |
| 35 | + Self { state_file_path } |
| 36 | + } |
| 37 | + |
| 38 | + /// Returns the current index and increments it for the next call. |
| 39 | + /// |
| 40 | + /// If the state file doesn't exist, starts at 0. |
| 41 | + /// The incremented value is persisted for subsequent calls. |
| 42 | + pub fn get_and_increment(&self) -> Result<u32> { |
| 43 | + // Create parent directories if needed |
| 44 | + if let Some(parent) = self.state_file_path.parent() { |
| 45 | + fs::create_dir_all(parent)?; |
| 46 | + } |
| 47 | + |
| 48 | + // Read current value (default to 0 if file doesn't exist) |
| 49 | + let current = if self.state_file_path.exists() { |
| 50 | + fs::read_to_string(&self.state_file_path)? |
| 51 | + .trim() |
| 52 | + .parse::<u32>() |
| 53 | + .unwrap_or(0) |
| 54 | + } else { |
| 55 | + 0 |
| 56 | + }; |
| 57 | + |
| 58 | + // Write incremented value for next call |
| 59 | + fs::write(&self.state_file_path, (current + 1).to_string())?; |
| 60 | + |
| 61 | + Ok(current) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +#[cfg(test)] |
| 66 | +mod tests { |
| 67 | + use super::*; |
| 68 | + use tempfile::TempDir; |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn test_get_and_increment_starts_at_zero() { |
| 72 | + let temp_dir = TempDir::new().unwrap(); |
| 73 | + let state = RunIndexState::new( |
| 74 | + temp_dir.path().to_str().unwrap(), |
| 75 | + "run-123", |
| 76 | + "my_job-{\"shard\":1}", |
| 77 | + ); |
| 78 | + |
| 79 | + assert_eq!(state.get_and_increment().unwrap(), 0); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn test_get_and_increment_increments() { |
| 84 | + let temp_dir = TempDir::new().unwrap(); |
| 85 | + let state = RunIndexState::new( |
| 86 | + temp_dir.path().to_str().unwrap(), |
| 87 | + "run-123", |
| 88 | + "my_job-{\"shard\":1}", |
| 89 | + ); |
| 90 | + |
| 91 | + assert_eq!(state.get_and_increment().unwrap(), 0); |
| 92 | + assert_eq!(state.get_and_increment().unwrap(), 1); |
| 93 | + assert_eq!(state.get_and_increment().unwrap(), 2); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_different_run_part_ids_have_separate_counters() { |
| 98 | + let temp_dir = TempDir::new().unwrap(); |
| 99 | + let repo_path = temp_dir.path().to_str().unwrap(); |
| 100 | + |
| 101 | + let state1 = RunIndexState::new(repo_path, "run-123", "job_a"); |
| 102 | + let state2 = RunIndexState::new(repo_path, "run-123", "job_b"); |
| 103 | + |
| 104 | + assert_eq!(state1.get_and_increment().unwrap(), 0); |
| 105 | + assert_eq!(state2.get_and_increment().unwrap(), 0); |
| 106 | + assert_eq!(state1.get_and_increment().unwrap(), 1); |
| 107 | + assert_eq!(state2.get_and_increment().unwrap(), 1); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn test_different_run_ids_have_separate_counters() { |
| 112 | + let temp_dir = TempDir::new().unwrap(); |
| 113 | + let repo_path = temp_dir.path().to_str().unwrap(); |
| 114 | + |
| 115 | + let state1 = RunIndexState::new(repo_path, "run-123", "my_job"); |
| 116 | + let state2 = RunIndexState::new(repo_path, "run-456", "my_job"); |
| 117 | + |
| 118 | + assert_eq!(state1.get_and_increment().unwrap(), 0); |
| 119 | + assert_eq!(state2.get_and_increment().unwrap(), 0); |
| 120 | + assert_eq!(state1.get_and_increment().unwrap(), 1); |
| 121 | + assert_eq!(state2.get_and_increment().unwrap(), 1); |
| 122 | + } |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn test_state_persists_across_new_instances() { |
| 126 | + let temp_dir = TempDir::new().unwrap(); |
| 127 | + let repo_path = temp_dir.path().to_str().unwrap(); |
| 128 | + |
| 129 | + { |
| 130 | + let state = RunIndexState::new(repo_path, "run-123", "my_job"); |
| 131 | + assert_eq!(state.get_and_increment().unwrap(), 0); |
| 132 | + } |
| 133 | + |
| 134 | + { |
| 135 | + let state = RunIndexState::new(repo_path, "run-123", "my_job"); |
| 136 | + assert_eq!(state.get_and_increment().unwrap(), 1); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn test_creates_directory_structure() { |
| 142 | + let temp_dir = TempDir::new().unwrap(); |
| 143 | + let repo_path = temp_dir.path().to_str().unwrap(); |
| 144 | + |
| 145 | + let state = RunIndexState::new(repo_path, "run-123", "my_job"); |
| 146 | + state.get_and_increment().unwrap(); |
| 147 | + |
| 148 | + // Verify the directory structure was created |
| 149 | + let codspeed_dir = temp_dir.path().join(".codspeed"); |
| 150 | + assert!(codspeed_dir.exists()); |
| 151 | + assert!(codspeed_dir.join("run-state").exists()); |
| 152 | + assert!(codspeed_dir.join("run-state").join("run-123").exists()); |
| 153 | + } |
| 154 | +} |
0 commit comments