BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / call / report / series.rs

series.rs

series.rs 5.9 KiB Download Raw
1use std::collections::BTreeMap;
2
3use calling::{CallHealth, Limitation, NetworkLevel, StatsSample};
4
5#[derive(Debug, Clone, Copy, PartialEq)]
6pub struct Series {
7 pub min: f64,
8 pub max: f64,
9 pub last: f64,
10 pub samples: u32,
11}
12
13impl Series {
14 fn new(value: f64) -> Self {
15 Series { min: value, max: value, last: value, samples: 1 }
16 }
17
18 fn observe(&mut self, value: f64) {
19 self.min = self.min.min(value);
20 self.max = self.max.max(value);
21 self.last = value;
22 self.samples += 1;
23 }
24}
25
26#[derive(Debug, Clone, Default, PartialEq)]
27pub struct StatsSeries {
28 values: BTreeMap<&'static str, Series>,
29 pub stalled_audio_in: u32,
30 pub stalled_audio_out: u32,
31 pub level_samples: [u32; 3],
32 pub limitation_samples: [u32; 3],
33 pub cpu_limited_samples: u32,
34 pub level_changes: Vec<(u128, &'static str)>,
35 pub last_level: Option<NetworkLevel>,
36}
37
38pub fn level_name(level: NetworkLevel) -> &'static str {
39 match level {
40 NetworkLevel::Good => "good",
41 NetworkLevel::Poor => "poor",
42 NetworkLevel::Bad => "bad",
43 }
44}
45
46pub fn limitation_name(limitation: Limitation) -> &'static str {
47 match limitation {
48 Limitation::None => "none",
49 Limitation::Cpu => "cpu",
50 Limitation::Bandwidth => "bandwidth",
51 }
52}
53
54impl StatsSeries {
55 pub fn values(&self) -> &BTreeMap<&'static str, Series> {
56 &self.values
57 }
58
59 fn observe(&mut self, name: &'static str, value: f64) {
60 match self.values.get_mut(name) {
61 Some(series) => series.observe(value),
62 None => {
63 self.values.insert(name, Series::new(value));
64 }
65 }
66 }
67
68 pub fn observe_sample(&mut self, sample: &StatsSample) {
69 let video = &sample.video;
70 if let Some(before) = self.values.get("audio_in_packets")
71 && sample.audio_in_packets <= before.last as u64
72 {
73 self.stalled_audio_in += 1;
74 }
75 if let Some(before) = self.values.get("audio_out_packets")
76 && sample.audio_out_packets <= before.last as u64
77 {
78 self.stalled_audio_out += 1;
79 }
80 let readings: [(&'static str, f64); 16] = [
81 ("audio_in_packets", sample.audio_in_packets as f64),
82 ("audio_out_packets", sample.audio_out_packets as f64),
83 ("audio_in_level", sample.audio_in_level),
84 ("audio_out_level", sample.audio_out_level),
85 ("video_in_packets", video.inbound_packets as f64),
86 ("video_in_frames_decoded", video.inbound_frames_decoded as f64),
87 ("video_in_width", f64::from(video.inbound_width)),
88 ("video_in_height", f64::from(video.inbound_height)),
89 ("video_out_packets", video.outbound_packets as f64),
90 ("video_out_frames_encoded", video.outbound_frames_encoded as f64),
91 ("video_out_width", f64::from(video.outbound_width)),
92 ("video_out_height", f64::from(video.outbound_height)),
93 ("video_remote_round_trip_ms", video.remote_round_trip_ms),
94 ("share_in_packets", sample.share_in_packets as f64),
95 ("share_out_packets", sample.share_out_packets as f64),
96 ("video_frames_published", video.frames_published as f64),
97 ];
98 for (name, value) in readings {
99 self.observe(name, value);
100 }
101 }
102
103 pub fn observe_health(&mut self, health: &CallHealth, at_ms: u128) {
104 let sample = &health.sample;
105 for (name, value) in [
106 ("loss_percent", sample.loss_percent),
107 ("round_trip_ms", sample.round_trip_ms),
108 ("jitter_ms", sample.jitter_ms),
109 ("sent_kbps", sample.sent_kbps),
110 ("received_kbps", sample.received_kbps),
111 ] {
112 self.observe(name, value);
113 }
114 self.level_samples[health.level as usize] += 1;
115 self.limitation_samples[sample.limitation as usize] += 1;
116 if health.cpu_limited {
117 self.cpu_limited_samples += 1;
118 }
119 if self.last_level != Some(health.level) {
120 self.level_changes.push((at_ms, level_name(health.level)));
121 self.last_level = Some(health.level);
122 }
123 }
124}
125
126#[cfg(test)]
127mod tests {
128 use calling::{HealthSample, MediaPath, TransportDetails, call::VideoReport};
129
130 use super::*;
131
132 fn sample(audio_in: u64) -> StatsSample {
133 StatsSample {
134 audio_in_packets: audio_in,
135 audio_out_packets: audio_in * 2,
136 video: VideoReport { inbound_width: 1280, inbound_height: 720, ..VideoReport::default() },
137 ..StatsSample::default()
138 }
139 }
140
141 fn health(level: NetworkLevel, loss_percent: f64) -> CallHealth {
142 CallHealth {
143 level,
144 cpu_limited: false,
145 sample: HealthSample { loss_percent, ..HealthSample::default() },
146 transport: TransportDetails::default(),
147 path: MediaPath::Direct,
148 }
149 }
150
151 #[test]
152 fn samples_keep_min_max_last_and_count_stalls() {
153 let mut series = StatsSeries::default();
154 for packets in [10, 20, 20, 35] {
155 series.observe_sample(&sample(packets));
156 }
157 let audio_in = series.values()["audio_in_packets"];
158 assert_eq!((audio_in.min, audio_in.max, audio_in.last, audio_in.samples), (10., 35., 35., 4));
159 assert_eq!(series.stalled_audio_in, 1);
160 assert_eq!(series.values()["video_in_width"].last, 1280.);
161 }
162
163 #[test]
164 fn health_keeps_ranges_and_level_changes() {
165 let mut series = StatsSeries::default();
166 series.observe_health(&health(NetworkLevel::Good, 0.), 2000);
167 series.observe_health(&health(NetworkLevel::Poor, 7.), 4000);
168 series.observe_health(&health(NetworkLevel::Poor, 6.), 6000);
169 assert_eq!(series.values()["loss_percent"].max, 7.);
170 assert_eq!(series.level_samples, [1, 2, 0]);
171 assert_eq!(series.level_changes, [(2000, "good"), (4000, "poor")]);
172 }
173}