-
Notifications
You must be signed in to change notification settings - Fork 0
/
rolling_counter_test.go
156 lines (149 loc) · 3.04 KB
/
rolling_counter_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
156
package rolling
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRollingCounterAdd(t *testing.T) {
size := 3
bucketDuration := time.Second
opts := RollingCounterOpts{
Size: size,
BucketDuration: bucketDuration,
}
r := NewRollingCounter(opts)
listBuckets := func() [][]float64 {
buckets := make([][]float64, 0)
r.Reduce(func(i Iterator) float64 {
for i.Next() {
bucket := i.Bucket()
buckets = append(buckets, bucket.Points)
}
return 0.0
})
return buckets
}
assert.Equal(t, [][]float64{{}, {}, {}}, listBuckets())
r.Add(1)
assert.Equal(t, [][]float64{{}, {}, {1}}, listBuckets())
time.Sleep(time.Second)
r.Add(2)
r.Add(3)
assert.Equal(t, [][]float64{{}, {1}, {5}}, listBuckets())
time.Sleep(time.Second)
r.Add(4)
r.Add(5)
r.Add(6)
assert.Equal(t, [][]float64{{1}, {5}, {15}}, listBuckets())
time.Sleep(time.Second)
r.Add(7)
assert.Equal(t, [][]float64{{5}, {15}, {7}}, listBuckets())
}
func TestRollingCounterReduce(t *testing.T) {
size := 3
bucketDuration := time.Second
opts := RollingCounterOpts{
Size: size,
BucketDuration: bucketDuration,
}
r := NewRollingCounter(opts)
for x := 0; x < size; x = x + 1 {
for i := 0; i <= x; i++ {
r.Add(1)
}
if x < size-1 {
time.Sleep(bucketDuration)
}
}
var result = r.Reduce(func(iterator Iterator) float64 {
var result float64
for iterator.Next() {
bucket := iterator.Bucket()
result += bucket.Points[0]
}
return result
})
if result != 6.0 {
t.Fatalf("Validate sum of points. result: %f", result)
}
}
func TestRollingCounterDataRace(t *testing.T) {
size := 3
bucketDuration := time.Millisecond * 10
opts := RollingCounterOpts{
Size: size,
BucketDuration: bucketDuration,
}
r := NewRollingCounter(opts)
var stop = make(chan bool)
go func() {
for {
select {
case <-stop:
return
default:
r.Add(1)
time.Sleep(time.Millisecond * 5)
}
}
}()
go func() {
for {
select {
case <-stop:
return
default:
_ = r.Reduce(func(i Iterator) float64 {
for i.Next() {
bucket := i.Bucket()
for range bucket.Points {
continue
}
}
return 0
})
}
}
}()
time.Sleep(time.Second * 3)
close(stop)
}
func BenchmarkRollingCounterIncr(b *testing.B) {
size := 3
bucketDuration := time.Millisecond * 100
opts := RollingCounterOpts{
Size: size,
BucketDuration: bucketDuration,
}
r := NewRollingCounter(opts)
b.ResetTimer()
for i := 0; i <= b.N; i++ {
r.Add(1)
}
}
func BenchmarkRollingCounterReduce(b *testing.B) {
size := 3
bucketDuration := time.Second
opts := RollingCounterOpts{
Size: size,
BucketDuration: bucketDuration,
}
r := NewRollingCounter(opts)
for i := 0; i <= 10; i++ {
r.Add(1)
time.Sleep(time.Millisecond * 500)
}
b.ResetTimer()
for i := 0; i <= b.N; i++ {
var _ = r.Reduce(func(i Iterator) float64 {
var result float64
for i.Next() {
bucket := i.Bucket()
if len(bucket.Points) != 0 {
result += bucket.Points[0]
}
}
return result
})
}
}