From 1c6f909c977061ace3e5bc00af8adc8b1b8b84ab Mon Sep 17 00:00:00 2001 From: Tobias Oetiker Date: Tue, 22 Nov 2011 09:25:02 +0100 Subject: make Median matcher work fine on startup and when unknown values are in the stream --- lib/Smokeping/matchers/Median.pm | 11 +++++++++-- 1 file 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; +} -- cgit v1.2.3-24-g4f1b