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
use crate::{Clock, ExponentiallyDecayingReservoir, Meter, Reservoir, Snapshot};
use std::sync::Arc;
use std::time::{Duration, Instant};
pub struct Timer {
meter: Meter,
reservoir: Box<dyn Reservoir>,
clock: Arc<dyn Clock>,
}
impl Default for Timer {
#[inline]
fn default() -> Timer {
Timer::new(ExponentiallyDecayingReservoir::new())
}
}
impl Timer {
pub fn new<R>(reservoir: R) -> Timer
where
R: Reservoir,
{
Timer {
meter: Meter::new(),
reservoir: Box::new(reservoir),
clock: crate::SYSTEM_CLOCK.clone(),
}
}
pub fn new_with<R>(reservoir: R, clock: Arc<dyn Clock>) -> Self
where
R: Reservoir,
{
Timer {
meter: Meter::new_with(clock.clone()),
reservoir: Box::new(reservoir),
clock,
}
}
#[inline]
pub fn update(&self, duration: Duration) {
self.meter.mark(1);
let nanos = duration.as_nanos() as i64;
self.reservoir.update(nanos);
}
#[inline]
pub fn time(&self) -> Time<'_> {
Time {
timer: self,
start: self.clock.now(),
}
}
#[inline]
pub fn count(&self) -> i64 {
self.meter.count()
}
#[inline]
pub fn one_minute_rate(&self) -> f64 {
self.meter.one_minute_rate()
}
#[inline]
pub fn five_minute_rate(&self) -> f64 {
self.meter.five_minute_rate()
}
#[inline]
pub fn fifteen_minute_rate(&self) -> f64 {
self.meter.fifteen_minute_rate()
}
#[inline]
pub fn mean_rate(&self) -> f64 {
self.meter.mean_rate()
}
#[inline]
pub fn snapshot(&self) -> Box<dyn Snapshot> {
self.reservoir.snapshot()
}
}
pub struct Time<'a> {
timer: &'a Timer,
start: Instant,
}
impl Drop for Time<'_> {
#[inline]
fn drop(&mut self) {
self.timer.update(self.timer.clock.now() - self.start);
}
}
#[cfg(test)]
mod test {
use crate::Timer;
use std::thread;
use std::time::Duration;
#[test]
#[allow(clippy::float_cmp)]
fn basic() {
let timer = Timer::default();
for _ in 0..15 {
timer.update(Duration::from_nanos(0));
}
for _ in 0..5 {
timer.update(Duration::from_nanos(5));
}
assert_eq!(timer.count(), 20);
assert!(timer.mean_rate() > 0.);
assert_eq!(timer.snapshot().value(0.8), 5.)
}
#[test]
fn time() {
let timer = Timer::default();
let guard = timer.time();
thread::sleep(Duration::from_millis(10));
drop(guard);
assert_eq!(timer.count(), 1);
assert!(timer.snapshot().max() >= 10_000_000);
}
}