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
//! Measuring the time of operations

use std::time::{Duration, SystemTime};

pub struct MeasureTime(SystemTime);

impl MeasureTime {
    pub fn new() -> Self {
        Self(SystemTime::now())
    }
}

impl Default for MeasureTime {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for MeasureTime {
    fn drop(&mut self) {
        match self.0.elapsed() {
            Ok(duration) => {
                // truncate to seconds, good enough
                let duration = Duration::from_secs(duration.as_secs());
                log::info!("Processing took {}", humantime::format_duration(duration))
            }
            Err(err) => log::warn!("Unable to measure processing time: {err}"),
        }
    }
}