Use sub-benchmarks

This commit is contained in:
Vladimir Mihailenco 2018-08-10 13:55:57 +03:00
parent e3b56f7641
commit fee18a3d42
2 changed files with 105 additions and 110 deletions

View File

@ -2,6 +2,8 @@ package redis_test
import ( import (
"bytes" "bytes"
"fmt"
"strings"
"testing" "testing"
"time" "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) { func BenchmarkRedisGetNil(b *testing.B) {
client := benchmarkRedisClient(10) client := benchmarkRedisClient(10)
defer client.Close() defer client.Close()
@ -69,53 +54,46 @@ func BenchmarkRedisGetNil(b *testing.B) {
}) })
} }
func benchmarkSetRedis(b *testing.B, poolSize, payloadSize int) { type setStringBenchmark struct {
client := benchmarkRedisClient(poolSize) poolSize int
valueSize int
}
func (bm setStringBenchmark) String() string {
return fmt.Sprintf("pool=%d value=%d", bm.poolSize, bm.valueSize)
}
func BenchmarkRedisSetString(b *testing.B) {
benchmarks := []setStringBenchmark{
{10, 64},
{10, 1024},
{10, 64 * 1024},
{10, 1024 * 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() defer client.Close()
value := string(bytes.Repeat([]byte{'1'}, payloadSize)) value := strings.Repeat("1", bm.valueSize)
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.Set("key", value, 0).Err(); err != nil { err := client.Set("key", value, 0).Err()
if err != nil {
b.Fatal(err) b.Fatal(err)
} }
} }
}) })
})
} }
func BenchmarkSetRedis10Conns64Bytes(b *testing.B) {
benchmarkSetRedis(b, 10, 64)
}
func BenchmarkSetRedis100Conns64Bytes(b *testing.B) {
benchmarkSetRedis(b, 100, 64)
}
func BenchmarkSetRedis10Conns1KB(b *testing.B) {
benchmarkSetRedis(b, 10, 1024)
}
func BenchmarkSetRedis100Conns1KB(b *testing.B) {
benchmarkSetRedis(b, 100, 1024)
}
func BenchmarkSetRedis10Conns10KB(b *testing.B) {
benchmarkSetRedis(b, 10, 10*1024)
}
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)
} }
func BenchmarkRedisSetGetBytes(b *testing.B) { func BenchmarkRedisSetGetBytes(b *testing.B) {

View File

@ -1,16 +1,35 @@
package pool_test package pool_test
import ( import (
"fmt"
"testing" "testing"
"time" "time"
"github.com/go-redis/redis/internal/pool" "github.com/go-redis/redis/internal/pool"
) )
func benchmarkPoolGetPut(b *testing.B, poolSize int) { type poolGetPutBenchmark struct {
poolSize int
}
func (bm poolGetPutBenchmark) String() string {
return fmt.Sprintf("pool=%d", bm.poolSize)
}
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{ connPool := pool.NewConnPool(&pool.Options{
Dialer: dummyDialer, Dialer: dummyDialer,
PoolSize: poolSize, PoolSize: bm.poolSize,
PoolTimeout: time.Second, PoolTimeout: time.Second,
IdleTimeout: time.Hour, IdleTimeout: time.Hour,
IdleCheckFrequency: time.Hour, IdleCheckFrequency: time.Hour,
@ -27,24 +46,32 @@ func benchmarkPoolGetPut(b *testing.B, poolSize int) {
connPool.Put(cn) connPool.Put(cn)
} }
}) })
})
}
} }
func BenchmarkPoolGetPut10Conns(b *testing.B) { type poolGetRemoveBenchmark struct {
benchmarkPoolGetPut(b, 10) poolSize int
} }
func BenchmarkPoolGetPut100Conns(b *testing.B) { func (bm poolGetRemoveBenchmark) String() string {
benchmarkPoolGetPut(b, 100) return fmt.Sprintf("pool=%d", bm.poolSize)
} }
func BenchmarkPoolGetPut1000Conns(b *testing.B) { func BenchmarkPoolGetRemove(b *testing.B) {
benchmarkPoolGetPut(b, 1000) benchmarks := []poolGetRemoveBenchmark{
{1},
{2},
{8},
{32},
{64},
{128},
} }
for _, bm := range benchmarks {
func benchmarkPoolGetRemove(b *testing.B, poolSize int) { b.Run(bm.String(), func(b *testing.B) {
connPool := pool.NewConnPool(&pool.Options{ connPool := pool.NewConnPool(&pool.Options{
Dialer: dummyDialer, Dialer: dummyDialer,
PoolSize: poolSize, PoolSize: bm.poolSize,
PoolTimeout: time.Second, PoolTimeout: time.Second,
IdleTimeout: time.Hour, IdleTimeout: time.Hour,
IdleCheckFrequency: time.Hour, IdleCheckFrequency: time.Hour,
@ -61,16 +88,6 @@ func benchmarkPoolGetRemove(b *testing.B, poolSize int) {
connPool.Remove(cn) connPool.Remove(cn)
} }
}) })
})
} }
func BenchmarkPoolGetRemove10Conns(b *testing.B) {
benchmarkPoolGetRemove(b, 10)
}
func BenchmarkPoolGetRemove100Conns(b *testing.B) {
benchmarkPoolGetRemove(b, 100)
}
func BenchmarkPoolGetRemove1000Conns(b *testing.B) {
benchmarkPoolGetRemove(b, 1000)
} }