-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
62 lines (50 loc) · 1004 Bytes
/
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
61
62
package locker
import (
"context"
"testing"
"time"
"github.com/go-redis/redis/v8"
)
func Benchmark(b *testing.B) {
client := redis.NewClient(&redis.Options{})
defer client.Close()
ctx := context.Background()
key := "key"
err := client.Del(ctx, key).Err()
if err != nil {
b.Fatal(err)
}
locker := NewLocker(client)
b.Run("Locker.Lock", func(b *testing.B) {
for i := 0; i < b.N; i++ {
locker.Lock(ctx, key, time.Millisecond)
}
})
err = client.Del(ctx, key).Err()
if err != nil {
b.Fatal(err)
}
locker = NewLocker(client)
lr, err := locker.Lock(ctx, key, time.Second)
if err != nil {
b.Fatal(err)
}
b.Run("Lock.Lock", func(b *testing.B) {
for i := 0; i < b.N; i++ {
lr.Lock.Lock(ctx, time.Second)
}
})
err = client.Del(ctx, key).Err()
if err != nil {
b.Fatal(err)
}
lr, err = locker.Lock(ctx, key, time.Second)
if err != nil {
b.Fatal(err)
}
b.Run("Lock.Unlock", func(b *testing.B) {
for i := 0; i < b.N; i++ {
lr.Unlock(ctx)
}
})
}