#[derive(Debug, Clone)]
pub(crate) struct BurstGapModel {
gmin: u64,
position: u64,
received_since_loss: u64,
open: Option<(u64, u64, u64)>,
bursts: u64,
burst_packets: u64,
burst_losses: u64,
gap_losses: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct BurstGapMetrics {
pub(crate) burst_density: u8,
pub(crate) gap_density: u8,
pub(crate) burst_duration_ms: u16,
pub(crate) gap_duration_ms: u16,
}
fn density(part: u64, whole: u64) -> u8 {
(part * 256)
.checked_div(whole)
.map_or(0, |d| d.min(255) as u8)
}
impl BurstGapModel {
pub(crate) fn new(gmin: u8) -> Self {
Self {
gmin: u64::from(gmin.max(1)),
position: 0,
received_since_loss: 0,
open: None,
bursts: 0,
burst_packets: 0,
burst_losses: 0,
gap_losses: 0,
}
}
pub(crate) fn observe(&mut self, lost: bool, discarded: bool) {
let position = self.position;
self.position += 1;
if !(lost || discarded) {
self.received_since_loss += 1;
return;
}
match self.open {
Some((first, _, count)) if self.received_since_loss < self.gmin => {
self.open = Some((first, position, count + 1));
}
_ => {
self.close();
self.open = Some((position, position, 1));
}
}
self.received_since_loss = 0;
}
fn close(&mut self) {
if let Some((first, last, count)) = self.open.take() {
let (bursts, packets, losses, gap) = Self::closed(first, last, count);
self.bursts += bursts;
self.burst_packets += packets;
self.burst_losses += losses;
self.gap_losses += gap;
}
}
fn closed(first: u64, last: u64, count: u64) -> (u64, u64, u64, u64) {
if count >= 2 {
(1, last - first + 1, count, 0)
} else {
(0, 0, 0, 1)
}
}
pub(crate) fn metrics(&self, packet_ms: f64) -> BurstGapMetrics {
let (mut bursts, mut burst_packets, mut burst_losses, mut gap_losses) = (
self.bursts,
self.burst_packets,
self.burst_losses,
self.gap_losses,
);
if let Some((first, last, count)) = self.open {
let (b, p, l, g) = Self::closed(first, last, count);
bursts += b;
burst_packets += p;
burst_losses += l;
gap_losses += g;
}
let gap_packets = self.position - burst_packets;
let gaps = if bursts == 0 {
u64::from(gap_packets > 0)
} else {
(bursts + 1).min(gap_packets.max(1))
};
let mean_ms = |packets: u64, periods: u64| -> u16 {
if periods == 0 {
0
} else {
(packets as f64 * packet_ms / periods as f64)
.round()
.min(f64::from(u16::MAX)) as u16
}
};
BurstGapMetrics {
burst_density: density(burst_losses, burst_packets),
gap_density: density(gap_losses, gap_packets),
burst_duration_ms: mean_ms(burst_packets, bursts),
gap_duration_ms: mean_ms(gap_packets, gaps),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_example_of_rfc3611_section_4_7_2() {
let pattern = "11110111111111111111111X111X1011110111111111111111111X111111111";
let mut model = BurstGapModel::new(16);
for packet in pattern.chars() {
model.observe(packet == '0', packet == 'X');
}
let metrics = model.metrics(10.0);
assert_eq!(85, metrics.burst_density);
assert_eq!(10, metrics.gap_density);
assert_eq!(120, metrics.burst_duration_ms);
assert_eq!(255, metrics.gap_duration_ms);
}
#[test]
fn without_bursts_the_burst_metrics_are_zero() {
let mut model = BurstGapModel::new(16);
for i in 0..100 {
model.observe(i == 50, false);
}
let metrics = model.metrics(20.0);
assert_eq!(0, metrics.burst_density);
assert_eq!(0, metrics.burst_duration_ms);
assert_eq!(2, metrics.gap_density, "1 in 100, times 256");
assert_eq!(2_000, metrics.gap_duration_ms);
let metrics = BurstGapModel::new(16).metrics(20.0);
assert_eq!(
BurstGapMetrics {
burst_density: 0,
gap_density: 0,
burst_duration_ms: 0,
gap_duration_ms: 0
},
metrics
);
}
}