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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use std::time::{Duration, Instant};
#[derive(Debug)]
pub struct WorkLimiter {
mode: Mode,
cycle: u16,
start_time: Instant,
completed: usize,
allowed: usize,
desired_cycle_time: Duration,
smoothed_time_per_work_item_nanos: f64,
#[cfg(test)]
get_time: fn() -> Instant,
}
impl WorkLimiter {
pub fn new(desired_cycle_time: Duration) -> Self {
Self {
mode: Mode::Measure,
cycle: 0,
start_time: Instant::now(),
completed: 0,
allowed: 0,
desired_cycle_time,
smoothed_time_per_work_item_nanos: 0.0,
#[cfg(test)]
get_time: std::time::Instant::now,
}
}
pub fn start_cycle(&mut self) {
self.completed = 0;
if let Mode::Measure = self.mode {
self.start_time = self.now();
}
}
pub fn allow_work(&mut self) -> bool {
match self.mode {
Mode::Measure => (self.now() - self.start_time) < self.desired_cycle_time,
Mode::HistoricData => self.completed < self.allowed,
}
}
pub fn record_work(&mut self, work: usize) {
self.completed += work;
}
pub fn finish_cycle(&mut self) {
if self.completed == 0 {
return;
}
if let Mode::Measure = self.mode {
let elapsed = self.now() - self.start_time;
let time_per_work_item_nanos = (elapsed.as_nanos()) as f64 / self.completed as f64;
self.smoothed_time_per_work_item_nanos = if self.allowed == 0 {
time_per_work_item_nanos
} else {
(7.0 * self.smoothed_time_per_work_item_nanos + time_per_work_item_nanos) / 8.0
}
.max(1.0);
self.allowed = (((self.desired_cycle_time.as_nanos()) as f64
/ self.smoothed_time_per_work_item_nanos) as usize)
.max(1);
}
self.cycle = self.cycle.wrapping_add(1);
self.mode = match self.cycle % SAMPLING_INTERVAL {
0 => Mode::Measure,
_ => Mode::HistoricData,
};
}
#[cfg(not(test))]
fn now(&self) -> Instant {
Instant::now()
}
#[cfg(test)]
fn now(&self) -> Instant {
(self.get_time)()
}
}
const SAMPLING_INTERVAL: u16 = 256;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Mode {
Measure,
HistoricData,
}
#[cfg(test)]
mod tests {
use super::*;
use std::cell::RefCell;
#[test]
fn limit_work() {
const CYCLE_TIME: Duration = Duration::from_millis(500);
const BATCH_WORK_ITEMS: usize = 12;
const BATCH_TIME: Duration = Duration::from_millis(100);
const EXPECTED_INITIAL_BATCHES: usize =
(CYCLE_TIME.as_nanos() / BATCH_TIME.as_nanos()) as usize;
const EXPECTED_ALLOWED_WORK_ITEMS: usize = EXPECTED_INITIAL_BATCHES * BATCH_WORK_ITEMS;
let mut limiter = WorkLimiter::new(CYCLE_TIME);
limiter.get_time = get_time;
reset_time();
limiter.start_cycle();
let mut initial_batches = 0;
while limiter.allow_work() {
limiter.record_work(BATCH_WORK_ITEMS);
advance_time(BATCH_TIME);
initial_batches += 1;
}
limiter.finish_cycle();
assert_eq!(initial_batches, EXPECTED_INITIAL_BATCHES);
assert_eq!(limiter.allowed, EXPECTED_ALLOWED_WORK_ITEMS);
let initial_time_per_work_item = limiter.smoothed_time_per_work_item_nanos;
const BATCH_SIZES: [usize; 4] = [1, 2, 3, 5];
for &batch_size in &BATCH_SIZES {
limiter.start_cycle();
let mut allowed_work = 0;
while limiter.allow_work() {
limiter.record_work(batch_size);
allowed_work += batch_size;
}
limiter.finish_cycle();
assert_eq!(allowed_work, EXPECTED_ALLOWED_WORK_ITEMS);
}
for _ in 0..(SAMPLING_INTERVAL as usize - BATCH_SIZES.len() - 1) {
limiter.start_cycle();
limiter.record_work(1);
limiter.finish_cycle();
}
const BATCH_WORK_ITEMS_2: usize = 96;
const TIME_PER_WORK_ITEMS_2_NANOS: f64 =
CYCLE_TIME.as_nanos() as f64 / (EXPECTED_INITIAL_BATCHES * BATCH_WORK_ITEMS_2) as f64;
let expected_updated_time_per_work_item =
(initial_time_per_work_item * 7.0 + TIME_PER_WORK_ITEMS_2_NANOS) / 8.0;
let expected_updated_allowed_work_items =
(CYCLE_TIME.as_nanos() as f64 / expected_updated_time_per_work_item) as usize;
limiter.start_cycle();
let mut initial_batches = 0;
while limiter.allow_work() {
limiter.record_work(BATCH_WORK_ITEMS_2);
advance_time(BATCH_TIME);
initial_batches += 1;
}
limiter.finish_cycle();
assert_eq!(initial_batches, EXPECTED_INITIAL_BATCHES);
assert_eq!(limiter.allowed, expected_updated_allowed_work_items);
}
thread_local! {
pub static TIME: RefCell<Instant> = RefCell::new(Instant::now());
}
fn reset_time() {
TIME.with(|t| {
*t.borrow_mut() = Instant::now();
})
}
fn get_time() -> Instant {
TIME.with(|t| *t.borrow())
}
fn advance_time(duration: Duration) {
TIME.with(|t| {
*t.borrow_mut() += duration;
})
}
}