diff options
-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; +} |