]> git.sesse.net Git - bursty/blob - summarize.pl
Fix some corruption issues in truncate-end.pl.
[bursty] / summarize.pl
1 #! /usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 sub wilson_interval {
7         my ($x, $n) = @_;
8         my $z = 1.96;  # 95% confidence interval
9
10         my $p = $x / $n;
11         my $min = ($p + $z * $z / (2 * $n) - $z * sqrt($p * (1 - $p) / $n + $z * $z / (4 * $n * $n))) / (1 + $z * $z / $n);
12         my $max = ($p + $z * $z / (2 * $n) + $z * sqrt($p * (1 - $p) / $n + $z * $z / (4 * $n * $n))) / (1 + $z * $z / $n);
13
14         $min = 0 if ($min < 0);
15
16         return ($min, $max);
17 }
18
19 my %buckets = ();
20
21 while (<>) {
22         chomp;
23         my (@x) = split /\s+/, $_;
24
25         my $num_packets = scalar @x;
26         my $num_drops = grep { /\*/ } @x;
27
28         $buckets{$num_packets}->{'num_bursts'}++;
29         $buckets{$num_packets}->{'num_drops'} += $num_drops;
30         if ($num_drops > 0) {
31                 $buckets{$num_packets}->{'bursts_with_drops'}++;
32         }
33 }
34
35 for my $n (sort { $a <=> $b } keys %buckets) {
36         my $num_bursts = $buckets{$n}->{'num_bursts'} // 0;
37         my $num_drops = $buckets{$n}->{'num_drops'} // 0;
38         my $bursts_with_drops = $buckets{$n}->{'bursts_with_drops'} // 0;
39
40         my $drop_rate = $num_drops / ($n * $num_bursts);
41         my $drop_event_rate = $bursts_with_drops / $num_bursts;
42
43         my ($drop_rate_min, $drop_rate_max) = wilson_interval($num_drops, $n * $num_bursts);
44         my ($drop_event_rate_min, $drop_event_rate_max) = wilson_interval($bursts_with_drops, $num_bursts);
45
46         printf "%3d: drop_rate %.5f  95%%ci = [%.5f, %.5f] (%4d/%8d)   drop_event_rate %.5f  95%%ci = [%.5f, %.5f] (%3d/%4d)\n",
47                 $n,
48                 $drop_rate, $drop_rate_min, $drop_rate_max, $num_drops, $n * $num_bursts,
49                 $drop_event_rate, $drop_event_rate_min, $drop_event_rate_max, $bursts_with_drops, $num_bursts;
50 }