Skip to content

Commit 7c5bbc3

Browse files
committed
Add BenchmarkSetGoroutines
1 parent a3cd07d commit 7c5bbc3

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

bench_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"strings"
8+
"sync"
89
"testing"
910
"time"
1011

@@ -27,20 +28,44 @@ func benchmarkRedisClient(ctx context.Context, poolSize int) *redis.Client {
2728

2829
func BenchmarkRedisPing(b *testing.B) {
2930
ctx := context.Background()
30-
client := benchmarkRedisClient(ctx, 10)
31-
defer client.Close()
31+
rdb := benchmarkRedisClient(ctx, 10)
32+
defer rdb.Close()
3233

3334
b.ResetTimer()
3435

3536
b.RunParallel(func(pb *testing.PB) {
3637
for pb.Next() {
37-
if err := client.Ping(ctx).Err(); err != nil {
38+
if err := rdb.Ping(ctx).Err(); err != nil {
3839
b.Fatal(err)
3940
}
4041
}
4142
})
4243
}
4344

45+
func BenchmarkSetGoroutines(b *testing.B) {
46+
ctx := context.Background()
47+
rdb := benchmarkRedisClient(ctx, 10)
48+
defer rdb.Close()
49+
50+
for i := 0; i < b.N; i++ {
51+
var wg sync.WaitGroup
52+
53+
for i := 0; i < 1000; i++ {
54+
wg.Add(1)
55+
go func() {
56+
defer wg.Done()
57+
58+
err := rdb.Set(ctx, "hello", "world", 0).Err()
59+
if err != nil {
60+
panic(err)
61+
}
62+
}()
63+
}
64+
65+
wg.Wait()
66+
}
67+
}
68+
4469
func BenchmarkRedisGetNil(b *testing.B) {
4570
ctx := context.Background()
4671
client := benchmarkRedisClient(ctx, 10)

0 commit comments

Comments
 (0)