2016-03-12 13:38:52 +03:00
|
|
|
package pool_test
|
|
|
|
|
|
|
|
import (
|
2019-07-04 11:18:06 +03:00
|
|
|
"context"
|
2018-08-10 13:55:57 +03:00
|
|
|
"fmt"
|
2016-03-12 13:38:52 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-03-11 17:29:16 +03:00
|
|
|
"github.com/go-redis/redis/v8/internal/pool"
|
2016-03-12 13:38:52 +03:00
|
|
|
)
|
|
|
|
|
2018-08-10 13:55:57 +03:00
|
|
|
type poolGetPutBenchmark struct {
|
|
|
|
poolSize int
|
2016-03-12 13:38:52 +03:00
|
|
|
}
|
|
|
|
|
2018-08-10 13:55:57 +03:00
|
|
|
func (bm poolGetPutBenchmark) String() string {
|
|
|
|
return fmt.Sprintf("pool=%d", bm.poolSize)
|
2016-03-12 13:38:52 +03:00
|
|
|
}
|
|
|
|
|
2018-08-10 13:55:57 +03:00
|
|
|
func BenchmarkPoolGetPut(b *testing.B) {
|
2020-08-15 15:36:02 +03:00
|
|
|
ctx := context.Background()
|
2018-08-10 13:55:57 +03:00
|
|
|
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,
|
|
|
|
})
|
2016-03-12 13:38:52 +03:00
|
|
|
|
2018-08-10 13:55:57 +03:00
|
|
|
b.ResetTimer()
|
2016-03-12 13:38:52 +03:00
|
|
|
|
2018-08-10 13:55:57 +03:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
2020-08-15 15:36:02 +03:00
|
|
|
cn, err := connPool.Get(ctx)
|
2018-08-10 13:55:57 +03:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Put(ctx, cn)
|
2018-08-10 13:55:57 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2016-03-12 13:38:52 +03:00
|
|
|
}
|
|
|
|
|
2018-08-10 13:55:57 +03:00
|
|
|
type poolGetRemoveBenchmark struct {
|
|
|
|
poolSize int
|
2016-03-12 13:38:52 +03:00
|
|
|
}
|
|
|
|
|
2018-08-10 13:55:57 +03:00
|
|
|
func (bm poolGetRemoveBenchmark) String() string {
|
|
|
|
return fmt.Sprintf("pool=%d", bm.poolSize)
|
2016-03-12 13:38:52 +03:00
|
|
|
}
|
|
|
|
|
2018-08-10 13:55:57 +03:00
|
|
|
func BenchmarkPoolGetRemove(b *testing.B) {
|
2020-08-15 15:36:02 +03:00
|
|
|
ctx := context.Background()
|
2018-08-10 13:55:57 +03:00
|
|
|
benchmarks := []poolGetRemoveBenchmark{
|
|
|
|
{1},
|
|
|
|
{2},
|
|
|
|
{8},
|
|
|
|
{32},
|
|
|
|
{64},
|
|
|
|
{128},
|
|
|
|
}
|
2020-08-15 15:36:02 +03:00
|
|
|
|
2018-08-10 13:55:57 +03:00
|
|
|
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() {
|
2020-08-15 15:36:02 +03:00
|
|
|
cn, err := connPool.Get(ctx)
|
2018-08-10 13:55:57 +03:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Remove(ctx, cn, nil)
|
2018-08-10 13:55:57 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2016-03-12 13:38:52 +03:00
|
|
|
}
|