-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: support adaptive flush strategy again (#482)
* Feat: support adaptive flush strategy again * fix: clippy * fix: clippy
- Loading branch information
Showing
4 changed files
with
166 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const LOOKBACK_COUNT: usize = 20; | ||
const ONE_TWENTY_SECONDS: f64 = 120.0; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq)] | ||
pub(crate) struct InvocationTimes { | ||
times: [u64; LOOKBACK_COUNT], | ||
head: usize, | ||
} | ||
|
||
impl InvocationTimes { | ||
pub(crate) fn new() -> InvocationTimes { | ||
InvocationTimes { | ||
times: [0; LOOKBACK_COUNT], | ||
head: 0, | ||
} | ||
} | ||
|
||
pub(crate) fn add(&mut self, timestamp: u64) { | ||
self.times[self.head] = timestamp; | ||
self.head = (self.head + 1) % LOOKBACK_COUNT; | ||
} | ||
|
||
pub(crate) fn should_adapt_to_periodic(&self, now: u64) -> bool { | ||
let mut count = 0; | ||
let mut last = 0; | ||
for time in &self.times { | ||
if *time != 0 { | ||
count += 1; | ||
last = *time; | ||
} | ||
} | ||
// If we haven't seen enough invocations, we should flush | ||
if count < LOOKBACK_COUNT { | ||
return false; | ||
} | ||
let elapsed = now - last; | ||
(elapsed as f64 / (count - 1) as f64) < ONE_TWENTY_SECONDS | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::lifecycle::invocation_times; | ||
|
||
#[test] | ||
fn new() { | ||
let invocation_times = invocation_times::InvocationTimes::new(); | ||
assert_eq!( | ||
invocation_times.times, | ||
[0; invocation_times::LOOKBACK_COUNT] | ||
); | ||
assert_eq!(invocation_times.head, 0); | ||
} | ||
|
||
#[test] | ||
fn insertion() { | ||
let mut invocation_times = invocation_times::InvocationTimes::new(); | ||
let timestamp = 1; | ||
invocation_times.add(timestamp); | ||
assert_eq!(invocation_times.times[0], timestamp); | ||
assert_eq!(invocation_times.head, 1); | ||
assert!(!invocation_times.should_adapt_to_periodic(1)); | ||
} | ||
|
||
#[test] | ||
fn insertion_with_full_buffer_fast_invokes() { | ||
let mut invocation_times = invocation_times::InvocationTimes::new(); | ||
for i in 0..=invocation_times::LOOKBACK_COUNT { | ||
invocation_times.add(i as u64); | ||
} | ||
// should wrap around | ||
assert_eq!(invocation_times.times[0], 20); | ||
assert_eq!(invocation_times.head, 1); | ||
assert!(invocation_times.should_adapt_to_periodic(21)); | ||
} | ||
|
||
#[test] | ||
fn insertion_with_full_buffer_slow_invokes() { | ||
let mut invocation_times = invocation_times::InvocationTimes::new(); | ||
invocation_times.add(1_u64); | ||
for i in 0..invocation_times::LOOKBACK_COUNT { | ||
invocation_times.add((i + 5000) as u64); | ||
} | ||
// should wrap around | ||
assert_eq!(invocation_times.times[0], 5019); | ||
assert_eq!(invocation_times.head, 1); | ||
assert!(!invocation_times.should_adapt_to_periodic(10000)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod flush_control; | ||
pub mod invocation; | ||
pub mod invocation_times; | ||
pub mod listener; |