Fix panic in statsd p100 calculation (#3230)

This commit is contained in:
Trevor Pounds 2017-09-14 18:27:42 -04:00 committed by Daniel Nelson
parent 103ae3b710
commit 73372872c2
2 changed files with 29 additions and 4 deletions

View File

@ -110,8 +110,15 @@ func (rs *RunningStats) Percentile(n int) float64 {
} }
i := int(float64(len(rs.perc)) * float64(n) / float64(100)) i := int(float64(len(rs.perc)) * float64(n) / float64(100))
if i < 0 { return rs.perc[clamp(i, 0, len(rs.perc)-1)]
i = 0
} }
return rs.perc[i]
func clamp(i int, min int, max int) int {
if i < min {
return min
}
if i > max {
return max
}
return i
} }

View File

@ -23,12 +23,18 @@ func TestRunningStats_Single(t *testing.T) {
if rs.Lower() != 10.1 { if rs.Lower() != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Lower()) t.Errorf("Expected %v, got %v", 10.1, rs.Lower())
} }
if rs.Percentile(100) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(100))
}
if rs.Percentile(90) != 10.1 { if rs.Percentile(90) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(90)) t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(90))
} }
if rs.Percentile(50) != 10.1 { if rs.Percentile(50) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(50)) t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(50))
} }
if rs.Percentile(0) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(0))
}
if rs.Count() != 1 { if rs.Count() != 1 {
t.Errorf("Expected %v, got %v", 1, rs.Count()) t.Errorf("Expected %v, got %v", 1, rs.Count())
} }
@ -58,12 +64,18 @@ func TestRunningStats_Duplicate(t *testing.T) {
if rs.Lower() != 10.1 { if rs.Lower() != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Lower()) t.Errorf("Expected %v, got %v", 10.1, rs.Lower())
} }
if rs.Percentile(100) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(100))
}
if rs.Percentile(90) != 10.1 { if rs.Percentile(90) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(90)) t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(90))
} }
if rs.Percentile(50) != 10.1 { if rs.Percentile(50) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(50)) t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(50))
} }
if rs.Percentile(0) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(0))
}
if rs.Count() != 4 { if rs.Count() != 4 {
t.Errorf("Expected %v, got %v", 4, rs.Count()) t.Errorf("Expected %v, got %v", 4, rs.Count())
} }
@ -93,12 +105,18 @@ func TestRunningStats(t *testing.T) {
if rs.Lower() != 5 { if rs.Lower() != 5 {
t.Errorf("Expected %v, got %v", 5, rs.Lower()) t.Errorf("Expected %v, got %v", 5, rs.Lower())
} }
if rs.Percentile(100) != 45 {
t.Errorf("Expected %v, got %v", 45, rs.Percentile(100))
}
if rs.Percentile(90) != 32 { if rs.Percentile(90) != 32 {
t.Errorf("Expected %v, got %v", 32, rs.Percentile(90)) t.Errorf("Expected %v, got %v", 32, rs.Percentile(90))
} }
if rs.Percentile(50) != 11 { if rs.Percentile(50) != 11 {
t.Errorf("Expected %v, got %v", 11, rs.Percentile(50)) t.Errorf("Expected %v, got %v", 11, rs.Percentile(50))
} }
if rs.Percentile(0) != 5 {
t.Errorf("Expected %v, got %v", 5, rs.Percentile(0))
}
if rs.Count() != 16 { if rs.Count() != 16 {
t.Errorf("Expected %v, got %v", 4, rs.Count()) t.Errorf("Expected %v, got %v", 4, rs.Count())
} }