Skip to content

Commit

Permalink
Add BrokerStatsData
Browse files Browse the repository at this point in the history
  • Loading branch information
sainad2222 committed Dec 7, 2024
1 parent dc52c0a commit 01d247a
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions rocketmq-remoting/src/protocol/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod get_consumer_listby_group_response_body;
pub mod consumer_connection;

pub mod acl_info;
pub mod broker_item;
pub mod check_client_request_body;
pub mod check_rocksdb_cqwrite_progress_response_body;
pub mod cluster_acl_version_info;
Expand Down
60 changes: 60 additions & 0 deletions rocketmq-remoting/src/protocol/body/broker_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use serde::Deserialize;
use serde::Serialize;

#[derive(Debug, Clone, Serialize, Deserialize, Default)]

Check warning on line 21 in rocketmq-remoting/src/protocol/body/broker_item.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-remoting/src/protocol/body/broker_item.rs#L21

Added line #L21 was not covered by tests
pub struct BrokerStatsItem {
sum: u64,
tps: f64,
avgpt: f64,
}

impl BrokerStatsItem {
// Constructor
pub fn new(sum: u64, tps: f64, avgpt: f64) -> Self {
Self { sum, tps, avgpt }
}

// Getter and Setter for `sum`
pub fn get_sum(&self) -> u64 {
self.sum
}

pub fn set_sum(&mut self, sum: u64) {
self.sum = sum;
}

Check warning on line 41 in rocketmq-remoting/src/protocol/body/broker_item.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-remoting/src/protocol/body/broker_item.rs#L39-L41

Added lines #L39 - L41 were not covered by tests

// Getter and Setter for `tps`
pub fn get_tps(&self) -> f64 {
self.tps
}

pub fn set_tps(&mut self, tps: f64) {
self.tps = tps;
}

Check warning on line 50 in rocketmq-remoting/src/protocol/body/broker_item.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-remoting/src/protocol/body/broker_item.rs#L48-L50

Added lines #L48 - L50 were not covered by tests

// Getter and Setter for `avgpt`
pub fn get_avgpt(&self) -> f64 {
self.avgpt
}

pub fn set_avgpt(&mut self, avgpt: f64) {
self.avgpt = avgpt;
}

Check warning on line 59 in rocketmq-remoting/src/protocol/body/broker_item.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-remoting/src/protocol/body/broker_item.rs#L57-L59

Added lines #L57 - L59 were not covered by tests
}
1 change: 1 addition & 0 deletions rocketmq-remoting/src/protocol/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

pub mod broker_stats_data;
pub mod customized_retry_policy;
pub mod exponential_retry_policy;
pub mod group_forbidden;
Expand Down
116 changes: 116 additions & 0 deletions rocketmq-remoting/src/protocol/subscription/broker_stats_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use crate::protocol::body::broker_item::BrokerStatsItem;

pub struct BrokerStatsData {
stats_minute: BrokerStatsItem,
stats_hour: BrokerStatsItem,
stats_day: BrokerStatsItem,
}

impl BrokerStatsData {
pub fn new(
stats_minute: BrokerStatsItem,
stats_hour: BrokerStatsItem,
stats_day: BrokerStatsItem,
) -> Self {
Self {
stats_minute,
stats_hour,
stats_day,
}
}

pub fn get_stats_minute(&self) -> &BrokerStatsItem {
&self.stats_minute
}

pub fn set_stats_minute(&mut self, stats_minute: BrokerStatsItem) {
self.stats_minute = stats_minute;
}

pub fn get_stats_hour(&self) -> &BrokerStatsItem {
&self.stats_hour
}

pub fn set_stats_hour(&mut self, stats_hour: BrokerStatsItem) {
self.stats_hour = stats_hour;
}

pub fn get_stats_day(&self) -> &BrokerStatsItem {
&self.stats_day
}

pub fn set_stats_day(&mut self, stats_day: BrokerStatsItem) {
self.stats_day = stats_day;
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_initial_values() {
let stats_minute = BrokerStatsItem::new(100, 12.5, 8.3);
let stats_hour = BrokerStatsItem::new(500, 15.0, 9.0);
let stats_day = BrokerStatsItem::new(1000, 20.0, 10.0);

let broker_stats =
BrokerStatsData::new(stats_minute.clone(), stats_hour.clone(), stats_day.clone());

assert_eq!(broker_stats.get_stats_minute().get_sum(), 100);
assert_eq!(broker_stats.get_stats_hour().get_sum(), 500);
assert_eq!(broker_stats.get_stats_day().get_sum(), 1000);

assert_eq!(broker_stats.get_stats_minute().get_tps(), 12.5);
assert_eq!(broker_stats.get_stats_hour().get_tps(), 15.0);
assert_eq!(broker_stats.get_stats_day().get_tps(), 20.0);
}

#[test]
fn test_set_stats_minute() {
let stats_minute = BrokerStatsItem::new(100, 12.5, 8.3);
let stats_hour = BrokerStatsItem::new(500, 15.0, 9.0);
let stats_day = BrokerStatsItem::new(1000, 20.0, 10.0);

let mut broker_stats = BrokerStatsData::new(stats_minute, stats_hour, stats_day);

let new_stats_minute = BrokerStatsItem::new(200, 25.0, 12.0);
broker_stats.set_stats_minute(new_stats_minute.clone());

assert_eq!(broker_stats.get_stats_minute().get_sum(), 200);
assert_eq!(broker_stats.get_stats_minute().get_tps(), 25.0);
assert_eq!(broker_stats.get_stats_minute().get_avgpt(), 12.0);
}

#[test]
fn test_set_stats_hour() {
let stats_minute = BrokerStatsItem::new(100, 12.5, 8.3);
let stats_hour = BrokerStatsItem::new(500, 15.0, 9.0);
let stats_day = BrokerStatsItem::new(1000, 20.0, 10.0);

let mut broker_stats = BrokerStatsData::new(stats_minute, stats_hour, stats_day);

let new_stats_hour = BrokerStatsItem::new(600, 18.0, 10.0);
broker_stats.set_stats_hour(new_stats_hour.clone());

assert_eq!(broker_stats.get_stats_hour().get_sum(), 600);
assert_eq!(broker_stats.get_stats_hour().get_tps(), 18.0);
assert_eq!(broker_stats.get_stats_hour().get_avgpt(), 10.0);
}

#[test]
fn test_set_stats_day() {
let stats_minute = BrokerStatsItem::new(100, 12.5, 8.3);
let stats_hour = BrokerStatsItem::new(500, 15.0, 9.0);
let stats_day = BrokerStatsItem::new(1000, 20.0, 10.0);

let mut broker_stats = BrokerStatsData::new(stats_minute, stats_hour, stats_day);

let new_stats_day = BrokerStatsItem::new(1200, 22.0, 11.0);
broker_stats.set_stats_day(new_stats_day.clone());

assert_eq!(broker_stats.get_stats_day().get_sum(), 1200);
assert_eq!(broker_stats.get_stats_day().get_tps(), 22.0);
assert_eq!(broker_stats.get_stats_day().get_avgpt(), 11.0);
}
}

0 comments on commit 01d247a

Please # to comment.