-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
226 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::{ | ||
collections::VecDeque, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
/// Rolling average implementation. | ||
pub struct ThroughputCounter { | ||
buckets: VecDeque<(Instant, u64)>, | ||
bucket_size: Duration, | ||
window_size: Duration, | ||
current_count: u64, | ||
current_bucket_start: Instant, | ||
} | ||
impl ThroughputCounter { | ||
pub fn new(window_size: Duration) -> Self { | ||
let now = Instant::now(); | ||
Self { | ||
buckets: VecDeque::new(), | ||
bucket_size: window_size / 60, | ||
window_size, | ||
current_count: 0, | ||
current_bucket_start: now, | ||
} | ||
} | ||
|
||
pub fn increment(&mut self) { | ||
let now = Instant::now(); | ||
if now.duration_since(self.current_bucket_start) >= self.bucket_size { | ||
// Clean-up expired buckets. | ||
while let Some((time, _)) = self.buckets.front() { | ||
if now.duration_since(*time) < self.window_size { | ||
break; | ||
} | ||
self.buckets.pop_front(); | ||
} | ||
|
||
// Make a new bucket. | ||
if self.current_count > 0 { | ||
self.buckets.push_back((self.current_bucket_start, self.current_count)); | ||
} | ||
self.current_count = 0; | ||
self.current_bucket_start = now; | ||
} | ||
self.current_count += 1; | ||
} | ||
|
||
/// Returns ops/s | ||
pub fn get_throughput(&self) -> f64 { | ||
let now = Instant::now(); | ||
let total_ops = self | ||
.buckets | ||
.iter() | ||
.skip_while(|(time, _)| now.duration_since(*time) >= self.window_size) | ||
.map(|(_, count)| count) | ||
.sum::<u64>() | ||
+ self.current_count; | ||
let window_duration = if let Some((oldest_time, _)) = self.buckets.front() { | ||
now.duration_since(*oldest_time).as_secs_f64() | ||
} else { | ||
now.duration_since(self.current_bucket_start).as_secs_f64() | ||
}; | ||
|
||
if window_duration > 0.0 { | ||
total_ops as f64 / window_duration | ||
} else { | ||
0.0 | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ mod apply_state; | |
mod pipeline; | ||
mod sync; | ||
mod util; | ||
mod counter; | ||
|
||
pub use sync::SyncControllerConfig; | ||
|
||
|
Oops, something went wrong.