skill_runtime/
metrics.rs

1use std::sync::atomic::{AtomicU64, Ordering};
2
3/// Performance metrics for skill execution
4pub struct ExecutionMetrics {
5    pub cold_start_ms: AtomicU64,
6    pub warm_start_ms: AtomicU64,
7    pub total_executions: AtomicU64,
8    pub failed_executions: AtomicU64,
9}
10
11impl ExecutionMetrics {
12    pub fn new() -> Self {
13        Self {
14            cold_start_ms: AtomicU64::new(0),
15            warm_start_ms: AtomicU64::new(0),
16            total_executions: AtomicU64::new(0),
17            failed_executions: AtomicU64::new(0),
18        }
19    }
20
21    pub fn record_cold_start(&self, duration_ms: u64) {
22        self.cold_start_ms.store(duration_ms, Ordering::Relaxed);
23    }
24
25    pub fn record_warm_start(&self, duration_ms: u64) {
26        self.warm_start_ms.store(duration_ms, Ordering::Relaxed);
27    }
28
29    pub fn record_execution(&self, success: bool) {
30        self.total_executions.fetch_add(1, Ordering::Relaxed);
31        if !success {
32            self.failed_executions.fetch_add(1, Ordering::Relaxed);
33        }
34    }
35
36    pub fn get_cold_start_ms(&self) -> u64 {
37        self.cold_start_ms.load(Ordering::Relaxed)
38    }
39
40    pub fn get_warm_start_ms(&self) -> u64 {
41        self.warm_start_ms.load(Ordering::Relaxed)
42    }
43
44    pub fn get_total_executions(&self) -> u64 {
45        self.total_executions.load(Ordering::Relaxed)
46    }
47
48    pub fn get_failed_executions(&self) -> u64 {
49        self.failed_executions.load(Ordering::Relaxed)
50    }
51
52    pub fn get_success_rate(&self) -> f64 {
53        let total = self.get_total_executions();
54        if total == 0 {
55            return 0.0;
56        }
57        let failed = self.get_failed_executions();
58        ((total - failed) as f64 / total as f64) * 100.0
59    }
60}
61
62impl Default for ExecutionMetrics {
63    fn default() -> Self {
64        Self::new()
65    }
66}