2012-07-27 15:43:30 +04:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2012-08-12 22:41:44 +04:00
|
|
|
"github.com/vmihailenco/bufio"
|
2012-07-27 15:43:30 +04:00
|
|
|
. "launchpad.net/gocheck"
|
|
|
|
|
|
|
|
"github.com/vmihailenco/redis"
|
|
|
|
)
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-08-09 14:12:41 +04:00
|
|
|
type LineReader struct {
|
|
|
|
line []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLineReader(line []byte) *LineReader {
|
|
|
|
return &LineReader{line: line}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LineReader) Read(buf []byte) (int, error) {
|
|
|
|
return copy(buf, r.line), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-07-27 15:43:30 +04:00
|
|
|
type RequestTest struct{}
|
|
|
|
|
|
|
|
var _ = Suite(&RequestTest{})
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (t *RequestTest) SetUpTest(c *C) {}
|
|
|
|
|
|
|
|
func (t *RequestTest) TearDownTest(c *C) {}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-08-20 13:00:59 +04:00
|
|
|
func (t *RequestTest) benchmarkReq(c *C, reqString string, req redis.Req, checker Checker, expected interface{}) {
|
2012-07-27 15:43:30 +04:00
|
|
|
c.StopTimer()
|
|
|
|
|
2012-08-20 13:00:59 +04:00
|
|
|
lineReader := NewLineReader([]byte(reqString))
|
2012-08-09 14:12:41 +04:00
|
|
|
rd := bufio.NewReaderSize(lineReader, 1024)
|
2012-07-27 15:43:30 +04:00
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2012-08-20 13:00:59 +04:00
|
|
|
vIface, err := req.ParseReply(rd)
|
2012-07-27 15:43:30 +04:00
|
|
|
c.Check(err, IsNil)
|
2012-08-20 13:00:59 +04:00
|
|
|
c.Check(vIface, checker, expected)
|
|
|
|
req.SetVal(vIface)
|
2012-07-27 15:43:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
c.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < c.N; i++ {
|
|
|
|
v, _ := req.ParseReply(rd)
|
|
|
|
req.SetVal(v)
|
|
|
|
}
|
|
|
|
}
|
2012-08-20 13:00:59 +04:00
|
|
|
|
|
|
|
func (t *RequestTest) BenchmarkStatusReq(c *C) {
|
|
|
|
t.benchmarkReq(c, "+OK\r\n", redis.NewStatusReq(), Equals, "OK")
|
|
|
|
}
|
|
|
|
|
2012-08-24 16:16:12 +04:00
|
|
|
func (t *RequestTest) BenchmarkIntReq(c *C) {
|
|
|
|
t.benchmarkReq(c, ":1\r\n", redis.NewIntReq(), Equals, int64(1))
|
|
|
|
}
|
|
|
|
|
2012-08-20 13:00:59 +04:00
|
|
|
func (t *RequestTest) BenchmarkStringReq(c *C) {
|
|
|
|
t.benchmarkReq(c, "$5\r\nhello\r\n", redis.NewStringReq(), Equals, "hello")
|
|
|
|
}
|
|
|
|
|
2012-08-24 16:16:12 +04:00
|
|
|
func (t *RequestTest) BenchmarkFloatReq(c *C) {
|
|
|
|
t.benchmarkReq(c, "$5\r\n1.111\r\n", redis.NewFloatReq(), Equals, 1.111)
|
|
|
|
}
|
|
|
|
|
2012-08-20 13:00:59 +04:00
|
|
|
func (t *RequestTest) BenchmarkStringSliceReq(c *C) {
|
|
|
|
t.benchmarkReq(
|
|
|
|
c,
|
|
|
|
"*2\r\n$5\r\nhello\r\n$5\r\nhello\r\n",
|
|
|
|
redis.NewStringSliceReq(),
|
|
|
|
DeepEquals,
|
|
|
|
[]string{"hello", "hello"},
|
|
|
|
)
|
|
|
|
}
|
2012-08-24 16:16:12 +04:00
|
|
|
|
|
|
|
func (t *RequestTest) BenchmarkIfaceSliceReq(c *C) {
|
|
|
|
t.benchmarkReq(
|
|
|
|
c,
|
|
|
|
"*2\r\n$5\r\nhello\r\n$5\r\nhello\r\n",
|
|
|
|
redis.NewIfaceSliceReq(),
|
|
|
|
DeepEquals,
|
|
|
|
[]interface{}{"hello", "hello"},
|
|
|
|
)
|
|
|
|
}
|