1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
mod aggregated;
mod per_client;
pub use crate::stats::aggregated::AggregatedStats;
pub use crate::stats::per_client::PerClientStats;
use hashbrown::hash_map::Iter;
use std::net::IpAddr;
#[derive(Debug, Clone, Copy)]
pub struct ClientStatEntry {
pub valid_requests: u64,
pub invalid_requests: u64,
pub health_checks: u64,
pub responses_sent: u64,
pub bytes_sent: usize,
}
impl ClientStatEntry {
fn new() -> Self {
ClientStatEntry {
valid_requests: 0,
invalid_requests: 0,
health_checks: 0,
responses_sent: 0,
bytes_sent: 0,
}
}
}
pub trait ServerStats {
fn add_valid_request(&mut self, addr: &IpAddr);
fn add_invalid_request(&mut self, addr: &IpAddr);
fn add_health_check(&mut self, addr: &IpAddr);
fn add_response(&mut self, addr: &IpAddr, bytes_sent: usize);
fn total_valid_requests(&self) -> u64;
fn total_invalid_requests(&self) -> u64;
fn total_health_checks(&self) -> u64;
fn total_responses_sent(&self) -> u64;
fn total_bytes_sent(&self) -> usize;
fn total_unique_clients(&self) -> u64;
fn stats_for_client(&self, addr: &IpAddr) -> Option<&ClientStatEntry>;
fn iter(&self) -> Iter<IpAddr, ClientStatEntry>;
fn clear(&mut self);
}
#[cfg(test)]
mod test {
use crate::stats::{ServerStats, PerClientStats};
use std::net::{IpAddr, Ipv4Addr};
#[test]
fn simple_stats_starts_empty() {
let stats = PerClientStats::new();
assert_eq!(stats.total_valid_requests(), 0);
assert_eq!(stats.total_invalid_requests(), 0);
assert_eq!(stats.total_health_checks(), 0);
assert_eq!(stats.total_responses_sent(), 0);
assert_eq!(stats.total_bytes_sent(), 0);
assert_eq!(stats.total_unique_clients(), 0);
assert_eq!(stats.num_overflows(), 0);
}
#[test]
fn client_requests_are_tracked() {
let mut stats = PerClientStats::new();
let ip1 = "127.0.0.1".parse().unwrap();
let ip2 = "127.0.0.2".parse().unwrap();
let ip3 = "127.0.0.3".parse().unwrap();
stats.add_valid_request(&ip1);
stats.add_valid_request(&ip2);
stats.add_valid_request(&ip3);
assert_eq!(stats.total_valid_requests(), 3);
stats.add_invalid_request(&ip2);
assert_eq!(stats.total_invalid_requests(), 1);
stats.add_response(&ip2, 8192);
assert_eq!(stats.total_bytes_sent(), 8192);
assert_eq!(stats.total_unique_clients(), 3);
}
#[test]
fn per_client_stats() {
let mut stats = PerClientStats::new();
let ip = "127.0.0.3".parse().unwrap();
stats.add_valid_request(&ip);
stats.add_response(&ip, 2048);
stats.add_response(&ip, 1024);
let entry = stats.stats_for_client(&ip).unwrap();
assert_eq!(entry.valid_requests, 1);
assert_eq!(entry.invalid_requests, 0);
assert_eq!(entry.responses_sent, 2);
assert_eq!(entry.bytes_sent, 3072);
}
#[test]
fn overflow_max_entries() {
let mut stats = PerClientStats::with_limit(100);
for i in 0..201 {
let ipv4 = Ipv4Addr::from(i as u32);
let addr = IpAddr::from(ipv4);
stats.add_valid_request(&addr);
};
assert_eq!(stats.total_unique_clients(), 100);
assert_eq!(stats.num_overflows(), 101);
}
}