#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct GarnetSessionMetrics {
pub total_net_input_bytes: u64,
pub total_net_output_bytes: u64,
pub total_commands_processed: u64,
pub total_pending: u64,
pub total_found: u64,
pub total_notfound: u64,
pub total_cluster_commands_processed: u64,
pub total_write_commands_processed: u64,
pub total_read_commands_processed: u64,
pub total_transactions_commands_received: u64,
pub total_number_resp_server_session_exceptions: u64,
pub total_transaction_commands_execution_failed: u64,
}
impl GarnetSessionMetrics {
pub fn add(&mut self, add: &GarnetSessionMetrics) {
self.incr_total_net_input_bytes(add.get_total_net_input_bytes());
self.incr_total_net_output_bytes(add.get_total_net_output_bytes());
self.incr_total_commands_processed(add.get_total_commands_processed());
self.incr_total_pending(add.get_total_pending());
self.incr_total_found(add.get_total_found());
self.incr_total_notfound(add.get_total_notfound());
self.incr_total_cluster_commands_processed(add.get_total_cluster_commands_processed());
self.add_total_write_commands_processed(add.get_total_write_commands_processed());
self.add_total_read_commands_processed(add.get_total_read_commands_processed());
self.incr_total_transaction_commands_received(add.get_total_transaction_commands_received());
self.incr_total_transaction_execution_failed(
add.get_total_transaction_commands_execution_failed(),
);
self.incr_total_number_resp_server_session_exceptions(
add.get_total_number_resp_server_session_exceptions(),
);
}
pub fn reset(&mut self) {
*self = Self::default();
}
#[inline]
pub fn incr_total_net_input_bytes(&mut self, bytes: u64) {
self.total_net_input_bytes = self.total_net_input_bytes.wrapping_add(bytes);
}
#[inline]
pub fn get_total_net_input_bytes(&self) -> u64 {
self.total_net_input_bytes
}
#[inline]
pub fn incr_total_net_output_bytes(&mut self, bytes: u64) {
self.total_net_output_bytes = self.total_net_output_bytes.wrapping_add(bytes);
}
#[inline]
pub fn get_total_net_output_bytes(&self) -> u64 {
self.total_net_output_bytes
}
#[inline]
pub fn incr_total_commands_processed(&mut self, cmds: u64) {
self.total_commands_processed = self.total_commands_processed.wrapping_add(cmds);
}
#[inline]
pub fn get_total_commands_processed(&self) -> u64 {
self.total_commands_processed
}
#[inline]
pub fn incr_total_pending(&mut self, count: u64) {
self.total_pending = self.total_pending.wrapping_add(count);
}
#[inline]
pub fn get_total_pending(&self) -> u64 {
self.total_pending
}
#[inline]
pub fn incr_total_found(&mut self, count: u64) {
self.total_found = self.total_found.wrapping_add(count);
}
#[inline]
pub fn get_total_found(&self) -> u64 {
self.total_found
}
#[inline]
pub fn incr_total_notfound(&mut self, count: u64) {
self.total_notfound = self.total_notfound.wrapping_add(count);
}
#[inline]
pub fn get_total_notfound(&self) -> u64 {
self.total_notfound
}
#[inline]
pub fn incr_total_cluster_commands_processed(&mut self, count: u64) {
self.total_cluster_commands_processed =
self.total_cluster_commands_processed.wrapping_add(count);
}
#[inline]
pub fn get_total_cluster_commands_processed(&self) -> u64 {
self.total_cluster_commands_processed
}
#[inline]
pub fn add_total_write_commands_processed(&mut self, count: u64) {
self.total_write_commands_processed = self.total_write_commands_processed.wrapping_add(count);
}
#[inline]
pub fn get_total_write_commands_processed(&self) -> u64 {
self.total_write_commands_processed
}
#[inline]
pub fn add_total_read_commands_processed(&mut self, count: u64) {
self.total_read_commands_processed = self.total_read_commands_processed.wrapping_add(count);
}
#[inline]
pub fn incr_total_transaction_commands_received(&mut self, count: u64) {
self.total_transactions_commands_received = self
.total_transactions_commands_received
.wrapping_add(count);
}
#[inline]
pub fn incr_total_transaction_execution_failed(&mut self, count: u64) {
self.total_transaction_commands_execution_failed = self
.total_transaction_commands_execution_failed
.wrapping_add(count);
}
#[inline]
pub fn get_total_read_commands_processed(&self) -> u64 {
self.total_read_commands_processed
}
#[inline]
pub fn get_total_transaction_commands_received(&self) -> u64 {
self.total_transactions_commands_received
}
#[inline]
pub fn get_total_transaction_commands_execution_failed(&self) -> u64 {
self.total_transaction_commands_execution_failed
}
#[inline]
pub fn incr_total_number_resp_server_session_exceptions(&mut self, count: u64) {
self.total_number_resp_server_session_exceptions = self
.total_number_resp_server_session_exceptions
.wrapping_add(count);
}
#[inline]
pub fn get_total_number_resp_server_session_exceptions(&self) -> u64 {
self.total_number_resp_server_session_exceptions
}
}
#[cfg(test)]
mod tests {
use super::GarnetSessionMetrics;
#[test]
fn incr_and_get_roundtrip() {
let mut m = GarnetSessionMetrics::default();
m.incr_total_net_input_bytes(100);
m.incr_total_net_output_bytes(200);
m.incr_total_commands_processed(7);
m.incr_total_pending(2);
m.incr_total_found(5);
m.incr_total_notfound(2);
m.incr_total_cluster_commands_processed(1);
m.add_total_write_commands_processed(3);
m.add_total_read_commands_processed(4);
m.incr_total_transaction_commands_received(1);
m.incr_total_transaction_execution_failed(0);
m.incr_total_number_resp_server_session_exceptions(0);
assert_eq!(m.get_total_net_input_bytes(), 100);
assert_eq!(m.get_total_net_output_bytes(), 200);
assert_eq!(m.get_total_commands_processed(), 7);
assert_eq!(m.get_total_pending(), 2);
assert_eq!(m.get_total_found(), 5);
assert_eq!(m.get_total_notfound(), 2);
assert_eq!(m.get_total_cluster_commands_processed(), 1);
assert_eq!(m.get_total_write_commands_processed(), 3);
assert_eq!(m.get_total_read_commands_processed(), 4);
assert_eq!(m.get_total_transaction_commands_received(), 1);
assert_eq!(m.get_total_transaction_commands_execution_failed(), 0);
assert_eq!(m.get_total_number_resp_server_session_exceptions(), 0);
}
#[test]
fn add_aggregates_every_counter() {
let mut base = GarnetSessionMetrics::default();
base.incr_total_commands_processed(10);
base.incr_total_found(6);
let mut delta = GarnetSessionMetrics::default();
delta.incr_total_commands_processed(3);
delta.incr_total_found(1);
delta.incr_total_net_input_bytes(64);
base.add(&delta);
assert_eq!(base.get_total_commands_processed(), 13);
assert_eq!(base.get_total_found(), 7);
assert_eq!(base.get_total_net_input_bytes(), 64);
base.reset();
assert_eq!(base, GarnetSessionMetrics::default());
}
}