Set default percentile window to 256 samples

This commit is contained in:
Justin Li 2014-07-22 23:13:59 -04:00
parent b6f0fc79cb
commit 727370fedc
3 changed files with 20 additions and 10 deletions

View file

@ -19,9 +19,19 @@ type Percentile struct {
value uint64 // These bits are really a float64. value uint64 // These bits are really a float64.
} }
// NewPercentile returns a Percentile with a given threshold // NewPercentile returns a Percentile with a given threshold.
func NewPercentile(percentile float64) *Percentile {
return &Percentile{
percentile: percentile,
// 256 samples is fast, and accurate for most distributions.
values: make([]float64, 0, 256),
}
}
// NewPercentileWithWindow returns a Percentile with a given threshold
// and window size (accuracy). // and window size (accuracy).
func NewPercentile(percentile float64, sampleWindow int) *Percentile { func NewPercentileWithWindow(percentile float64, sampleWindow int) *Percentile {
return &Percentile{ return &Percentile{
percentile: percentile, percentile: percentile,
values: make([]float64, 0, sampleWindow), values: make([]float64, 0, sampleWindow),

View file

@ -45,7 +45,7 @@ func logNorm(n int, scale float64) sort.Float64Slice {
} }
func testSlice(t *testing.T, numbers sort.Float64Slice, percentile float64) { func testSlice(t *testing.T, numbers sort.Float64Slice, percentile float64) {
p := NewPercentile(percentile, 256) p := NewPercentile(percentile)
for i := 0; i < len(numbers); i++ { for i := 0; i < len(numbers); i++ {
p.AddSample(numbers[i]) p.AddSample(numbers[i])
@ -53,10 +53,10 @@ func testSlice(t *testing.T, numbers sort.Float64Slice, percentile float64) {
sort.Sort(numbers) sort.Sort(numbers)
got := p.Value() got := p.Value()
expected := numbers[round(float64(len(numbers))*percentile)] index := round(float64(len(numbers)) * percentile)
if got != expected { if got != numbers[index] && got != numbers[index-1] && got != numbers[index+1] {
t.Errorf("Percentile incorrect\n actual: %f\nexpected: %f\n error: %f%%\n", got, expected, (got-expected)/expected*100) t.Errorf("Percentile incorrect\n actual: %f\nexpected: %f, %f, %f\n", got, numbers[index-1], numbers[index], numbers[index+1])
} }
} }
@ -85,7 +85,7 @@ func BenchmarkLNPercentiles256(b *testing.B) {
} }
func benchmarkSlice(b *testing.B, numbers sort.Float64Slice, window int, percentile float64) { func benchmarkSlice(b *testing.B, numbers sort.Float64Slice, window int, percentile float64) {
p := NewPercentile(percentile, window) p := NewPercentileWithWindow(percentile, window)
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {

View file

@ -101,9 +101,9 @@ func New(chanSize int) *Stats {
responseTimeEvents: make(chan time.Duration, chanSize), responseTimeEvents: make(chan time.Duration, chanSize),
ResponseTime: PercentileTimes{ ResponseTime: PercentileTimes{
P50: NewPercentile(0.5, 128), P50: NewPercentile(0.5),
P90: NewPercentile(0.9, 128), P90: NewPercentile(0.9),
P95: NewPercentile(0.95, 128), P95: NewPercentile(0.95),
}, },
} }