rustfs_common/
bucket_stats.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::HashMap;
16
17use crate::last_minute::{self};
18pub struct ReplicationLatency {
19    // Delays for single and multipart PUT requests
20    upload_histogram: last_minute::LastMinuteHistogram,
21}
22
23impl ReplicationLatency {
24    // Merge two ReplicationLatency
25    pub fn merge(&mut self, other: &mut ReplicationLatency) -> &ReplicationLatency {
26        self.upload_histogram.merge(&other.upload_histogram);
27        self
28    }
29
30    // Get upload delay (categorized by object size interval)
31    pub fn get_upload_latency(&mut self) -> HashMap<String, u64> {
32        let mut ret = HashMap::new();
33        let avg = self.upload_histogram.get_avg_data();
34        for (i, v) in avg.iter().enumerate() {
35            let avg_duration = v.avg();
36            ret.insert(self.size_tag_to_string(i), avg_duration.as_millis() as u64);
37        }
38        ret
39    }
40    pub fn update(&mut self, size: i64, during: std::time::Duration) {
41        self.upload_histogram.add(size, during);
42    }
43
44    // Simulate the conversion from size tag to string
45    fn size_tag_to_string(&self, tag: usize) -> String {
46        match tag {
47            0 => String::from("Size < 1 KiB"),
48            1 => String::from("Size < 1 MiB"),
49            2 => String::from("Size < 10 MiB"),
50            3 => String::from("Size < 100 MiB"),
51            4 => String::from("Size < 1 GiB"),
52            _ => String::from("Size > 1 GiB"),
53        }
54    }
55}
56
57// #[derive(Debug, Clone, Default)]
58// pub struct ReplicationLastMinute {
59//     pub last_minute: LastMinuteLatency,
60// }
61
62// impl ReplicationLastMinute {
63//     pub fn merge(&mut self, other: ReplicationLastMinute) -> ReplicationLastMinute {
64//         let mut nl = ReplicationLastMinute::default();
65//         nl.last_minute = self.last_minute.merge(&mut other.last_minute);
66//         nl
67//     }
68
69//     pub fn add_size(&mut self, n: i64) {
70//         let t = SystemTime::now()
71//             .duration_since(UNIX_EPOCH)
72//             .expect("Time went backwards")
73//             .as_secs();
74//         self.last_minute.add_all(t - 1, &AccElem { total: t - 1, size: n as u64, n: 1 });
75//     }
76
77//     pub fn get_total(&self) -> AccElem {
78//         self.last_minute.get_total()
79//     }
80// }
81
82// impl fmt::Display for ReplicationLastMinute {
83//     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84//         let t = self.last_minute.get_total();
85//         write!(f, "ReplicationLastMinute sz= {}, n= {}, dur= {}", t.size, t.n, t.total)
86//     }
87// }