ferrite_logging/
metrics.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::time::{Duration, Instant};
use tracing::{info, info_span};

pub struct PerformanceMetrics {
    start_time: Instant,
    operation:  &'static str,
    mark_frame: bool,
}

impl PerformanceMetrics {
    pub fn new(operation: &'static str, mark_frame: bool) -> Self {
        let span = info_span!("performance_measurement",
            operation = operation,
            start_time = ?Instant::now()
        );

        span.in_scope(|| {
            info!("Starting operation: {}", operation);
        });

        Self {
            start_time: Instant::now(),
            operation,
            mark_frame,
        }
    }

    pub fn finish(self) -> Duration {
        let duration = self.start_time.elapsed();
        info!(
            operation = self.operation,
            duration_us = duration.as_micros(),
            "Operation completed"
        );

        // If frame marking is enabled, mark the frame completion
        if self.mark_frame {
            tracy_client::frame_mark();
        }

        duration
    }
}