diff options
author | Tobias Oetiker <tobi@oetiker.ch> | 2011-11-22 09:25:02 +0100 |
---|---|---|
committer | Tobias Oetiker <tobi@oetiker.ch> | 2011-11-22 09:25:02 +0100 |
commit | 1c6f909c977061ace3e5bc00af8adc8b1b8b84ab (patch) | |
tree | cf5722a8c9ccc341c8c66880a9ab6d5cc967f455 | |
parent | 1e9842ca33991ba4c395ed1a457866c7189d8aa6 (diff) | |
download | smokeping-1c6f909c977061ace3e5bc00af8adc8b1b8b84ab.tar.gz smokeping-1c6f909c977061ace3e5bc00af8adc8b1b8b84ab.tar.xz |
make Median matcher work fine on startup and when unknown values are in the stream
-rw-r--r-- | lib/Smokeping/matchers/Median.pm | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/lib/Smokeping/matchers/Median.pm b/lib/Smokeping/matchers/Median.pm index 7dbad43..24be199 100644 --- a/lib/Smokeping/matchers/Median.pm +++ b/lib/Smokeping/matchers/Median.pm @@ -85,7 +85,14 @@ sub Test($$) my $ac = $self->{param}{old}; my $bc = $self->{param}{new}; my $cc = $ac +$bc; - my $oldm = (sort {$a <=> $b} @{$data->{rtt}}[-$cc..-$bc-1])[int($ac/2)]; - my $newm = (sort {$a <=> $b} @{$data->{rtt}}[-$bc..-1])[int($bc/2)]; + my $oldm = robust_median(@{$data->{rtt}}[-$cc..-$bc-1]); + my $newm = robust_median(@{$data->{rtt}}[-$bc..-1]); return abs($oldm-$newm) > $self->{param}{diff}; } + +sub robust_median(@){ + my @numbers = sort {$a <=> $b} grep { defined $_ and $_ =~ /\d/ } @_; + my $count = scalar @numbers; + return 0 if $count == 0; + return ($count / 2 == int($count/2)) ? $numbers[$count/2] : ($numbers[$count/2+0.5] + $numbers[$count/2-0.5])/2; +} |