-
Notifications
You must be signed in to change notification settings - Fork 35
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
1 parent
ed658bb
commit ffef5c5
Showing
18 changed files
with
435 additions
and
118 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 |
---|---|---|
@@ -1,26 +1,23 @@ | ||
use std::time::Instant; | ||
|
||
#[derive(Clone)] | ||
pub struct Counter { | ||
pub timestamp: Instant, | ||
pub value: u64, | ||
pub value: usize, | ||
pub description: String, | ||
} | ||
|
||
impl Counter { | ||
pub fn new() -> Self { | ||
pub fn new(value: usize) -> Self { | ||
Self { | ||
timestamp: Instant::now(), | ||
value: 0, | ||
value, | ||
description: String::new(), | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
pub fn add(&mut self, value: u64) { | ||
pub fn add(&mut self, value: usize) { | ||
self.value += value; | ||
} | ||
} | ||
|
||
impl Default for Counter { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} |
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,2 +1,3 @@ | ||
pub mod counter; | ||
pub mod statistic_counter; | ||
pub mod monitor; | ||
pub mod time_window_counter; |
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,80 @@ | ||
use std::collections::HashMap; | ||
|
||
use strum::{Display, EnumString, IntoStaticStr}; | ||
|
||
use super::counter::Counter; | ||
use super::time_window_counter::TimeWindowCounter; | ||
|
||
#[derive(Clone)] | ||
pub struct Monitor { | ||
pub accumulate_counters: HashMap<CounterType, Counter>, | ||
pub time_window_counters: HashMap<CounterType, TimeWindowCounter>, | ||
pub time_window_secs: usize, | ||
} | ||
|
||
#[derive(EnumString, IntoStaticStr, Display, PartialEq, Eq, Hash, Clone)] | ||
pub enum CounterType { | ||
// time window counter, aggregate by: sum by interval | ||
#[strum(serialize = "batch_write_failures")] | ||
BatchWriteFailures, | ||
#[strum(serialize = "serial_writes")] | ||
SerialWrites, | ||
|
||
// time window counter, aggregate by: avg by interval | ||
#[strum(serialize = "rps")] | ||
Records, | ||
|
||
// time window counter, aggregate by: avg by count | ||
#[strum(serialize = "bytes_per_query")] | ||
BytesPerQuery, | ||
#[strum(serialize = "records_per_query")] | ||
RecordsPerQuery, | ||
#[strum(serialize = "rt_per_query")] | ||
RtPerQuery, | ||
#[strum(serialize = "buffer_size")] | ||
BufferSize, | ||
|
||
// accumulate counter | ||
#[strum(serialize = "sinked_count")] | ||
SinkedCount, | ||
} | ||
|
||
const DEFAULT_INTERVAL_SECS: usize = 5; | ||
|
||
impl Monitor { | ||
pub fn new_default() -> Self { | ||
Self::new(DEFAULT_INTERVAL_SECS) | ||
} | ||
|
||
pub fn new(interval_secs: usize) -> Self { | ||
Self { | ||
accumulate_counters: HashMap::new(), | ||
time_window_counters: HashMap::new(), | ||
time_window_secs: interval_secs, | ||
} | ||
} | ||
|
||
pub fn add_counter(&mut self, counter_type: CounterType, value: usize) -> &mut Self { | ||
match counter_type { | ||
CounterType::SinkedCount => { | ||
if let Some(counter) = self.accumulate_counters.get_mut(&counter_type) { | ||
counter.add(value) | ||
} else { | ||
self.accumulate_counters | ||
.insert(counter_type, Counter::new(value)); | ||
} | ||
} | ||
|
||
_ => { | ||
if let Some(counter) = self.time_window_counters.get_mut(&counter_type) { | ||
counter.add(value) | ||
} else { | ||
let mut counter = TimeWindowCounter::new(self.time_window_secs); | ||
counter.add(value); | ||
self.time_window_counters.insert(counter_type, counter); | ||
} | ||
} | ||
} | ||
self | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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::LinkedList; | ||
|
||
use super::counter::Counter; | ||
|
||
#[derive(Clone)] | ||
pub struct TimeWindowCounter { | ||
pub time_window_secs: usize, | ||
pub counters: LinkedList<Counter>, | ||
pub description: String, | ||
} | ||
|
||
impl TimeWindowCounter { | ||
pub fn new(time_window_secs: usize) -> Self { | ||
Self { | ||
time_window_secs, | ||
counters: LinkedList::new(), | ||
description: String::new(), | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
pub fn add(&mut self, value: usize) { | ||
self.counters.push_back(Counter::new(value)); | ||
} | ||
|
||
#[inline(always)] | ||
pub fn sum(&mut self) -> usize { | ||
let mut sum = 0; | ||
for counter in self.counters.iter() { | ||
sum += counter.value; | ||
} | ||
sum | ||
} | ||
|
||
#[inline(always)] | ||
pub fn count(&mut self) -> usize { | ||
self.counters.len() | ||
} | ||
|
||
#[inline(always)] | ||
pub fn avg_by_interval(&mut self) -> usize { | ||
self.sum() / self.time_window_secs | ||
} | ||
|
||
#[inline(always)] | ||
pub fn avg_by_count(&mut self) -> usize { | ||
if self.counters.len() > 0 { | ||
self.sum() / self.counters.len() | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
pub fn refresh_window(&mut self) { | ||
let mut outdate_count = 0; | ||
for counter in self.counters.iter() { | ||
if counter.timestamp.elapsed().as_secs() > self.time_window_secs as u64 { | ||
outdate_count += 1; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
for _ in 0..outdate_count { | ||
self.counters.pop_front(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.