Skip to content

Commit

Permalink
Extract pool package. Add pool benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Mar 12, 2016
1 parent aad4561 commit ad0739b
Show file tree
Hide file tree
Showing 25 changed files with 969 additions and 898 deletions.
59 changes: 54 additions & 5 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package redis_test

import (
"bytes"
"errors"
"net"
"testing"
"time"

redigo "github.com/garyburd/redigo/redis"

"gopkg.in/redis.v3"
"gopkg.in/redis.v3/internal/pool"
)

func benchmarkRedisClient(poolSize int) *redis.Client {
Expand Down Expand Up @@ -274,11 +277,11 @@ func BenchmarkZAdd(b *testing.B) {
})
}

func BenchmarkPool(b *testing.B) {
client := benchmarkRedisClient(10)
defer client.Close()

pool := client.Pool()
func benchmarkPoolGetPut(b *testing.B, poolSize int) {
dial := func() (*pool.Conn, error) {
return pool.NewConn(&net.TCPConn{}), nil
}
pool := pool.NewConnPool(dial, poolSize, time.Second, 0)

b.ResetTimer()

Expand All @@ -294,3 +297,49 @@ func BenchmarkPool(b *testing.B) {
}
})
}

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() (*pool.Conn, error) {
return pool.NewConn(&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.Remove(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)
}
5 changes: 3 additions & 2 deletions cluster_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package redis

import (
"gopkg.in/redis.v3/internal/hashtag"
"gopkg.in/redis.v3/internal/pool"
)

// ClusterPipeline is not thread-safe.
Expand Down Expand Up @@ -96,9 +97,9 @@ func (pipe *ClusterPipeline) Close() error {
}

func (pipe *ClusterPipeline) execClusterCmds(
cn *conn, cmds []Cmder, failedCmds map[string][]Cmder,
cn *pool.Conn, cmds []Cmder, failedCmds map[string][]Cmder,
) (map[string][]Cmder, error) {
if err := cn.writeCmds(cmds...); err != nil {
if err := writeCmd(cn, cmds...); err != nil {
setCmdsErr(cmds, err)
return failedCmds, err
}
Expand Down
56 changes: 38 additions & 18 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strconv"
"strings"
"time"

"gopkg.in/redis.v3/internal/pool"
)

var (
Expand All @@ -28,7 +30,7 @@ var (

type Cmder interface {
args() []interface{}
readReply(*conn) error
readReply(*pool.Conn) error
setErr(error)
reset()

Expand All @@ -51,6 +53,20 @@ func resetCmds(cmds []Cmder) {
}
}

func writeCmd(cn *pool.Conn, cmds ...Cmder) error {
cn.Buf = cn.Buf[:0]
for _, cmd := range cmds {
var err error
cn.Buf, err = appendArgs(cn.Buf, cmd.args())
if err != nil {
return err
}
}

_, err := cn.Write(cn.Buf)
return err
}

func cmdString(cmd Cmder, val interface{}) string {
var ss []string
for _, arg := range cmd.args() {
Expand Down Expand Up @@ -143,7 +159,7 @@ func (cmd *Cmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *Cmd) readReply(cn *conn) error {
func (cmd *Cmd) readReply(cn *pool.Conn) error {
val, err := readReply(cn, sliceParser)
if err != nil {
cmd.err = err
Expand Down Expand Up @@ -188,7 +204,7 @@ func (cmd *SliceCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *SliceCmd) readReply(cn *conn) error {
func (cmd *SliceCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, sliceParser)
if err != nil {
cmd.err = err
Expand Down Expand Up @@ -231,7 +247,7 @@ func (cmd *StatusCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *StatusCmd) readReply(cn *conn) error {
func (cmd *StatusCmd) readReply(cn *pool.Conn) error {
cmd.val, cmd.err = readStringReply(cn)
return cmd.err
}
Expand Down Expand Up @@ -265,7 +281,7 @@ func (cmd *IntCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *IntCmd) readReply(cn *conn) error {
func (cmd *IntCmd) readReply(cn *pool.Conn) error {
cmd.val, cmd.err = readIntReply(cn)
return cmd.err
}
Expand Down Expand Up @@ -303,7 +319,7 @@ func (cmd *DurationCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *DurationCmd) readReply(cn *conn) error {
func (cmd *DurationCmd) readReply(cn *pool.Conn) error {
n, err := readIntReply(cn)
if err != nil {
cmd.err = err
Expand Down Expand Up @@ -344,7 +360,7 @@ func (cmd *BoolCmd) String() string {

var ok = []byte("OK")

func (cmd *BoolCmd) readReply(cn *conn) error {
func (cmd *BoolCmd) readReply(cn *pool.Conn) error {
v, err := readReply(cn, nil)
// `SET key value NX` returns nil when key already exists. But
// `SETNX key value` returns bool (0/1). So convert nil to bool.
Expand Down Expand Up @@ -430,13 +446,17 @@ func (cmd *StringCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *StringCmd) readReply(cn *conn) error {
func (cmd *StringCmd) readReply(cn *pool.Conn) error {
b, err := readBytesReply(cn)
if err != nil {
cmd.err = err
return err
}
cmd.val = cn.copyBuf(b)

new := make([]byte, len(b))
copy(new, b)
cmd.val = new

return nil
}

Expand Down Expand Up @@ -469,7 +489,7 @@ func (cmd *FloatCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *FloatCmd) readReply(cn *conn) error {
func (cmd *FloatCmd) readReply(cn *pool.Conn) error {
cmd.val, cmd.err = readFloatReply(cn)
return cmd.err
}
Expand Down Expand Up @@ -503,7 +523,7 @@ func (cmd *StringSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *StringSliceCmd) readReply(cn *conn) error {
func (cmd *StringSliceCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, stringSliceParser)
if err != nil {
cmd.err = err
Expand Down Expand Up @@ -542,7 +562,7 @@ func (cmd *BoolSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *BoolSliceCmd) readReply(cn *conn) error {
func (cmd *BoolSliceCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, boolSliceParser)
if err != nil {
cmd.err = err
Expand Down Expand Up @@ -581,7 +601,7 @@ func (cmd *StringStringMapCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *StringStringMapCmd) readReply(cn *conn) error {
func (cmd *StringStringMapCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, stringStringMapParser)
if err != nil {
cmd.err = err
Expand Down Expand Up @@ -620,7 +640,7 @@ func (cmd *StringIntMapCmd) reset() {
cmd.err = nil
}

func (cmd *StringIntMapCmd) readReply(cn *conn) error {
func (cmd *StringIntMapCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, stringIntMapParser)
if err != nil {
cmd.err = err
Expand Down Expand Up @@ -659,7 +679,7 @@ func (cmd *ZSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *ZSliceCmd) readReply(cn *conn) error {
func (cmd *ZSliceCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, zSliceParser)
if err != nil {
cmd.err = err
Expand Down Expand Up @@ -703,7 +723,7 @@ func (cmd *ScanCmd) String() string {
return cmdString(cmd, cmd.keys)
}

func (cmd *ScanCmd) readReply(cn *conn) error {
func (cmd *ScanCmd) readReply(cn *pool.Conn) error {
keys, cursor, err := readScanReply(cn)
if err != nil {
cmd.err = err
Expand Down Expand Up @@ -751,7 +771,7 @@ func (cmd *ClusterSlotCmd) reset() {
cmd.err = nil
}

func (cmd *ClusterSlotCmd) readReply(cn *conn) error {
func (cmd *ClusterSlotCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, clusterSlotInfoSliceParser)
if err != nil {
cmd.err = err
Expand Down Expand Up @@ -838,7 +858,7 @@ func (cmd *GeoLocationCmd) String() string {
return cmdString(cmd, cmd.locations)
}

func (cmd *GeoLocationCmd) readReply(cn *conn) error {
func (cmd *GeoLocationCmd) readReply(cn *pool.Conn) error {
reply, err := readArrayReply(cn, newGeoLocationSliceParser(cmd.q))
if err != nil {
cmd.err = err
Expand Down
Loading

0 comments on commit ad0739b

Please sign in to comment.