From fee18a3d42dfe2fcabefad01a534edfb9b5d5423 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Fri, 10 Aug 2018 13:55:57 +0300 Subject: [PATCH] Use sub-benchmarks --- bench_test.go | 90 ++++++++++---------------- internal/pool/bench_test.go | 125 ++++++++++++++++++++---------------- 2 files changed, 105 insertions(+), 110 deletions(-) diff --git a/bench_test.go b/bench_test.go index b7c4081e..dc2299b7 100644 --- a/bench_test.go +++ b/bench_test.go @@ -2,6 +2,8 @@ package redis_test import ( "bytes" + "fmt" + "strings" "testing" "time" @@ -37,23 +39,6 @@ func BenchmarkRedisPing(b *testing.B) { }) } -func BenchmarkRedisSetString(b *testing.B) { - client := benchmarkRedisClient(10) - defer client.Close() - - value := string(bytes.Repeat([]byte{'1'}, 10000)) - - b.ResetTimer() - - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - if err := client.Set("key", value, 0).Err(); err != nil { - b.Fatal(err) - } - } - }) -} - func BenchmarkRedisGetNil(b *testing.B) { client := benchmarkRedisClient(10) defer client.Close() @@ -69,53 +54,46 @@ func BenchmarkRedisGetNil(b *testing.B) { }) } -func benchmarkSetRedis(b *testing.B, poolSize, payloadSize int) { - client := benchmarkRedisClient(poolSize) - defer client.Close() - - value := string(bytes.Repeat([]byte{'1'}, payloadSize)) - - b.ResetTimer() - - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - if err := client.Set("key", value, 0).Err(); err != nil { - b.Fatal(err) - } - } - }) +type setStringBenchmark struct { + poolSize int + valueSize int } -func BenchmarkSetRedis10Conns64Bytes(b *testing.B) { - benchmarkSetRedis(b, 10, 64) +func (bm setStringBenchmark) String() string { + return fmt.Sprintf("pool=%d value=%d", bm.poolSize, bm.valueSize) } -func BenchmarkSetRedis100Conns64Bytes(b *testing.B) { - benchmarkSetRedis(b, 100, 64) -} +func BenchmarkRedisSetString(b *testing.B) { + benchmarks := []setStringBenchmark{ + {10, 64}, + {10, 1024}, + {10, 64 * 1024}, + {10, 1024 * 1024}, -func BenchmarkSetRedis10Conns1KB(b *testing.B) { - benchmarkSetRedis(b, 10, 1024) -} + {100, 64}, + {100, 1024}, + {100, 64 * 1024}, + {100, 1024 * 1024}, + } + for _, bm := range benchmarks { + b.Run(bm.String(), func(b *testing.B) { + client := benchmarkRedisClient(bm.poolSize) + defer client.Close() -func BenchmarkSetRedis100Conns1KB(b *testing.B) { - benchmarkSetRedis(b, 100, 1024) -} + value := strings.Repeat("1", bm.valueSize) -func BenchmarkSetRedis10Conns10KB(b *testing.B) { - benchmarkSetRedis(b, 10, 10*1024) -} + b.ResetTimer() -func BenchmarkSetRedis100Conns10KB(b *testing.B) { - benchmarkSetRedis(b, 100, 10*1024) -} - -func BenchmarkSetRedis10Conns1MB(b *testing.B) { - benchmarkSetRedis(b, 10, 1024*1024) -} - -func BenchmarkSetRedis100Conns1MB(b *testing.B) { - benchmarkSetRedis(b, 100, 1024*1024) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + err := client.Set("key", value, 0).Err() + if err != nil { + b.Fatal(err) + } + } + }) + }) + } } func BenchmarkRedisSetGetBytes(b *testing.B) { diff --git a/internal/pool/bench_test.go b/internal/pool/bench_test.go index e80c9c00..54fbf913 100644 --- a/internal/pool/bench_test.go +++ b/internal/pool/bench_test.go @@ -1,76 +1,93 @@ package pool_test import ( + "fmt" "testing" "time" "github.com/go-redis/redis/internal/pool" ) -func benchmarkPoolGetPut(b *testing.B, poolSize int) { - connPool := pool.NewConnPool(&pool.Options{ - Dialer: dummyDialer, - PoolSize: poolSize, - PoolTimeout: time.Second, - IdleTimeout: time.Hour, - IdleCheckFrequency: time.Hour, - }) - - b.ResetTimer() - - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - cn, err := connPool.Get() - if err != nil { - b.Fatal(err) - } - connPool.Put(cn) - } - }) +type poolGetPutBenchmark struct { + poolSize int } -func BenchmarkPoolGetPut10Conns(b *testing.B) { - benchmarkPoolGetPut(b, 10) +func (bm poolGetPutBenchmark) String() string { + return fmt.Sprintf("pool=%d", bm.poolSize) } -func BenchmarkPoolGetPut100Conns(b *testing.B) { - benchmarkPoolGetPut(b, 100) +func BenchmarkPoolGetPut(b *testing.B) { + benchmarks := []poolGetPutBenchmark{ + {1}, + {2}, + {8}, + {32}, + {64}, + {128}, + } + for _, bm := range benchmarks { + b.Run(bm.String(), func(b *testing.B) { + connPool := pool.NewConnPool(&pool.Options{ + Dialer: dummyDialer, + PoolSize: bm.poolSize, + PoolTimeout: time.Second, + IdleTimeout: time.Hour, + IdleCheckFrequency: time.Hour, + }) + + b.ResetTimer() + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + cn, err := connPool.Get() + if err != nil { + b.Fatal(err) + } + connPool.Put(cn) + } + }) + }) + } } -func BenchmarkPoolGetPut1000Conns(b *testing.B) { - benchmarkPoolGetPut(b, 1000) +type poolGetRemoveBenchmark struct { + poolSize int } -func benchmarkPoolGetRemove(b *testing.B, poolSize int) { - connPool := pool.NewConnPool(&pool.Options{ - Dialer: dummyDialer, - PoolSize: poolSize, - PoolTimeout: time.Second, - IdleTimeout: time.Hour, - IdleCheckFrequency: time.Hour, - }) - - b.ResetTimer() - - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - cn, err := connPool.Get() - if err != nil { - b.Fatal(err) - } - connPool.Remove(cn) - } - }) +func (bm poolGetRemoveBenchmark) String() string { + return fmt.Sprintf("pool=%d", bm.poolSize) } -func BenchmarkPoolGetRemove10Conns(b *testing.B) { - benchmarkPoolGetRemove(b, 10) -} +func BenchmarkPoolGetRemove(b *testing.B) { + benchmarks := []poolGetRemoveBenchmark{ + {1}, + {2}, + {8}, + {32}, + {64}, + {128}, + } + for _, bm := range benchmarks { + b.Run(bm.String(), func(b *testing.B) { + connPool := pool.NewConnPool(&pool.Options{ + Dialer: dummyDialer, + PoolSize: bm.poolSize, + PoolTimeout: time.Second, + IdleTimeout: time.Hour, + IdleCheckFrequency: time.Hour, + }) -func BenchmarkPoolGetRemove100Conns(b *testing.B) { - benchmarkPoolGetRemove(b, 100) -} + b.ResetTimer() -func BenchmarkPoolGetRemove1000Conns(b *testing.B) { - benchmarkPoolGetRemove(b, 1000) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + cn, err := connPool.Get() + if err != nil { + b.Fatal(err) + } + connPool.Remove(cn) + } + }) + }) + } }