Debian Codegraph @

sample_test.go

raw
1 package metrics
2
3 import (
4 "math"
5 "math/rand"
6 "runtime"
7 "testing"
8 "time"
9 )
10
11 func float64NotEqual(a, b float64) bool {
12 v := math.Abs(a - b)
13 if b == 0.0 && v > 0.00001 {
14 return false
15 }
16 return math.Abs(v/b) > 0.00001
17 }
18
19 // Benchmark{Compute,Copy}{1000,1000000} demonstrate that, even for relatively
20 // expensive computations like Variance, the cost of copying the Sample, as
21 // approximated by a make and copy, is much greater than the cost of the
22 // computation for small samples and only slightly less for large samples.
23 func BenchmarkCompute1000(b *testing.B) {
24 s := make([]int64, 1000)
25 for i := 0; i < len(s); i++ {
26 s[i] = int64(i)
27 }
28 b.ResetTimer()
29 for i := 0; i < b.N; i++ {
30 SampleVariance(s)
31 }
32 }
33 func BenchmarkCompute1000000(b *testing.B) {
34 s := make([]int64, 1000000)
35 for i := 0; i < len(s); i++ {
36 s[i] = int64(i)
37 }
38 b.ResetTimer()
39 for i := 0; i < b.N; i++ {
40 SampleVariance(s)
41 }
42 }
43 func BenchmarkCopy1000(b *testing.B) {
44 s := make([]int64, 1000)
45 for i := 0; i < len(s); i++ {
46 s[i] = int64(i)
47 }
48 b.ResetTimer()
49 for i := 0; i < b.N; i++ {
50 sCopy := make([]int64, len(s))
51 copy(sCopy, s)
52 }
53 }
54 func BenchmarkCopy1000000(b *testing.B) {
55 s := make([]int64, 1000000)
56 for i := 0; i < len(s); i++ {
57 s[i] = int64(i)
58 }
59 b.ResetTimer()
60 for i := 0; i < b.N; i++ {
61 sCopy := make([]int64, len(s))
62 copy(sCopy, s)
63 }
64 }
65
66 func BenchmarkExpDecaySample257(b *testing.B) {
67 benchmarkSample(b, NewExpDecaySample(257, 0.015))
68 }
69
70 func BenchmarkExpDecaySample514(b *testing.B) {
71 benchmarkSample(b, NewExpDecaySample(514, 0.015))
72 }
73
74 func BenchmarkExpDecaySample1028(b *testing.B) {
75 benchmarkSample(b, NewExpDecaySample(1028, 0.015))
76 }
77
78 func BenchmarkUniformSample257(b *testing.B) {
79 benchmarkSample(b, NewUniformSample(257))
80 }
81
82 func BenchmarkUniformSample514(b *testing.B) {
83 benchmarkSample(b, NewUniformSample(514))
84 }
85
86 func BenchmarkUniformSample1028(b *testing.B) {
87 benchmarkSample(b, NewUniformSample(1028))
88 }
89
90 func TestExpDecaySample10(t *testing.T) {
91 rand.Seed(1)
92 s := NewExpDecaySample(100, 0.99)
93 for i := 0; i < 10; i++ {
94 s.Update(int64(i))
95 }
96 if size := s.Count(); 10 != size {
97 t.Errorf("s.Count(): 10 != %v\n", size)
98 }
99 if size := s.Size(); 10 != size {
100 t.Errorf("s.Size(): 10 != %v\n", size)
101 }
102 if l := len(s.Values()); 10 != l {
103 t.Errorf("len(s.Values()): 10 != %v\n", l)
104 }
105 for _, v := range s.Values() {
106 if v > 10 || v < 0 {
107 t.Errorf("out of range [0, 10): %v\n", v)
108 }
109 }
110 }
111
112 func TestExpDecaySample100(t *testing.T) {
113 rand.Seed(1)
114 s := NewExpDecaySample(1000, 0.01)
115 for i := 0; i < 100; i++ {
116 s.Update(int64(i))
117 }
118 if size := s.Count(); 100 != size {
119 t.Errorf("s.Count(): 100 != %v\n", size)
120 }
121 if size := s.Size(); 100 != size {
122 t.Errorf("s.Size(): 100 != %v\n", size)
123 }
124 if l := len(s.Values()); 100 != l {
125 t.Errorf("len(s.Values()): 100 != %v\n", l)
126 }
127 for _, v := range s.Values() {
128 if v > 100 || v < 0 {
129 t.Errorf("out of range [0, 100): %v\n", v)
130 }
131 }
132 }
133
134 func TestExpDecaySample1000(t *testing.T) {
135 rand.Seed(1)
136 s := NewExpDecaySample(100, 0.99)
137 for i := 0; i < 1000; i++ {
138 s.Update(int64(i))
139 }
140 if size := s.Count(); 1000 != size {
141 t.Errorf("s.Count(): 1000 != %v\n", size)
142 }
143 if size := s.Size(); 100 != size {
144 t.Errorf("s.Size(): 100 != %v\n", size)
145 }
146 if l := len(s.Values()); 100 != l {
147 t.Errorf("len(s.Values()): 100 != %v\n", l)
148 }
149 for _, v := range s.Values() {
150 if v > 1000 || v < 0 {
151 t.Errorf("out of range [0, 1000): %v\n", v)
152 }
153 }
154 }
155
156 // This test makes sure that the sample's priority is not amplified by using
157 // nanosecond duration since start rather than second duration since start.
158 // The priority becomes +Inf quickly after starting if this is done,
159 // effectively freezing the set of samples until a rescale step happens.
160 func TestExpDecaySampleNanosecondRegression(t *testing.T) {
161 rand.Seed(1)
162 s := NewExpDecaySample(100, 0.99)
163 for i := 0; i < 100; i++ {
164 s.Update(10)
165 }
166 time.Sleep(1 * time.Millisecond)
167 for i := 0; i < 100; i++ {
168 s.Update(20)
169 }
170 v := s.Values()
171 avg := float64(0)
172 for i := 0; i < len(v); i++ {
173 avg += float64(v[i])
174 }
175 avg /= float64(len(v))
176 if avg > 16 || avg < 14 {
177 t.Errorf("out of range [14, 16]: %v\n", avg)
178 }
179 }
180
181 func TestExpDecaySampleRescale(t *testing.T) {
182 s := NewExpDecaySample(2, 0.001).(*ExpDecaySample)
183 s.update(time.Now(), 1)
184 s.update(time.Now().Add(time.Hour+time.Microsecond), 1)
185 for _, v := range s.values.Values() {
186 if v.k == 0.0 {
187 t.Fatal("v.k == 0.0")
188 }
189 }
190 }
191
192 func TestExpDecaySampleSnapshot(t *testing.T) {
193 now := time.Now()
194 rand.Seed(1)
195 s := NewExpDecaySample(100, 0.99)
196 for i := 1; i <= 10000; i++ {
197 s.(*ExpDecaySample).update(now.Add(time.Duration(i)), int64(i))
198 }
199 snapshot := s.Snapshot()
200 s.Update(1)
201 testExpDecaySampleStatistics(t, snapshot)
202 }
203
204 func TestExpDecaySampleStatistics(t *testing.T) {
205 now := time.Now()
206 rand.Seed(1)
207 s := NewExpDecaySample(100, 0.99)
208 for i := 1; i <= 10000; i++ {
209 s.(*ExpDecaySample).update(now.Add(time.Duration(i)), int64(i))
210 }
211 testExpDecaySampleStatistics(t, s)
212 }
213
214 func TestUniformSample(t *testing.T) {
215 rand.Seed(1)
216 s := NewUniformSample(100)
217 for i := 0; i < 1000; i++ {
218 s.Update(int64(i))
219 }
220 if size := s.Count(); 1000 != size {
221 t.Errorf("s.Count(): 1000 != %v\n", size)
222 }
223 if size := s.Size(); 100 != size {
224 t.Errorf("s.Size(): 100 != %v\n", size)
225 }
226 if l := len(s.Values()); 100 != l {
227 t.Errorf("len(s.Values()): 100 != %v\n", l)
228 }
229 for _, v := range s.Values() {
230 if v > 1000 || v < 0 {
231 t.Errorf("out of range [0, 100): %v\n", v)
232 }
233 }
234 }
235
236 func TestUniformSampleIncludesTail(t *testing.T) {
237 rand.Seed(1)
238 s := NewUniformSample(100)
239 max := 100
240 for i := 0; i < max; i++ {
241 s.Update(int64(i))
242 }
243 v := s.Values()
244 sum := 0
245 exp := (max - 1) * max / 2
246 for i := 0; i < len(v); i++ {
247 sum += int(v[i])
248 }
249 if exp != sum {
250 t.Errorf("sum: %v != %v\n", exp, sum)
251 }
252 }
253
254 func TestUniformSampleSnapshot(t *testing.T) {
255 s := NewUniformSample(100)
256 for i := 1; i <= 10000; i++ {
257 s.Update(int64(i))
258 }
259 snapshot := s.Snapshot()
260 s.Update(1)
261 testUniformSampleStatistics(t, snapshot)
262 }
263
264 func TestUniformSampleStatistics(t *testing.T) {
265 rand.Seed(1)
266 s := NewUniformSample(100)
267 for i := 1; i <= 10000; i++ {
268 s.Update(int64(i))
269 }
270 testUniformSampleStatistics(t, s)
271 }
272
273 func benchmarkSample(b *testing.B, s Sample) {
274 var memStats runtime.MemStats
275 runtime.ReadMemStats(&memStats)
276 pauseTotalNs := memStats.PauseTotalNs
277 b.ResetTimer()
278 for i := 0; i < b.N; i++ {
279 s.Update(1)
280 }
281 b.StopTimer()
282 runtime.GC()
283 runtime.ReadMemStats(&memStats)
284 b.Logf("GC cost: %d ns/op", int(memStats.PauseTotalNs-pauseTotalNs)/b.N)
285 }
286
287 func testExpDecaySampleStatistics(t *testing.T, s Sample) {
288 if count := s.Count(); 10000 != count {
289 t.Errorf("s.Count(): 10000 != %v\n", count)
290 }
291 if min := s.Min(); 107 != min {
292 t.Errorf("s.Min(): 107 != %v\n", min)
293 }
294 if max := s.Max(); 10000 != max {
295 t.Errorf("s.Max(): 10000 != %v\n", max)
296 }
297 if mean := s.Mean(); float64NotEqual(4965.98, mean) {
298 t.Errorf("s.Mean(): 4965.98 != %v\n", mean)
299 }
300 if stdDev := s.StdDev(); float64NotEqual(2959.825156930727, stdDev) {
301 t.Errorf("s.StdDev(): 2959.825156930727 != %v\n", stdDev)
302 }
303 ps := s.Percentiles([]float64{0.5, 0.75, 0.99})
304 if float64NotEqual(4615, ps[0]) {
305 t.Errorf("median: 4615 != %v\n", ps[0])
306 }
307 if float64NotEqual(7672, ps[1]) {
308 t.Errorf("75th percentile: 7672 != %v\n", ps[1])
309 }
310 if float64NotEqual(9998.99, ps[2]) {
311 t.Errorf("99th percentile: 9998.99 != %v\n", ps[2])
312 }
313 }
314
315 func testUniformSampleStatistics(t *testing.T, s Sample) {
316 if count := s.Count(); 10000 != count {
317 t.Errorf("s.Count(): 10000 != %v\n", count)
318 }
319 if min := s.Min(); 37 != min {
320 t.Errorf("s.Min(): 37 != %v\n", min)
321 }
322 if max := s.Max(); 9989 != max {
323 t.Errorf("s.Max(): 9989 != %v\n", max)
324 }
325 if mean := s.Mean(); float64NotEqual(4748.14, mean) {
326 t.Errorf("s.Mean(): 4748.14 != %v\n", mean)
327 }
328 if stdDev := s.StdDev(); float64NotEqual(2826.684117548333, stdDev) {
329 t.Errorf("s.StdDev(): 2826.684117548333 != %v\n", stdDev)
330 }
331 ps := s.Percentiles([]float64{0.5, 0.75, 0.99})
332 if float64NotEqual(4599, ps[0]) {
333 t.Errorf("median: 4599 != %v\n", ps[0])
334 }
335 if float64NotEqual(7380.5, ps[1]) {
336 t.Errorf("75th percentile: 7380.5 != %v\n", ps[1])
337 }
338 if float64NotEqual(9986.429999999998, ps[2]) {
339 t.Errorf("99th percentile: 9986.429999999998 != %v\n", ps[2])
340 }
341 }
342
343 // TestUniformSampleConcurrentUpdateCount would expose data race problems with
344 // concurrent Update and Count calls on Sample when test is called with -race
345 // argument
346 func TestUniformSampleConcurrentUpdateCount(t *testing.T) {
347 if testing.Short() {
348 t.Skip("skipping in short mode")
349 }
350 s := NewUniformSample(100)
351 for i := 0; i < 100; i++ {
352 s.Update(int64(i))
353 }
354 quit := make(chan struct{})
355 go func() {
356 t := time.NewTicker(10 * time.Millisecond)
357 for {
358 select {
359 case <-t.C:
360 s.Update(rand.Int63())
361 case <-quit:
362 t.Stop()
363 return
364 }
365 }
366 }()
367 for i := 0; i < 1000; i++ {
368 s.Count()
369 time.Sleep(5 * time.Millisecond)
370 }
371 quit <- struct{}{}
372 }