Count and Query fixed for uncompressed Stream (fixes #1)
Blake Mizerany
10 years ago
108 | 108 |
// Fast path when there hasn't been enough data for a flush;
|
109 | 109 |
// this also yeilds better accuracy for small sets of data.
|
110 | 110 |
i := float64(len(s.b)) * q
|
111 | |
return s.b[int(i)].Value
|
|
111 |
return s.b[int(i)-1].Value
|
112 | 112 |
}
|
113 | 113 |
s.flush()
|
114 | 114 |
return s.stream.query(q)
|
|
132 | 132 |
return s.b
|
133 | 133 |
}
|
134 | 134 |
return s.stream.samples()
|
|
135 |
}
|
|
136 |
|
|
137 |
// Count returns the total number of samples observed in the stream
|
|
138 |
// since initialization.
|
|
139 |
func (s *Stream) Count() int {
|
|
140 |
return len(s.b) + s.stream.count()
|
135 | 141 |
}
|
136 | 142 |
|
137 | 143 |
func (s *Stream) flush() {
|
|
197 | 203 |
}
|
198 | 204 |
}
|
199 | 205 |
|
200 | |
// Count returns the total number of samples observed in the stream
|
201 | |
// since initialization.
|
202 | |
func (s *stream) Count() int {
|
|
206 |
func (s *stream) count() int {
|
203 | 207 |
return int(s.n)
|
204 | 208 |
}
|
205 | 209 |
|
79 | 79 |
}
|
80 | 80 |
}
|
81 | 81 |
|
|
82 |
func TestUncompressed(t *testing.T) {
|
|
83 |
tests := []float64{0.50, 0.90, 0.95, 0.99}
|
|
84 |
q := NewTargeted(tests...)
|
|
85 |
for i := 1; i <= 100; i++ {
|
|
86 |
q.Insert(float64(i))
|
|
87 |
}
|
|
88 |
if g := q.Count(); g != 100 {
|
|
89 |
t.Errorf("want count 100, got %d", g)
|
|
90 |
}
|
|
91 |
// Before compression, Query should have 100% accuracy.
|
|
92 |
for _, v := range tests {
|
|
93 |
w := v * 100
|
|
94 |
if g := q.Query(v); g != w {
|
|
95 |
t.Errorf("want %f, got %f", w, g)
|
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
82 | 100 |
func getPerc(x []float64, p float64) float64 {
|
83 | 101 |
k := int(float64(len(x)) * p)
|
84 | 102 |
return x[k]
|