rustnet_core/network/
interface_stats.rs1use std::io;
2use std::time::SystemTime;
3
4#[derive(Debug, Clone)]
6pub struct InterfaceStats {
7 pub interface_name: String,
8 pub rx_bytes: u64,
9 pub tx_bytes: u64,
10 pub rx_packets: u64,
11 pub tx_packets: u64,
12 pub rx_errors: u64,
13 pub tx_errors: u64,
14 pub rx_dropped: u64,
15 pub tx_dropped: u64,
16 pub collisions: u64,
17 pub timestamp: SystemTime,
18}
19
20impl InterfaceStats {
21 pub fn calculate_rates(&self, previous: &InterfaceStats) -> InterfaceRates {
23 let duration = self
24 .timestamp
25 .duration_since(previous.timestamp)
26 .unwrap_or_default()
27 .as_secs_f64();
28
29 if duration == 0.0 {
30 return InterfaceRates::default();
31 }
32
33 InterfaceRates {
34 rx_bytes_per_sec: ((self.rx_bytes.saturating_sub(previous.rx_bytes)) as f64 / duration)
35 as u64,
36 tx_bytes_per_sec: ((self.tx_bytes.saturating_sub(previous.tx_bytes)) as f64 / duration)
37 as u64,
38 }
39 }
40}
41
42#[derive(Debug, Clone, Default)]
44pub struct InterfaceRates {
45 pub rx_bytes_per_sec: u64,
46 pub tx_bytes_per_sec: u64,
47}
48
49pub trait InterfaceStatsProvider: Send + Sync {
51 fn get_all_stats(&self) -> Result<Vec<InterfaceStats>, io::Error>;
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58 use std::time::Duration;
59
60 #[test]
61 fn test_rate_calculation() {
62 let t1 = SystemTime::now();
63 let t2 = t1 + Duration::from_secs(1);
64
65 let stats1 = InterfaceStats {
66 interface_name: "test".to_string(),
67 rx_bytes: 1000,
68 tx_bytes: 500,
69 rx_packets: 10,
70 tx_packets: 5,
71 rx_errors: 0,
72 tx_errors: 0,
73 rx_dropped: 0,
74 tx_dropped: 0,
75 collisions: 0,
76 timestamp: t1,
77 };
78
79 let stats2 = InterfaceStats {
80 interface_name: "test".to_string(),
81 rx_bytes: 2000,
82 tx_bytes: 1000,
83 rx_packets: 20,
84 tx_packets: 10,
85 rx_errors: 0,
86 tx_errors: 0,
87 rx_dropped: 0,
88 tx_dropped: 0,
89 collisions: 0,
90 timestamp: t2,
91 };
92
93 let rates = stats2.calculate_rates(&stats1);
94 assert_eq!(rates.rx_bytes_per_sec, 1000);
95 assert_eq!(rates.tx_bytes_per_sec, 500);
96 }
97
98 #[test]
99 fn test_rate_calculation_zero_duration() {
100 let t = SystemTime::now();
101
102 let stats1 = InterfaceStats {
103 interface_name: "test".to_string(),
104 rx_bytes: 1000,
105 tx_bytes: 500,
106 rx_packets: 10,
107 tx_packets: 5,
108 rx_errors: 0,
109 tx_errors: 0,
110 rx_dropped: 0,
111 tx_dropped: 0,
112 collisions: 0,
113 timestamp: t,
114 };
115
116 let stats2 = stats1.clone();
117
118 let rates = stats2.calculate_rates(&stats1);
119 assert_eq!(rates.rx_bytes_per_sec, 0);
120 assert_eq!(rates.tx_bytes_per_sec, 0);
121 }
122
123 #[test]
124 fn test_rate_calculation_with_counter_wrapping() {
125 let t1 = SystemTime::now();
126 let t2 = t1 + Duration::from_secs(1);
127
128 let stats1 = InterfaceStats {
129 interface_name: "test".to_string(),
130 rx_bytes: 1000,
131 tx_bytes: 500,
132 rx_packets: 10,
133 tx_packets: 5,
134 rx_errors: 0,
135 tx_errors: 0,
136 rx_dropped: 0,
137 tx_dropped: 0,
138 collisions: 0,
139 timestamp: t1,
140 };
141
142 let stats2 = InterfaceStats {
144 interface_name: "test".to_string(),
145 rx_bytes: 500, tx_bytes: 250,
147 rx_packets: 5,
148 tx_packets: 2,
149 rx_errors: 0,
150 tx_errors: 0,
151 rx_dropped: 0,
152 tx_dropped: 0,
153 collisions: 0,
154 timestamp: t2,
155 };
156
157 let rates = stats2.calculate_rates(&stats1);
158 assert_eq!(rates.rx_bytes_per_sec, 0);
160 assert_eq!(rates.tx_bytes_per_sec, 0);
161 }
162}