forked from codesenberg/bombardier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimiter_test.go
159 lines (153 loc) · 3.34 KB
/
limiter_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
157
158
159
package main
import (
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestNoopLimiter(t *testing.T) {
var lim limiter = &nooplimiter{}
done := make(chan struct{})
counter := uint64(0)
var wg sync.WaitGroup
wg.Add(int(defaultNumberOfConns))
for i := uint64(0); i < defaultNumberOfConns; i++ {
go func() {
defer wg.Done()
for {
res := lim.pace(done)
if res != cont {
t.Error("nooplimiter should always return cont")
}
atomic.AddUint64(&counter, 1)
select {
case <-done:
return
default:
}
}
}()
}
time.Sleep(100 * time.Millisecond)
close(done)
wg.Wait()
if counter == 0 {
t.Error("no events happened")
}
}
func BenchmarkNoopLimiter(bm *testing.B) {
var lim limiter = &nooplimiter{}
done := make(chan struct{})
bm.SetParallelism(int(defaultNumberOfConns) / runtime.NumCPU())
bm.ResetTimer()
bm.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lim.pace(done)
}
})
}
func TestBucketLimiterLowRates(t *testing.T) {
expectations := []struct {
rate uint64
duration time.Duration
}{
{1, 1 * time.Second},
{10, 1 * time.Second},
{15, 1 * time.Second},
{50, 1 * time.Second},
{100, 1 * time.Second},
{150, 1 * time.Second},
{500, 1 * time.Second},
{1000, 1 * time.Second},
{1500, 1 * time.Second},
{5000, 1 * time.Second},
}
var lwg sync.WaitGroup
lwg.Add(len(expectations))
for i := range expectations {
exp := expectations[i]
go func() {
defer lwg.Done()
lim := newBucketLimiter(exp.rate)
done := make(chan struct{})
counter := uint64(0)
waitChan := make(chan struct{})
go func() {
defer func() {
waitChan <- struct{}{}
}()
for lim.pace(done) == cont {
counter++
}
}()
time.Sleep(exp.duration)
close(done)
select {
case <-waitChan:
break
case <-time.After(exp.duration + 100*time.Millisecond):
t.Error("failed to complete: ", exp)
return
}
expcounter := float64(exp.rate) * exp.duration.Seconds()
if float64(counter) < (expcounter*0.9) ||
float64(counter) > (expcounter*1.1+5) {
t.Error(expcounter, counter)
}
}()
}
lwg.Wait()
}
func TestBucketLimiterHighRates(t *testing.T) {
expectations := []struct {
rate uint64
duration time.Duration
}{
{100000, 100 * time.Millisecond},
{150000, 100 * time.Millisecond},
{200000, 100 * time.Millisecond},
{500000, 100 * time.Millisecond},
{1000000, 100 * time.Millisecond},
}
for i := range expectations {
exp := expectations[i]
lim := newBucketLimiter(exp.rate)
counter := uint64(0)
done := make(chan struct{})
waitChan := make(chan struct{})
go func() {
defer func() {
waitChan <- struct{}{}
}()
for lim.pace(done) == cont {
counter++
}
}()
time.Sleep(exp.duration)
close(done)
select {
case <-waitChan:
break
case <-time.After(exp.duration + 10*time.Millisecond):
t.Error("failed to complete: ", exp)
return
}
expcounter := float64(exp.rate) * exp.duration.Seconds()
if float64(counter) < (expcounter*0.9) ||
float64(counter) > (expcounter*1.1) {
t.Error(expcounter, counter)
}
}
}
func BenchmarkBucketLimiter(bm *testing.B) {
lim := newBucketLimiter(maxRps)
done := make(chan struct{})
bm.SetParallelism(int(defaultNumberOfConns) / runtime.NumCPU())
bm.ResetTimer()
bm.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lim.pace(done)
}
})
}