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" "context"
"fmt" "fmt"
"strings" "strings"
"sync"
"testing" "testing"
"time" "time"
@ -27,20 +28,44 @@ func benchmarkRedisClient(ctx context.Context, poolSize int) *redis.Client {
func BenchmarkRedisPing(b *testing.B) { func BenchmarkRedisPing(b *testing.B) {
ctx := context.Background() ctx := context.Background()
client := benchmarkRedisClient(ctx, 10) rdb := benchmarkRedisClient(ctx, 10)
defer client.Close() defer rdb.Close()
b.ResetTimer() b.ResetTimer()
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
if err := client.Ping(ctx).Err(); err != nil { if err := rdb.Ping(ctx).Err(); err != nil {
b.Fatal(err) 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) { func BenchmarkRedisGetNil(b *testing.B) {
ctx := context.Background() ctx := context.Background()
client := benchmarkRedisClient(ctx, 10) client := benchmarkRedisClient(ctx, 10)