Codebase list golang-github-beorn7-perks / c4274eb
Count and Query fixed for uncompressed Stream (fixes #1) Blake Mizerany 10 years ago
2 changed file(s) with 26 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
108108 // Fast path when there hasn't been enough data for a flush;
109109 // this also yeilds better accuracy for small sets of data.
110110 i := float64(len(s.b)) * q
111 return s.b[int(i)].Value
111 return s.b[int(i)-1].Value
112112 }
113113 s.flush()
114114 return s.stream.query(q)
132132 return s.b
133133 }
134134 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()
135141 }
136142
137143 func (s *Stream) flush() {
197203 }
198204 }
199205
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 {
203207 return int(s.n)
204208 }
205209
7979 }
8080 }
8181
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
82100 func getPerc(x []float64, p float64) float64 {
83101 k := int(float64(len(x)) * p)
84102 return x[k]