redis/req_test.go

76 lines
1.7 KiB
Go
Raw Normal View History

2012-07-27 15:43:30 +04:00
package redis_test
import (
"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) {}
//------------------------------------------------------------------------------
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()
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++ {
vIface, err := req.ParseReply(rd)
2012-07-27 15:43:30 +04:00
c.Check(err, IsNil)
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)
}
}
func (t *RequestTest) BenchmarkStatusReq(c *C) {
t.benchmarkReq(c, "+OK\r\n", redis.NewStatusReq(), Equals, "OK")
}
func (t *RequestTest) BenchmarkStringReq(c *C) {
t.benchmarkReq(c, "$5\r\nhello\r\n", redis.NewStringReq(), Equals, "hello")
}
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"},
)
}