diff --git a/quantile/stream.go b/quantile/stream.go index d0c5ff6..499d569 100644 --- a/quantile/stream.go +++ b/quantile/stream.go @@ -109,7 +109,7 @@ // Fast path when there hasn't been enough data for a flush; // this also yeilds better accuracy for small sets of data. i := float64(len(s.b)) * q - return s.b[int(i)].Value + return s.b[int(i)-1].Value } s.flush() return s.stream.query(q) @@ -133,6 +133,12 @@ return s.b } return s.stream.samples() +} + +// Count returns the total number of samples observed in the stream +// since initialization. +func (s *Stream) Count() int { + return len(s.b) + s.stream.count() } func (s *Stream) flush() { @@ -198,9 +204,7 @@ } } -// Count returns the total number of samples observed in the stream -// since initialization. -func (s *stream) Count() int { +func (s *stream) count() int { return int(s.n) } diff --git a/quantile/stream_test.go b/quantile/stream_test.go index b7e599d..0986550 100644 --- a/quantile/stream_test.go +++ b/quantile/stream_test.go @@ -80,6 +80,24 @@ } } +func TestUncompressed(t *testing.T) { + tests := []float64{0.50, 0.90, 0.95, 0.99} + q := NewTargeted(tests...) + for i := 1; i <= 100; i++ { + q.Insert(float64(i)) + } + if g := q.Count(); g != 100 { + t.Errorf("want count 100, got %d", g) + } + // Before compression, Query should have 100% accuracy. + for _, v := range tests { + w := v * 100 + if g := q.Query(v); g != w { + t.Errorf("want %f, got %f", w, g) + } + } +} + func getPerc(x []float64, p float64) float64 { k := int(float64(len(x)) * p) return x[k]