1#[derive(Clone, Copy, Debug, PartialEq)]
18pub struct TelemetryConfig {
19 pub alpha: f64,
21 pub ref_latency: f64,
25 pub ref_throughput: f64,
27}
28
29impl Default for TelemetryConfig {
30 fn default() -> Self {
31 Self {
32 alpha: 0.1,
33 ref_latency: 0.25,
34 ref_throughput: 262_144.0,
35 }
36 }
37}
38
39#[derive(Clone, Debug)]
41pub struct IoTelemetry {
42 cfg: TelemetryConfig,
43 samples: u64,
44 latency_ewma: f64,
45 throughput_ewma: f64,
46 fail_ewma: f64,
48 failures: u64,
49}
50
51impl IoTelemetry {
52 pub fn new(cfg: TelemetryConfig) -> Self {
53 Self {
54 cfg,
55 samples: 0,
56 latency_ewma: 0.0,
57 throughput_ewma: 0.0,
58 fail_ewma: 0.0,
59 failures: 0,
60 }
61 }
62
63 pub fn record_load(&mut self, latency_secs: f64, bytes: u64) {
67 let a = self.cfg.alpha;
68 if self.samples == 0 {
69 self.latency_ewma = latency_secs.max(0.0);
70 } else {
71 self.latency_ewma += a * (latency_secs.max(0.0) - self.latency_ewma);
72 }
73 self.samples += 1;
74 if latency_secs > 0.0 && bytes > 0 {
75 let thr = bytes as f64 / latency_secs;
76 self.throughput_ewma = if self.throughput_ewma == 0.0 {
77 thr
78 } else {
79 self.throughput_ewma + a * (thr - self.throughput_ewma)
80 };
81 }
82 self.fail_ewma += a * (0.0 - self.fail_ewma);
84 }
85
86 pub fn record_failure(&mut self) {
88 let a = self.cfg.alpha;
89 self.failures += 1;
90 self.fail_ewma = if self.samples + self.failures == 1 {
91 1.0
92 } else {
93 self.fail_ewma + a * (1.0 - self.fail_ewma)
94 };
95 }
96
97 pub fn config(&self) -> &TelemetryConfig {
98 &self.cfg
99 }
100
101 pub fn samples(&self) -> u64 {
103 self.samples
104 }
105
106 pub fn failures(&self) -> u64 {
107 self.failures
108 }
109
110 pub fn latency_ewma(&self) -> f64 {
112 self.latency_ewma
113 }
114
115 pub fn throughput_ewma(&self) -> f64 {
117 self.throughput_ewma
118 }
119
120 pub fn health(&self) -> f32 {
124 if self.samples == 0 {
125 return 1.0;
126 }
127 let lat = (self.cfg.ref_latency / self.latency_ewma.max(1e-9)).clamp(0.0, 1.0);
128 let thr = if self.throughput_ewma > 0.0 {
129 (self.throughput_ewma / self.cfg.ref_throughput).clamp(0.0, 1.0)
130 } else {
131 1.0
132 };
133 let rel = 1.0 - self.fail_ewma.clamp(0.0, 1.0);
134 let h = 0.45 * lat + 0.45 * thr + 0.10 * rel;
136 (h * rel).clamp(0.0, 1.0) as f32
137 }
138
139 pub fn adapt(&self, base: &crate::tier::TierPolicy) -> crate::tier::TierPolicy {
146 let slow = 1.0 - self.health();
147 crate::tier::TierPolicy {
148 radii: base.radii.map(|r| r * (1.0 - 0.25 * slow)),
149 hysteresis: (base.hysteresis + 0.35 * slow).min(0.45),
150 min_dwell: base.min_dwell + 2.0 * slow,
151 }
152 }
153}
154
155impl Default for IoTelemetry {
156 fn default() -> Self {
157 Self::new(TelemetryConfig::default())
158 }
159}
160
161#[cfg(test)]
162mod tests {
163 use super::*;
164 use crate::tier::{Tier, TierPolicy};
165
166 #[test]
167 fn ohne_messungen_ist_alles_gut() {
168 let t = IoTelemetry::default();
169 assert_eq!(t.health(), 1.0);
170 let base = TierPolicy::default();
171 let adapted = t.adapt(&base);
172 assert_eq!(adapted.radii, base.radii);
173 assert_eq!(adapted.hysteresis, base.hysteresis);
174 assert_eq!(adapted.min_dwell, base.min_dwell);
175 }
176
177 #[test]
178 fn schnelle_platte_bleibt_bei_eins() {
179 let mut t = IoTelemetry::default();
180 for _ in 0..50 {
181 t.record_load(0.02, 512 * 1024); }
183 assert!(t.health() > 0.99, "health = {}", t.health());
184 }
185
186 #[test]
187 fn langsame_platte_drueckt_die_gesundheit() {
188 let mut t = IoTelemetry::default();
189 for _ in 0..50 {
190 t.record_load(2.0, 8 * 1024); }
192 assert!(t.health() < 0.2, "health = {}", t.health());
193 assert!(t.latency_ewma() > 1.9);
194 assert!(t.throughput_ewma() > 0.0);
195 }
196
197 #[test]
198 fn fehler_ziehen_zusaetzlich() {
199 let mut sauber = IoTelemetry::default();
200 let mut kaputt = IoTelemetry::default();
201 for _ in 0..30 {
202 sauber.record_load(0.1, 64 * 1024);
203 kaputt.record_load(0.1, 64 * 1024);
204 }
205 for _ in 0..5 {
206 kaputt.record_failure();
207 }
208 assert!(kaputt.health() < sauber.health());
209 assert_eq!(kaputt.failures(), 5);
210 }
211
212 #[test]
213 fn latenz_null_schwaecht_den_durchsatz_nicht() {
214 let mut t = IoTelemetry::default();
217 for _ in 0..10 {
218 t.record_load(0.0, 100);
219 }
220 assert_eq!(t.throughput_ewma(), 0.0);
221 assert_eq!(t.health(), 1.0);
222 }
223
224 #[test]
227 fn langsame_platte_wertet_frueher_ab() {
228 let base = TierPolicy {
229 radii: [25.0, 100.0, 300.0, 800.0],
230 hysteresis: 0.1,
231 min_dwell: 0.5,
232 };
233 let mut t = IoTelemetry::default();
234 for _ in 0..80 {
235 t.record_load(1.5, 4 * 1024);
236 }
237 let slow = t.adapt(&base);
238 assert!(slow.radii[0] < base.radii[0]);
239 assert!(slow.hysteresis > base.hysteresis);
240 assert!(slow.min_dwell > base.min_dwell + 1.0);
241 assert_eq!(base.desired(90.0), Tier::T1);
243 assert_eq!(slow.desired(90.0), Tier::T2);
244 }
245
246 #[test]
248 fn telemetrie_ist_deterministisch() {
249 fn run() -> (f64, f64, f32) {
250 let mut t = IoTelemetry::new(TelemetryConfig {
251 alpha: 0.15,
252 ref_latency: 0.2,
253 ref_throughput: 100_000.0,
254 });
255 for i in 0..40 {
256 t.record_load(0.05 + (i as f64 * 0.37).sin() * 0.05, 900 + i * 13);
257 if i % 7 == 0 {
258 t.record_failure();
259 }
260 }
261 (t.latency_ewma(), t.throughput_ewma(), t.health())
262 }
263 assert_eq!(run(), run());
264 }
265}