use std::sync::Arc;
use fluxframe_core::metrics::{
Counters, EffectTelemetry, LatencyHistogram, MetricsSnapshot, StageTimings,
};
const PER_STAGE_HISTOGRAM_CAPACITY: usize = 2048;
#[derive(Clone)]
pub(crate) struct RuntimeMetrics {
pub(crate) counters: Arc<Counters>,
pub(crate) processing: Arc<LatencyHistogram>,
pub(crate) output: Arc<LatencyHistogram>,
pub(crate) end_to_end: Arc<LatencyHistogram>,
pub(crate) inference: Arc<LatencyHistogram>,
pub(crate) stages: Arc<StageTimings>,
}
impl RuntimeMetrics {
pub(crate) fn new() -> Self {
let hist = || {
Arc::new(LatencyHistogram::with_capacity(
PER_STAGE_HISTOGRAM_CAPACITY,
))
};
Self {
counters: Arc::new(Counters::new()),
processing: hist(),
output: hist(),
end_to_end: hist(),
inference: hist(),
stages: Arc::new(StageTimings::with_capacity(PER_STAGE_HISTOGRAM_CAPACITY)),
}
}
pub(crate) fn effect_telemetry(&self) -> EffectTelemetry {
EffectTelemetry::with_inference(Arc::clone(&self.inference))
.with_stages(Arc::clone(&self.stages))
}
pub(crate) fn snapshot(&self) -> MetricsSnapshot {
MetricsSnapshot::new()
.with_counters(self.counters.snapshot())
.with_processing(self.processing.snapshot())
.with_output(self.output.snapshot())
.with_end_to_end(self.end_to_end.snapshot())
.with_inference(self.inference.snapshot())
.with_stages(self.stages.snapshot())
}
pub(crate) fn sync_dropped(&self, current_total: u64) {
let already = self.counters.snapshot().frames_dropped;
let delta = current_total.saturating_sub(already);
if delta > 0 {
self.counters.add_frames_dropped(delta);
}
}
}
impl Default for RuntimeMetrics {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_bundle_is_empty() {
let m = RuntimeMetrics::new();
let s = m.snapshot();
assert_eq!(s.counters.frames_in, 0);
assert!(s.processing.is_empty());
assert!(s.output.is_empty());
assert!(s.end_to_end.is_empty());
assert!(s.inference.is_empty());
assert!(s.capture.is_empty(), "capture stays unwired in 5.D.3");
}
#[test]
fn effect_telemetry_writes_to_shared_inference_histogram() {
let m = RuntimeMetrics::new();
let t = m.effect_telemetry();
t.record_inference(std::time::Duration::from_micros(8_000));
t.record_inference(std::time::Duration::from_micros(12_500));
let s = m.snapshot();
assert_eq!(s.inference.len(), 2);
assert_eq!(s.inference.percentile_us(0.5), 8_000);
assert_eq!(s.inference.percentile_us(1.0), 12_500);
}
#[test]
fn effect_telemetry_writes_to_shared_stage_table() {
use fluxframe_core::metrics::StageKey;
let m = RuntimeMetrics::new();
let t = m.effect_telemetry();
let key = StageKey::new("composite", "compose");
t.record_stage(key, std::time::Duration::from_micros(700));
let s = m.snapshot();
assert_eq!(s.stages.len(), 1);
assert_eq!(s.stages[0].key, key);
assert_eq!(s.stages[0].latency.percentile_us(0.5), 700);
}
#[test]
fn clone_shares_counters() {
let a = RuntimeMetrics::new();
let b = a.clone();
a.counters.inc_frames_in();
b.counters.inc_frames_in();
assert_eq!(a.snapshot().counters.frames_in, 2);
assert_eq!(b.snapshot().counters.frames_in, 2);
}
#[test]
fn snapshot_carries_recorded_samples() {
let m = RuntimeMetrics::new();
m.processing.record_us(10_000);
m.processing.record_us(20_000);
m.output.record_us(5_000);
m.end_to_end.record_us(15_000);
m.counters.inc_frames_in();
m.counters.inc_frames_out();
let s = m.snapshot();
assert_eq!(s.counters.frames_in, 1);
assert_eq!(s.counters.frames_out, 1);
assert_eq!(s.processing.percentile_us(1.0), 20_000);
assert_eq!(s.output.percentile_us(0.5), 5_000);
assert_eq!(s.end_to_end.percentile_us(0.5), 15_000);
}
#[test]
fn sync_dropped_accumulates_positive_delta() {
let m = RuntimeMetrics::new();
m.sync_dropped(5);
assert_eq!(m.snapshot().counters.frames_dropped, 5);
m.sync_dropped(8);
assert_eq!(m.snapshot().counters.frames_dropped, 8);
}
#[test]
fn sync_dropped_zero_total_is_noop() {
let m = RuntimeMetrics::new();
m.sync_dropped(0);
assert_eq!(m.snapshot().counters.frames_dropped, 0);
m.sync_dropped(5);
m.sync_dropped(5);
assert_eq!(
m.snapshot().counters.frames_dropped,
5,
"no double-count when total is unchanged"
);
}
#[test]
fn sync_dropped_handles_regressing_total() {
let m = RuntimeMetrics::new();
m.sync_dropped(10);
m.sync_dropped(3);
assert_eq!(m.snapshot().counters.frames_dropped, 10);
}
}