BlueGit

ict rusty-teams

Files

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

series.rs

8629da58 History
series.rs 6.6 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, others_unmuted: bool, own_unmuted: bool) {
69 let video = &sample.video;
70 if let Some(before) = self.values.get("audio_in_packets")
71 && others_unmuted
72 && sample.audio_in_packets <= before.last as u64
73 {
74 self.stalled_audio_in += 1;
75 }
76 if let Some(before) = self.values.get("audio_out_packets")
77 && own_unmuted
78 && sample.audio_out_packets <= before.last as u64
79 {
80 self.stalled_audio_out += 1;
81 }
82 let readings: [(&'static str, f64); 16] = [
83 ("audio_in_packets", sample.audio_in_packets as f64),
84 ("audio_out_packets", sample.audio_out_packets as f64),
85 ("audio_in_level", sample.audio_in_level),
86 ("audio_out_level", sample.audio_out_level),
87 ("video_in_packets", video.inbound_packets as f64),
88 ("video_in_frames_decoded", video.inbound_frames_decoded as f64),
89 ("video_in_width", f64::from(video.inbound_width)),
90 ("video_in_height", f64::from(video.inbound_height)),
91 ("video_out_packets", video.outbound_packets as f64),
92 ("video_out_frames_encoded", video.outbound_frames_encoded as f64),
93 ("video_out_width", f64::from(video.outbound_width)),
94 ("video_out_height", f64::from(video.outbound_height)),
95 ("video_remote_round_trip_ms", video.remote_round_trip_ms),
96 ("share_in_packets", sample.share_in_packets as f64),
97 ("share_out_packets", sample.share_out_packets as f64),
98 ("video_frames_published", video.frames_published as f64),
99 ];
100 for (name, value) in readings {
101 self.observe(name, value);
102 }
103 }
104
105 pub fn observe_health(&mut self, health: &CallHealth, at_ms: u128) {
106 let sample = &health.sample;
107 for (name, value) in [
108 ("loss_percent", sample.loss_percent),
109 ("round_trip_ms", sample.round_trip_ms),
110 ("jitter_ms", sample.jitter_ms),
111 ("sent_kbps", sample.sent_kbps),
112 ("received_kbps", sample.received_kbps),
113 ] {
114 self.observe(name, value);
115 }
116 self.level_samples[health.level as usize] += 1;
117 self.limitation_samples[sample.limitation as usize] += 1;
118 if health.cpu_limited {
119 self.cpu_limited_samples += 1;
120 }
121 if self.last_level != Some(health.level) {
122 self.level_changes.push((at_ms, level_name(health.level)));
123 self.last_level = Some(health.level);
124 }
125 }
126}
127
128#[cfg(test)]
129mod tests {
130 use calling::{HealthSample, MediaPath, TransportDetails, call::VideoReport};
131
132 use super::*;
133
134 fn sample(audio_in: u64) -> StatsSample {
135 StatsSample {
136 audio_in_packets: audio_in,
137 audio_out_packets: audio_in * 2,
138 video: VideoReport { inbound_width: 1280, inbound_height: 720, ..VideoReport::default() },
139 ..StatsSample::default()
140 }
141 }
142
143 fn health(level: NetworkLevel, loss_percent: f64) -> CallHealth {
144 CallHealth {
145 level,
146 cpu_limited: false,
147 sample: HealthSample { loss_percent, ..HealthSample::default() },
148 transport: TransportDetails::default(),
149 path: MediaPath::Direct,
150 }
151 }
152
153 #[test]
154 fn samples_keep_min_max_last_and_count_stalls() {
155 let mut series = StatsSeries::default();
156 for packets in [10, 20, 20, 35] {
157 series.observe_sample(&sample(packets), true, true);
158 }
159 let audio_in = series.values()["audio_in_packets"];
160 assert_eq!((audio_in.min, audio_in.max, audio_in.last, audio_in.samples), (10., 35., 35., 4));
161 assert_eq!(series.stalled_audio_in, 1);
162 assert_eq!(series.values()["video_in_width"].last, 1280.);
163 }
164
165 #[test]
166 fn stalls_only_count_while_the_sender_could_be_heard() {
167 let mut series = StatsSeries::default();
168 for packets in [10, 10, 10] {
169 series.observe_sample(&sample(packets), false, false);
170 }
171 assert_eq!((series.stalled_audio_in, series.stalled_audio_out), (0, 0));
172 series.observe_sample(&sample(10), true, false);
173 assert_eq!((series.stalled_audio_in, series.stalled_audio_out), (1, 0));
174 series.observe_sample(&sample(10), false, true);
175 assert_eq!((series.stalled_audio_in, series.stalled_audio_out), (1, 1));
176 }
177
178 #[test]
179 fn health_keeps_ranges_and_level_changes() {
180 let mut series = StatsSeries::default();
181 series.observe_health(&health(NetworkLevel::Good, 0.), 2000);
182 series.observe_health(&health(NetworkLevel::Poor, 7.), 4000);
183 series.observe_health(&health(NetworkLevel::Poor, 6.), 6000);
184 assert_eq!(series.values()["loss_percent"].max, 7.);
185 assert_eq!(series.level_samples, [1, 2, 0]);
186 assert_eq!(series.level_changes, [(2000, "good"), (4000, "poor")]);
187 }
188}