gotham/helpers/
timing.rs

1//! Defines types for timing requests and emitting timing information.
2use std::fmt::{self, Display, Formatter};
3use std::time::{Duration, Instant};
4
5use time::OffsetDateTime;
6
7/// Timer struct used to record execution times of requests.
8///
9/// The `elapsed` function returns the elapsed time in an easy to format way,
10/// suitable for use with requset logging middlewares.
11#[derive(Clone, Copy)]
12pub(crate) struct Timer {
13    // We use 2 start fields
14    // because we want formattable time to print start time
15    // but we cannot use it to calculate duration because it is not monotonic.
16    //
17    // It is possible that we spent a lot of time between initialization of fields,
18    // for example, if current thread unscheduled by OS but it should be very rare.
19    // On the other hand, adjusting system clock by NTP is much more possible.
20    start_monotonic: Instant,
21    start_formattable: OffsetDateTime,
22}
23
24impl Timer {
25    /// Begins measuring from the current time.
26    pub(crate) fn new() -> Timer {
27        Timer {
28            start_monotonic: Instant::now(),
29            start_formattable: OffsetDateTime::now_utc(),
30        }
31    }
32
33    /// Finishes measuring, and returns the elapsed time as a `Timing` value.
34    pub(crate) fn elapsed(&self) -> Timing {
35        let duration = self.start_monotonic.elapsed();
36        Timing(duration)
37    }
38
39    /// Retrieves the start time of this timer.
40    pub(crate) fn start_time(&self) -> &OffsetDateTime {
41        &self.start_formattable
42    }
43}
44
45/// Represents an elapsed time measured by `Timer`.
46#[derive(Clone, Copy)]
47pub(crate) struct Timing(Duration);
48
49impl Display for Timing {
50    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
51        let duration = self.0;
52        match duration.as_micros() {
53            i if i < 1000 => {
54                write!(f, "{}µs", i)
55            }
56            i if i < 1_000_000 => {
57                write!(f, "{:.2}ms", (i as f64) / 1000.0)
58            }
59            _ => {
60                write!(f, "{:.2}s", duration.as_secs_f32())
61            }
62        }
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use std::time::Duration;
69
70    use super::Timing;
71
72    #[test]
73    fn test_durations() {
74        let microsecond = Duration::from_micros(1);
75
76        let t0 = Timing(microsecond * 555);
77        assert_eq!(t0.to_string(), "555µs");
78
79        let t1 = Timing(microsecond * 666_444);
80        assert_eq!(t1.to_string(), "666.44ms");
81
82        let t2 = Timing(microsecond * 777_444_333);
83        assert_eq!(t2.to_string(), "777.44s");
84    }
85}