Skip to content

Commit

Permalink
Add ctx as first arg
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed May 19, 2020
1 parent 64bb0b7 commit f559312
Show file tree
Hide file tree
Showing 36 changed files with 3,159 additions and 2,929 deletions.
86 changes: 47 additions & 39 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,46 @@ import (
"github.com/go-redis/redis/v7"
)

func benchmarkRedisClient(poolSize int) *redis.Client {
func benchmarkRedisClient(ctx context.Context, poolSize int) *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: ":6379",
DialTimeout: time.Second,
ReadTimeout: time.Second,
WriteTimeout: time.Second,
PoolSize: poolSize,
})
if err := client.FlushDB().Err(); err != nil {
if err := client.FlushDB(ctx).Err(); err != nil {
panic(err)
}
return client
}

func BenchmarkRedisPing(b *testing.B) {
client := benchmarkRedisClient(10)
ctx := context.Background()
client := benchmarkRedisClient(ctx, 10)
defer client.Close()

b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := client.Ping().Err(); err != nil {
if err := client.Ping(ctx).Err(); err != nil {
b.Fatal(err)
}
}
})
}

func BenchmarkRedisGetNil(b *testing.B) {
client := benchmarkRedisClient(10)
ctx := context.Background()
client := benchmarkRedisClient(ctx, 10)
defer client.Close()

b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := client.Get("key").Err(); err != redis.Nil {
if err := client.Get(ctx, "key").Err(); err != redis.Nil {
b.Fatal(err)
}
}
Expand Down Expand Up @@ -80,7 +82,8 @@ func BenchmarkRedisSetString(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.String(), func(b *testing.B) {
client := benchmarkRedisClient(bm.poolSize)
ctx := context.Background()
client := benchmarkRedisClient(ctx, bm.poolSize)
defer client.Close()

value := strings.Repeat("1", bm.valueSize)
Expand All @@ -89,7 +92,7 @@ func BenchmarkRedisSetString(b *testing.B) {

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := client.Set("key", value, 0).Err()
err := client.Set(ctx, "key", value, 0).Err()
if err != nil {
b.Fatal(err)
}
Expand All @@ -100,7 +103,8 @@ func BenchmarkRedisSetString(b *testing.B) {
}

func BenchmarkRedisSetGetBytes(b *testing.B) {
client := benchmarkRedisClient(10)
ctx := context.Background()
client := benchmarkRedisClient(ctx, 10)
defer client.Close()

value := bytes.Repeat([]byte{'1'}, 10000)
Expand All @@ -109,11 +113,11 @@ func BenchmarkRedisSetGetBytes(b *testing.B) {

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := client.Set("key", value, 0).Err(); err != nil {
if err := client.Set(ctx, "key", value, 0).Err(); err != nil {
b.Fatal(err)
}

got, err := client.Get("key").Bytes()
got, err := client.Get(ctx, "key").Bytes()
if err != nil {
b.Fatal(err)
}
Expand All @@ -125,53 +129,56 @@ func BenchmarkRedisSetGetBytes(b *testing.B) {
}

func BenchmarkRedisMGet(b *testing.B) {
client := benchmarkRedisClient(10)
ctx := context.Background()
client := benchmarkRedisClient(ctx, 10)
defer client.Close()

if err := client.MSet("key1", "hello1", "key2", "hello2").Err(); err != nil {
if err := client.MSet(ctx, "key1", "hello1", "key2", "hello2").Err(); err != nil {
b.Fatal(err)
}

b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := client.MGet("key1", "key2").Err(); err != nil {
if err := client.MGet(ctx, "key1", "key2").Err(); err != nil {
b.Fatal(err)
}
}
})
}

func BenchmarkSetExpire(b *testing.B) {
client := benchmarkRedisClient(10)
ctx := context.Background()
client := benchmarkRedisClient(ctx, 10)
defer client.Close()

b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := client.Set("key", "hello", 0).Err(); err != nil {
if err := client.Set(ctx, "key", "hello", 0).Err(); err != nil {
b.Fatal(err)
}
if err := client.Expire("key", time.Second).Err(); err != nil {
if err := client.Expire(ctx, "key", time.Second).Err(); err != nil {
b.Fatal(err)
}
}
})
}

func BenchmarkPipeline(b *testing.B) {
client := benchmarkRedisClient(10)
ctx := context.Background()
client := benchmarkRedisClient(ctx, 10)
defer client.Close()

b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := client.Pipelined(func(pipe redis.Pipeliner) error {
pipe.Set("key", "hello", 0)
pipe.Expire("key", time.Second)
_, err := client.Pipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Set(ctx, "key", "hello", 0)
pipe.Expire(ctx, "key", time.Second)
return nil
})
if err != nil {
Expand All @@ -182,14 +189,15 @@ func BenchmarkPipeline(b *testing.B) {
}

func BenchmarkZAdd(b *testing.B) {
client := benchmarkRedisClient(10)
ctx := context.Background()
client := benchmarkRedisClient(ctx, 10)
defer client.Close()

b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := client.ZAdd("key", &redis.Z{
err := client.ZAdd(ctx, "key", &redis.Z{
Score: float64(1),
Member: "hello",
}).Err()
Expand All @@ -203,10 +211,9 @@ func BenchmarkZAdd(b *testing.B) {
var clientSink *redis.Client

func BenchmarkWithContext(b *testing.B) {
rdb := benchmarkRedisClient(10)
defer rdb.Close()

ctx := context.Background()
rdb := benchmarkRedisClient(ctx, 10)
defer rdb.Close()

b.ResetTimer()
b.ReportAllocs()
Expand All @@ -219,11 +226,10 @@ func BenchmarkWithContext(b *testing.B) {
var ringSink *redis.Ring

func BenchmarkRingWithContext(b *testing.B) {
ctx := context.Background()
rdb := redis.NewRing(&redis.RingOptions{})
defer rdb.Close()

ctx := context.Background()

b.ResetTimer()
b.ReportAllocs()

Expand All @@ -248,20 +254,21 @@ func BenchmarkClusterPing(b *testing.B) {
b.Skip("skipping in short mode")
}

ctx := context.Background()
cluster := newClusterScenario()
if err := startCluster(cluster); err != nil {
if err := startCluster(ctx, cluster); err != nil {
b.Fatal(err)
}
defer stopCluster(cluster)

client := cluster.newClusterClient(redisClusterOptions())
client := cluster.newClusterClient(ctx, redisClusterOptions())
defer client.Close()

b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := client.Ping().Err()
err := client.Ping(ctx).Err()
if err != nil {
b.Fatal(err)
}
Expand All @@ -274,13 +281,14 @@ func BenchmarkClusterSetString(b *testing.B) {
b.Skip("skipping in short mode")
}

ctx := context.Background()
cluster := newClusterScenario()
if err := startCluster(cluster); err != nil {
if err := startCluster(ctx, cluster); err != nil {
b.Fatal(err)
}
defer stopCluster(cluster)

client := cluster.newClusterClient(redisClusterOptions())
client := cluster.newClusterClient(ctx, redisClusterOptions())
defer client.Close()

value := string(bytes.Repeat([]byte{'1'}, 10000))
Expand All @@ -289,7 +297,7 @@ func BenchmarkClusterSetString(b *testing.B) {

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := client.Set("key", value, 0).Err()
err := client.Set(ctx, "key", value, 0).Err()
if err != nil {
b.Fatal(err)
}
Expand All @@ -302,19 +310,20 @@ func BenchmarkClusterReloadState(b *testing.B) {
b.Skip("skipping in short mode")
}

ctx := context.Background()
cluster := newClusterScenario()
if err := startCluster(cluster); err != nil {
if err := startCluster(ctx, cluster); err != nil {
b.Fatal(err)
}
defer stopCluster(cluster)

client := cluster.newClusterClient(redisClusterOptions())
client := cluster.newClusterClient(ctx, redisClusterOptions())
defer client.Close()

b.ResetTimer()

for i := 0; i < b.N; i++ {
err := client.ReloadState()
err := client.ReloadState(ctx)
if err != nil {
b.Fatal(err)
}
Expand All @@ -324,11 +333,10 @@ func BenchmarkClusterReloadState(b *testing.B) {
var clusterSink *redis.ClusterClient

func BenchmarkClusterWithContext(b *testing.B) {
ctx := context.Background()
rdb := redis.NewClusterClient(&redis.ClusterOptions{})
defer rdb.Close()

ctx := context.Background()

b.ResetTimer()
b.ReportAllocs()

Expand Down
Loading

0 comments on commit f559312

Please sign in to comment.