Skip to content

Commit

Permalink
[ISSUE #463] ✨Add StatisticsItemFormatter struct (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
flydream-ylgw authored Jun 9, 2024
1 parent f1747dc commit 6b4b742
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 15 deletions.
3 changes: 2 additions & 1 deletion rocketmq-common/src/common/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
mod interceptor;
pub mod interceptor;
pub mod state_getter;
pub mod statistics_brief;
pub mod statistics_item;
pub mod statistics_item_formatter;
18 changes: 4 additions & 14 deletions rocketmq-common/src/common/statistics/statistics_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::{
time::{SystemTime, UNIX_EPOCH},
};

use crate::common::statistics::interceptor::Interceptor;
use crate::{common::statistics::interceptor::Interceptor, TimeUtils::get_current_millis};

pub struct StatisticsItem {
stat_kind: String,
Expand All @@ -42,12 +42,7 @@ impl StatisticsItem {

let item_accumulates = item_names.iter().map(|_| AtomicI64::new(0)).collect();
let invoke_times = AtomicI64::new(0);
let last_timestamp = AtomicU64::new(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs(),
);
let last_timestamp = AtomicU64::new(get_current_millis());

Self {
stat_kind: stat_kind.to_string(),
Expand All @@ -67,13 +62,8 @@ impl StatisticsItem {
}

self.invoke_times.fetch_add(1, Ordering::SeqCst);
self.last_timestamp.store(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs(),
Ordering::SeqCst,
);
self.last_timestamp
.store(get_current_millis(), Ordering::SeqCst);

if let Some(ref interceptor) = self.interceptor {
interceptor.inc(item_incs);
Expand Down
67 changes: 67 additions & 0 deletions rocketmq-common/src/common/statistics/statistics_item_formatter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 std::sync::atomic::Ordering;

use crate::common::statistics::statistics_item::StatisticsItem;

pub struct StatisticsItemFormatter;

impl StatisticsItemFormatter {
pub fn format(&self, stat_item: &StatisticsItem) -> String {
let separator = "|";
let mut sb = String::new();

sb.push_str(stat_item.stat_kind());
sb.push_str(separator);
sb.push_str(stat_item.stat_object());
sb.push_str(separator);

for acc in stat_item.item_accumulates() {
sb.push_str(&acc.load(Ordering::SeqCst).to_string());
sb.push_str(separator);
}

sb.push_str(&stat_item.invoke_times().load(Ordering::SeqCst).to_string());

sb
}
}

#[cfg(test)]
mod tests {
use std::sync::Arc;

use super::*;

#[test]
fn format_creates_correct_string() {
let formatter = StatisticsItemFormatter;
let item = StatisticsItem::new("kind", "object", vec!["item1", "item2"]);
item.inc_items(vec![1, 2]);
let result = formatter.format(&item);
assert_eq!(result, "kind|object|1|2|1");
}

#[test]
fn format_creates_correct_string_with_multiple_items() {
let formatter = StatisticsItemFormatter;
let item = StatisticsItem::new("kind", "object", vec!["item1", "item2", "item3"]);
item.inc_items(vec![1, 2, 3]);
let result = formatter.format(&item);
assert_eq!(result, "kind|object|1|2|3|1");
}
}

0 comments on commit 6b4b742

Please sign in to comment.