Merge pull request #275 from go-redis/fix/move-pool-benchmark

Move benchmark to relevant package. Added bench task
This commit is contained in:
Vladimir Mihailenco 2016-03-12 13:09:13 +02:00
commit 7a4f8ede8f
4 changed files with 89 additions and 76 deletions

View File

@ -4,7 +4,10 @@ all: testdeps
testdeps: testdata/redis/src/redis-server testdeps: testdata/redis/src/redis-server
.PHONY: all test testdeps bench: testdeps
go test ./... -test.run=NONE -test.bench=. -test.benchmem
.PHONY: all test testdeps bench
testdata/redis: testdata/redis:
mkdir -p $@ mkdir -p $@

View File

@ -2,15 +2,12 @@ package redis_test
import ( import (
"bytes" "bytes"
"errors"
"net"
"testing" "testing"
"time" "time"
redigo "github.com/garyburd/redigo/redis" redigo "github.com/garyburd/redigo/redis"
"gopkg.in/redis.v3" "gopkg.in/redis.v3"
"gopkg.in/redis.v3/internal/pool"
) )
func benchmarkRedisClient(poolSize int) *redis.Client { func benchmarkRedisClient(poolSize int) *redis.Client {
@ -276,70 +273,3 @@ func BenchmarkZAdd(b *testing.B) {
} }
}) })
} }
func benchmarkPoolGetPut(b *testing.B, poolSize int) {
dial := func() (net.Conn, error) {
return &net.TCPConn{}, nil
}
pool := pool.NewConnPool(dial, poolSize, time.Second, 0)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
conn, _, err := pool.Get()
if err != nil {
b.Fatalf("no error expected on pool.Get but received: %s", err.Error())
}
if err = pool.Put(conn); err != nil {
b.Fatalf("no error expected on pool.Put but received: %s", err.Error())
}
}
})
}
func BenchmarkPoolGetPut10Conns(b *testing.B) {
benchmarkPoolGetPut(b, 10)
}
func BenchmarkPoolGetPut100Conns(b *testing.B) {
benchmarkPoolGetPut(b, 100)
}
func BenchmarkPoolGetPut1000Conns(b *testing.B) {
benchmarkPoolGetPut(b, 1000)
}
func benchmarkPoolGetRemove(b *testing.B, poolSize int) {
dial := func() (net.Conn, error) {
return &net.TCPConn{}, nil
}
pool := pool.NewConnPool(dial, poolSize, time.Second, 0)
removeReason := errors.New("benchmark")
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
conn, _, err := pool.Get()
if err != nil {
b.Fatalf("no error expected on pool.Get but received: %s", err.Error())
}
if err = pool.Replace(conn, removeReason); err != nil {
b.Fatalf("no error expected on pool.Remove but received: %s", err.Error())
}
}
})
}
func BenchmarkPoolGetRemove10Conns(b *testing.B) {
benchmarkPoolGetRemove(b, 10)
}
func BenchmarkPoolGetRemove100Conns(b *testing.B) {
benchmarkPoolGetRemove(b, 100)
}
func BenchmarkPoolGetRemove1000Conns(b *testing.B) {
benchmarkPoolGetRemove(b, 1000)
}

View File

@ -0,0 +1,80 @@
package pool_test
import (
"errors"
"net"
"testing"
"time"
"gopkg.in/redis.v3/internal/pool"
)
func benchmarkPoolGetPut(b *testing.B, poolSize int) {
dial := func() (net.Conn, error) {
return &net.TCPConn{}, nil
}
pool := pool.NewConnPool(dial, poolSize, time.Second, 0)
pool.DialLimiter = nil
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
conn, _, err := pool.Get()
if err != nil {
b.Fatalf("no error expected on pool.Get but received: %s", err.Error())
}
if err = pool.Put(conn); err != nil {
b.Fatalf("no error expected on pool.Put but received: %s", err.Error())
}
}
})
}
func BenchmarkPoolGetPut10Conns(b *testing.B) {
benchmarkPoolGetPut(b, 10)
}
func BenchmarkPoolGetPut100Conns(b *testing.B) {
benchmarkPoolGetPut(b, 100)
}
func BenchmarkPoolGetPut1000Conns(b *testing.B) {
benchmarkPoolGetPut(b, 1000)
}
func benchmarkPoolGetReplace(b *testing.B, poolSize int) {
dial := func() (net.Conn, error) {
return &net.TCPConn{}, nil
}
pool := pool.NewConnPool(dial, poolSize, time.Second, 0)
pool.DialLimiter = nil
removeReason := errors.New("benchmark")
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
conn, _, err := pool.Get()
if err != nil {
b.Fatalf("no error expected on pool.Get but received: %s", err.Error())
}
if err = pool.Replace(conn, removeReason); err != nil {
b.Fatalf("no error expected on pool.Remove but received: %s", err.Error())
}
}
})
}
func BenchmarkPoolGetReplace10Conns(b *testing.B) {
benchmarkPoolGetReplace(b, 10)
}
func BenchmarkPoolGetReplace100Conns(b *testing.B) {
benchmarkPoolGetReplace(b, 100)
}
func BenchmarkPoolGetReplace1000Conns(b *testing.B) {
benchmarkPoolGetReplace(b, 1000)
}

View File

@ -44,11 +44,11 @@ type dialer func() (net.Conn, error)
type ConnPool struct { type ConnPool struct {
_dial dialer _dial dialer
DialLimiter *ratelimit.RateLimiter
poolTimeout time.Duration poolTimeout time.Duration
idleTimeout time.Duration idleTimeout time.Duration
rl *ratelimit.RateLimiter
conns *connList conns *connList
freeConns chan *Conn freeConns chan *Conn
stats PoolStats stats PoolStats
@ -61,11 +61,11 @@ type ConnPool struct {
func NewConnPool(dial dialer, poolSize int, poolTimeout, idleTimeout time.Duration) *ConnPool { func NewConnPool(dial dialer, poolSize int, poolTimeout, idleTimeout time.Duration) *ConnPool {
p := &ConnPool{ p := &ConnPool{
_dial: dial, _dial: dial,
DialLimiter: ratelimit.New(3*poolSize, time.Second),
poolTimeout: poolTimeout, poolTimeout: poolTimeout,
idleTimeout: idleTimeout, idleTimeout: idleTimeout,
rl: ratelimit.New(3*poolSize, time.Second),
conns: newConnList(poolSize), conns: newConnList(poolSize),
freeConns: make(chan *Conn, poolSize), freeConns: make(chan *Conn, poolSize),
} }
@ -128,7 +128,7 @@ func (p *ConnPool) wait() *Conn {
} }
func (p *ConnPool) dial() (net.Conn, error) { func (p *ConnPool) dial() (net.Conn, error) {
if p.rl.Limit() { if p.DialLimiter != nil && p.DialLimiter.Limit() {
err := fmt.Errorf( err := fmt.Errorf(
"redis: you open connections too fast (last_error=%q)", "redis: you open connections too fast (last_error=%q)",
p.loadLastErr(), p.loadLastErr(),