-
Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathtimer_test.go
155 lines (134 loc) · 2.83 KB
/
timer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package timer
import (
"math"
"math/rand"
"testing"
)
func TestMovAvg(t *testing.T) {
count := 100
ma := newMovAvg(count)
for i := 0; i < count; i++ {
avg := ma.update(1)
if got, exp := avg, 1.0; got != exp {
t.Fatalf("unexpected movavg: got %f, exp %f", got, exp)
}
}
c := float64(count)
for i := 0; i < count; i++ {
avg := ma.update(2)
f := float64(i + 1)
if got, exp := avg, ((c-f)+2.0*f)/c; math.Abs(got-exp) > 1e-8 {
t.Fatalf("unexpected movavg i: %d got %f, exp %f", i, got, exp)
}
}
}
func TestMovAvgAccuracy(t *testing.T) {
count := 10000
avg := func(data []float64) float64 {
sum := 0.0
for _, v := range data {
sum += v
}
return sum / float64(len(data))
}
r := rand.New(rand.NewSource(42))
data := make([]float64, count)
for i := 0; i < count; i++ {
data[i] = r.Float64()
}
expAvg := avg(data)
ma := newMovAvg(count)
gotAvg := 0.0
for _, v := range data {
gotAvg = ma.update(v)
}
if math.Abs(gotAvg-expAvg) > 1e-8 {
t.Errorf("unexpected average value: got %f exp %f", gotAvg, expAvg)
}
size := count / 10
ma = newMovAvg(size)
expAvg = avg(data[count-size:])
gotAvg = 0.0
for _, v := range data {
gotAvg = ma.update(v)
}
if math.Abs(gotAvg-expAvg) > 1e-8 {
t.Errorf("unexpected moving average value: got %f exp %f", gotAvg, expAvg)
}
}
func BenchmarkMovAvgUpdate(b *testing.B) {
ma := newMovAvg(1000)
for i := 0; i < b.N; i++ {
ma.update(float64(i))
}
}
type setter struct {
value int64
}
func (s *setter) Set(v int64) {
s.value = v
}
func TestSampling(t *testing.T) {
sr := 0.1
size := 10000
rand.New(rand.NewSource(0))
s := &setter{}
tmr := New(sr, size, s).(*timer)
for i := 0; i < size; i++ {
tmr.Start()
tmr.Stop()
}
count := sr * float64(size)
err := 0.1
if math.Abs(float64(tmr.avg.count)-count) > err*count {
t.Errorf("unexpected numbers of samples taken got: %d exp: %d", int(tmr.avg.count), int(count))
}
}
func BenchmarkTimerStartStopWorst(b *testing.B) {
// Use 100% sample rate worst case
sr := 1.0
size := 1000
s := &setter{}
tmr := New(sr, size, s).(*timer)
for i := 0; i < b.N; i++ {
tmr.Start()
tmr.Stop()
}
}
func BenchmarkTimerStartPauseResumeStopWorst(b *testing.B) {
// Use 100% sample rate worst case
sr := 1.0
size := 1000
s := &setter{}
tmr := New(sr, size, s).(*timer)
for i := 0; i < b.N; i++ {
tmr.Start()
tmr.Pause()
tmr.Resume()
tmr.Stop()
}
}
func BenchmarkTimerStartStopBest(b *testing.B) {
// Use 0% sample rate best case
sr := 0.0
size := 1000
s := &setter{}
tmr := New(sr, size, s).(*timer)
for i := 0; i < b.N; i++ {
tmr.Start()
tmr.Stop()
}
}
func BenchmarkTimerStartPauseResumeStopBest(b *testing.B) {
// Use 0% sample rate base case
sr := 0.0
size := 1000
s := &setter{}
tmr := New(sr, size, s).(*timer)
for i := 0; i < b.N; i++ {
tmr.Start()
tmr.Pause()
tmr.Resume()
tmr.Stop()
}
}