Skip to main content

murk_engine/
metrics.rs

1//! Per-tick performance metrics for the simulation engine.
2//!
3//! [`StepMetrics`] captures timing and memory data for a single tick,
4//! enabling telemetry, profiling, and adaptive backoff decisions.
5
6/// Timing and memory metrics collected during a single tick.
7///
8/// All durations are in microseconds. The engine populates these fields
9/// after each `step()` call; consumers (telemetry, backoff logic) read
10/// them from the most recent tick.
11#[derive(Clone, Debug, Default)]
12pub struct StepMetrics {
13    /// Wall-clock time for the entire tick, in microseconds.
14    pub total_us: u64,
15    /// Time spent processing the ingress command queue, in microseconds.
16    pub command_processing_us: u64,
17    /// Per-propagator execution times: `(name, microseconds)`.
18    pub propagator_us: Vec<(String, u64)>,
19    /// Time spent publishing the snapshot to the ring buffer, in microseconds.
20    pub snapshot_publish_us: u64,
21    /// Memory usage of the arena after the tick, in bytes.
22    pub memory_bytes: usize,
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28
29    #[test]
30    fn default_metrics_are_zero() {
31        let m = StepMetrics::default();
32        assert_eq!(m.total_us, 0);
33        assert_eq!(m.command_processing_us, 0);
34        assert!(m.propagator_us.is_empty());
35        assert_eq!(m.snapshot_publish_us, 0);
36        assert_eq!(m.memory_bytes, 0);
37    }
38
39    #[test]
40    fn metrics_fields_accessible() {
41        let m = StepMetrics {
42            total_us: 100,
43            command_processing_us: 20,
44            propagator_us: vec![("diffusion".to_string(), 50), ("decay".to_string(), 30)],
45            snapshot_publish_us: 10,
46            memory_bytes: 4096,
47        };
48        assert_eq!(m.total_us, 100);
49        assert_eq!(m.command_processing_us, 20);
50        assert_eq!(m.propagator_us.len(), 2);
51        assert_eq!(m.propagator_us[0].0, "diffusion");
52        assert_eq!(m.propagator_us[0].1, 50);
53        assert_eq!(m.snapshot_publish_us, 10);
54        assert_eq!(m.memory_bytes, 4096);
55    }
56}