-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathratelimiter_test.go
141 lines (122 loc) · 2.89 KB
/
ratelimiter_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
package utils
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"
)
func TestRateLimiter(t *testing.T) {
t.Parallel()
ctx := context.Background()
t.Run("wrong args", func(t *testing.T) {
t.Parallel()
_, err := NewRateLimiter(ctx, RateLimiterArgs{
NPerSec: 0,
Max: 100,
})
require.Error(t, err)
_, err = NewRateLimiter(ctx, RateLimiterArgs{
NPerSec: 10,
Max: 9,
})
require.Error(t, err)
})
t.Run("stop", func(t *testing.T) {
t.Parallel()
RateLimiter2, err := NewRateLimiter(ctx, RateLimiterArgs{
NPerSec: 10,
Max: 100,
})
require.NoError(t, err)
RateLimiter2.Close()
ctx2, cancel := context.WithCancel(ctx)
_, err = NewRateLimiter(ctx2, RateLimiterArgs{
NPerSec: 10,
Max: 100,
})
require.NoError(t, err)
cancel()
})
t.Run("allow", func(t *testing.T) {
t.Parallel()
ratelimiter, err := NewRateLimiter(ctx, RateLimiterArgs{
NPerSec: 10,
Max: 100,
})
require.NoError(t, err)
defer ratelimiter.Close()
for i := 0; i < 20; i++ {
allowed := ratelimiter.Allow()
if i < 10 {
require.True(t, allowed, i)
} else if i >= 10 {
require.False(t, allowed, i)
}
}
time.Sleep(1050 * time.Millisecond)
require.GreaterOrEqual(t, ratelimiter.Len(), 10)
for i := 0; i < 5; i++ {
require.True(t, ratelimiter.Allow(), i)
}
require.GreaterOrEqual(t, ratelimiter.Len(), 5)
require.False(t, ratelimiter.AllowN(20))
require.GreaterOrEqual(t, ratelimiter.Len(), 5)
require.True(t, ratelimiter.AllowN(5))
for i := 0; i < 100; i++ {
require.False(t, ratelimiter.Allow(), i)
}
})
}
/*
goos: linux
goarch: amd64
pkg: github.com/Laisky/go-utils
cpu: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
BenchmarkRateLimiter/RateLimiter-8 684580170 1.553 ns/op 0 B/op 0 allocs/op
BenchmarkRateLimiter/rate.Limiter-8 4633182 309.2 ns/op 0 B/op 0 allocs/op
*/
func BenchmarkRateLimiter(b *testing.B) {
ctx := context.Background()
b.Run("RateLimiter", func(b *testing.B) {
RateLimiter, err := NewRateLimiter(ctx, RateLimiterArgs{
NPerSec: 10,
Max: 100,
})
require.NoError(b, err)
defer RateLimiter.Close()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
RateLimiter.Allow()
}
})
})
b.Run("golang.org/x/time/rate", func(b *testing.B) {
limiter := rate.NewLimiter(rate.Limit(10), 100)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
limiter.Allow()
}
})
})
}
func ExampleRateLimiter() {
ctx := context.Background()
RateLimiter, err := NewRateLimiter(ctx, RateLimiterArgs{
NPerSec: 10,
Max: 100,
})
if err != nil {
panic("new RateLimiter")
}
defer RateLimiter.Close()
inChan := make(chan int)
for msg := range inChan {
if !RateLimiter.Allow() {
continue
}
// do something with msg
fmt.Println(msg)
}
}