Skip to main content

aft_base/
stage_timer.rs

1//! Wall-clock timing of the stages inside one longer operation, for profiling
2//! by hand.
3
4use std::time::Instant;
5
6/// Wall-clock sub-stages of one operation, printed to stderr as
7/// `view_profile {prefix}.{stage} ms=...` only when `AFT_VIEW_PROFILE` is set.
8/// It records nothing else.
9pub struct StageTimer {
10    prefix: &'static str,
11    pub(crate) enabled: bool,
12    started: Instant,
13}
14
15impl StageTimer {
16    pub fn new(prefix: &'static str) -> Self {
17        Self {
18            prefix,
19            enabled: std::env::var_os("AFT_VIEW_PROFILE").is_some(),
20            started: Instant::now(),
21        }
22    }
23
24    pub fn finish(&mut self, stage: &str, detail: std::fmt::Arguments<'_>) {
25        #[cfg(any(test, feature = "test-support"))]
26        crate::test_allocations::heap_marker(self.prefix, stage);
27        if self.enabled {
28            eprintln!(
29                "view_profile {}.{stage} ms={:.3} {detail}",
30                self.prefix,
31                self.started.elapsed().as_secs_f64() * 1000.0
32            );
33        }
34        self.started = Instant::now();
35    }
36}