-
Notifications
You must be signed in to change notification settings - Fork 119
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
dc52c0a
commit 01d247a
Showing
4 changed files
with
178 additions
and
0 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,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)] | ||
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; | ||
} | ||
|
||
// Getter and Setter for `tps` | ||
pub fn get_tps(&self) -> f64 { | ||
self.tps | ||
} | ||
|
||
pub fn set_tps(&mut self, tps: f64) { | ||
self.tps = tps; | ||
} | ||
|
||
// Getter and Setter for `avgpt` | ||
pub fn get_avgpt(&self) -> f64 { | ||
self.avgpt | ||
} | ||
|
||
pub fn set_avgpt(&mut self, avgpt: f64) { | ||
self.avgpt = avgpt; | ||
} | ||
} |
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
116 changes: 116 additions & 0 deletions
116
rocketmq-remoting/src/protocol/subscription/broker_stats_data.rs
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,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); | ||
} | ||
} |