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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use {
    solana_sdk::timing::{duration_as_ms, duration_as_ns, duration_as_s, duration_as_us},
    std::{
        fmt,
        time::{Duration, Instant},
    },
};

#[derive(Debug)]
pub struct Measure {
    name: &'static str,
    start: Instant,
    duration: u64,
}

impl Measure {
    pub fn start(name: &'static str) -> Self {
        Self {
            name,
            start: Instant::now(),
            duration: 0,
        }
    }

    pub fn stop(&mut self) {
        self.duration = duration_as_ns(&self.start.elapsed());
    }

    pub fn as_ns(&self) -> u64 {
        self.duration
    }

    pub fn as_us(&self) -> u64 {
        self.duration / 1000
    }

    pub fn as_ms(&self) -> u64 {
        self.duration / (1000 * 1000)
    }

    pub fn as_s(&self) -> f32 {
        self.duration as f32 / (1000.0f32 * 1000.0f32 * 1000.0f32)
    }

    pub fn as_duration(&self) -> Duration {
        Duration::from_nanos(self.as_ns())
    }

    pub fn end_as_ns(self) -> u64 {
        duration_as_ns(&self.start.elapsed())
    }

    pub fn end_as_us(self) -> u64 {
        duration_as_us(&self.start.elapsed())
    }

    pub fn end_as_ms(self) -> u64 {
        duration_as_ms(&self.start.elapsed())
    }

    pub fn end_as_s(self) -> f32 {
        duration_as_s(&self.start.elapsed())
    }

    pub fn end_as_duration(self) -> Duration {
        self.start.elapsed()
    }
}

impl fmt::Display for Measure {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.duration == 0 {
            write!(f, "{} running", self.name)
        } else if self.as_us() < 1 {
            write!(f, "{} took {}ns", self.name, self.duration)
        } else if self.as_ms() < 1 {
            write!(f, "{} took {}us", self.name, self.as_us())
        } else if self.as_s() < 1. {
            write!(f, "{} took {}ms", self.name, self.as_ms())
        } else {
            write!(f, "{} took {:.1}s", self.name, self.as_s())
        }
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        std::{fmt::Debug, thread::sleep},
    };

    #[test]
    fn test_measure() {
        let mut measure = Measure::start("test");
        sleep(Duration::from_millis(100));
        measure.stop();
        assert!(measure.as_s() >= 0.09f32 && measure.as_s() <= 0.11f32);
        assert!(measure.as_ms() >= 90 && measure.as_ms() <= 110);
        assert!(measure.as_us() >= 99_000 && measure.as_us() <= 110_000);
        assert!(measure.as_ns() >= 99_000_000 && measure.as_ns() <= 110_000_000);
        assert!(
            measure.as_duration() >= Duration::from_millis(90)
                && measure.as_duration() <= Duration::from_millis(110)
        );
    }

    #[test]
    fn test_measure_end_as() {
        #[track_caller]
        fn test_end_as<Res>(method: fn(Measure) -> Res, sleep_ms: u64, lower: Res, upper: Res)
        where
            Res: PartialOrd + Debug,
        {
            let measure = Measure::start("test");
            sleep(Duration::from_millis(sleep_ms));
            let result = method(measure);
            assert!(
                result >= lower,
                "Result below the expected bound.\n\
                 Lower bound: {lower:?}\n\
                 Result: {result:?}"
            );
            assert!(
                result <= upper,
                "Result above the expected bound.\n\
                 Upper bound: {upper:?}\n\
                 Result: {result:?}"
            );
        }

        test_end_as(Measure::end_as_s, 100, 0.09f32, 0.11f32);
        test_end_as(Measure::end_as_ms, 100, 90, 110);
        test_end_as(Measure::end_as_us, 100, 90_000, 110_000);
        test_end_as(Measure::end_as_ns, 100, 90_000_000, 110_000_000);
        test_end_as(
            Measure::end_as_duration,
            100,
            Duration::from_millis(90),
            Duration::from_millis(110),
        );
    }

    #[test]
    fn test_measure_display() {
        let measure = Measure {
            name: "test_ns",
            start: Instant::now(),
            duration: 1,
        };
        assert_eq!(format!("{measure}"), "test_ns took 1ns");

        let measure = Measure {
            name: "test_us",
            start: Instant::now(),
            duration: 1000,
        };
        assert_eq!(format!("{measure}"), "test_us took 1us");

        let measure = Measure {
            name: "test_ms",
            start: Instant::now(),
            duration: 1000 * 1000,
        };
        assert_eq!(format!("{measure}"), "test_ms took 1ms");

        let measure = Measure {
            name: "test_s",
            start: Instant::now(),
            duration: 1000 * 1000 * 1000,
        };
        assert_eq!(format!("{measure}"), "test_s took 1.0s");

        let measure = Measure::start("test_not_stopped");
        assert_eq!(format!("{measure}"), "test_not_stopped running");
    }
}