Merge pull request #835 from go-redis/fix/sub-benchmarks

Use sub-benchmarks
This commit is contained in:
Vladimir Mihailenco 2018-08-10 14:54:54 +03:00 committed by GitHub
commit a80633d319
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
defer client.Close() valueSize int
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)
}
}
})
} }
func BenchmarkSetRedis10Conns64Bytes(b *testing.B) { func (bm setStringBenchmark) String() string {
benchmarkSetRedis(b, 10, 64) return fmt.Sprintf("pool=%d value=%d", bm.poolSize, bm.valueSize)
} }
func BenchmarkSetRedis100Conns64Bytes(b *testing.B) { func BenchmarkRedisSetString(b *testing.B) {
benchmarkSetRedis(b, 100, 64) benchmarks := []setStringBenchmark{
} {10, 64},
{10, 1024},
{10, 64 * 1024},
{10, 1024 * 1024},
func BenchmarkSetRedis10Conns1KB(b *testing.B) { {100, 64},
benchmarkSetRedis(b, 10, 1024) {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) { value := strings.Repeat("1", bm.valueSize)
benchmarkSetRedis(b, 100, 1024)
}
func BenchmarkSetRedis10Conns10KB(b *testing.B) { b.ResetTimer()
benchmarkSetRedis(b, 10, 10*1024)
}
func BenchmarkSetRedis100Conns10KB(b *testing.B) { b.RunParallel(func(pb *testing.PB) {
benchmarkSetRedis(b, 100, 10*1024) for pb.Next() {
} err := client.Set("key", value, 0).Err()
if err != nil {
func BenchmarkSetRedis10Conns1MB(b *testing.B) { b.Fatal(err)
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,76 +1,93 @@
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 {
connPool := pool.NewConnPool(&pool.Options{ poolSize int
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)
}
})
} }
func BenchmarkPoolGetPut10Conns(b *testing.B) { func (bm poolGetPutBenchmark) String() string {
benchmarkPoolGetPut(b, 10) return fmt.Sprintf("pool=%d", bm.poolSize)
} }
func BenchmarkPoolGetPut100Conns(b *testing.B) { func BenchmarkPoolGetPut(b *testing.B) {
benchmarkPoolGetPut(b, 100) 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) { type poolGetRemoveBenchmark struct {
benchmarkPoolGetPut(b, 1000) poolSize int
} }
func benchmarkPoolGetRemove(b *testing.B, poolSize int) { func (bm poolGetRemoveBenchmark) String() string {
connPool := pool.NewConnPool(&pool.Options{ return fmt.Sprintf("pool=%d", bm.poolSize)
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 BenchmarkPoolGetRemove10Conns(b *testing.B) { func BenchmarkPoolGetRemove(b *testing.B) {
benchmarkPoolGetRemove(b, 10) 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) { b.ResetTimer()
benchmarkPoolGetRemove(b, 100)
}
func BenchmarkPoolGetRemove1000Conns(b *testing.B) { b.RunParallel(func(pb *testing.PB) {
benchmarkPoolGetRemove(b, 1000) for pb.Next() {
cn, err := connPool.Get()
if err != nil {
b.Fatal(err)
}
connPool.Remove(cn)
}
})
})
}
} }