redis/internal/pool/bench_test.go

94 lines
1.6 KiB
Go
Raw Normal View History

package pool_test
import (
2018-08-10 13:55:57 +03:00
"fmt"
"testing"
"time"
2017-02-18 17:42:34 +03:00
"github.com/go-redis/redis/internal/pool"
)
2018-08-10 13:55:57 +03:00
type poolGetPutBenchmark struct {
poolSize int
}
2018-08-10 13:55:57 +03:00
func (bm poolGetPutBenchmark) String() string {
return fmt.Sprintf("pool=%d", bm.poolSize)
}
2018-08-10 13:55:57 +03:00
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,
})
2018-08-10 13:55:57 +03:00
b.ResetTimer()
2018-08-10 13:55:57 +03:00
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
cn, err := connPool.Get()
if err != nil {
b.Fatal(err)
}
connPool.Put(cn)
}
})
})
}
}
2018-08-10 13:55:57 +03:00
type poolGetRemoveBenchmark struct {
poolSize int
}
2018-08-10 13:55:57 +03:00
func (bm poolGetRemoveBenchmark) String() string {
return fmt.Sprintf("pool=%d", bm.poolSize)
}
2018-08-10 13:55:57 +03:00
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,
})
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
cn, err := connPool.Get()
if err != nil {
b.Fatal(err)
}
connPool.Remove(cn)
}
})
})
}
}