-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
60 lines (53 loc) · 1.48 KB
/
benchmark_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
package ratelimiter
import (
"context"
"testing"
"time"
)
func runBenchmarks(b *testing.B, limiter *RateLimiter) {
ctx := context.Background()
tests := []struct {
name string
key string
durationPerToken time.Duration
burst int
}{
{"Key1_Duration10ms_Burst5", "BenchmarkRedisDriver_Reserve_1", 10 * time.Millisecond, 5},
{"Key2_Duration20ms_Burst10", "BenchmarkRedisDriver_Reserve_2", 20 * time.Millisecond, 10},
{"Key3_Duration50ms_Burst3", "BenchmarkRedisDriver_Reserve_3", 50 * time.Millisecond, 3},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
now := time.Now()
b.ResetTimer()
for i := 0; i < b.N; i++ {
reserveReq := &ReserveRequest{
Key: tt.key,
DurationPerToken: tt.durationPerToken,
Burst: tt.burst,
Tokens: 1,
MaxFutureReserve: 0,
}
ctx := WithNowFuncForTest(ctx, func() time.Time {
return now.Add(time.Duration(i) * tt.durationPerToken)
})
_, err := limiter.Reserve(ctx, reserveReq)
if err != nil {
b.Fatalf("failed to reserve: %v", err)
}
}
})
}
}
func BenchmarkDriverRedis_Reserve(b *testing.B) {
driver, err := InitRedisDriver(context.Background(), redisCli)
if err != nil {
b.Fatalf("failed to initialize Redis driver: %v", err)
}
limiter := New(driver)
runBenchmarks(b, limiter)
}
func BenchmarkDriverGORM_Reserve(b *testing.B) {
limiter := New(NewGormDriver(db))
runBenchmarks(b, limiter)
}