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
44
45
46
47
48
49
50
51
use std::time::{Duration, Instant};
use tracing::{info, info_span, Instrument};

/// PerformanceMetrics provides utilities for measuring and logging execution
/// times of operations. It creates spans that can be visualized in tracy and
/// logs detailed timing information.
pub struct PerformanceMetrics {
    start_time: Instant,
    operation:  &'static str,
    mark_frame: bool,
}

impl PerformanceMetrics {
    /// Creates a new performance measurement context with optional frame
    /// marking. When frame marking is enabled, it will mark the frame in
    /// tracy's timeline.
    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,
        }
    }

    /// Finishes the measurement, logs the duration, and optionally marks a
    /// frame.
    pub fn finish(self) -> Duration {
        let duration = self.start_time.elapsed();
        info!(
            operation = self.operation,
            duration_ms = duration.as_millis(),
            "Operation completed"
        );

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

        duration
    }
}