Add BenchmarkSetGoroutines

This commit is contained in:
Vladimir Mihailenco 2021-03-20 09:34:25 +02:00
parent a3cd07dab0
commit 7c5bbc37bd
1 changed files with 28 additions and 3 deletions

View File

@ -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)