From 7c5bbc37bddafa4949161febd743b9cfa8d62f5e Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sat, 20 Mar 2021 09:34:25 +0200 Subject: [PATCH] Add BenchmarkSetGoroutines --- bench_test.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/bench_test.go b/bench_test.go index 5e532ea..708da5d 100644 --- a/bench_test.go +++ b/bench_test.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "strings" + "sync" "testing" "time" @@ -27,20 +28,44 @@ func benchmarkRedisClient(ctx context.Context, poolSize int) *redis.Client { func BenchmarkRedisPing(b *testing.B) { ctx := context.Background() - client := benchmarkRedisClient(ctx, 10) - defer client.Close() + rdb := benchmarkRedisClient(ctx, 10) + defer rdb.Close() b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { - if err := client.Ping(ctx).Err(); err != nil { + if err := rdb.Ping(ctx).Err(); err != nil { b.Fatal(err) } } }) } +func BenchmarkSetGoroutines(b *testing.B) { + ctx := context.Background() + rdb := benchmarkRedisClient(ctx, 10) + defer rdb.Close() + + for i := 0; i < b.N; i++ { + var wg sync.WaitGroup + + for i := 0; i < 1000; i++ { + wg.Add(1) + go func() { + defer wg.Done() + + err := rdb.Set(ctx, "hello", "world", 0).Err() + if err != nil { + panic(err) + } + }() + } + + wg.Wait() + } +} + func BenchmarkRedisGetNil(b *testing.B) { ctx := context.Background() client := benchmarkRedisClient(ctx, 10)