redis/parser_test.go

56 lines
1.1 KiB
Go
Raw Normal View History

2014-07-04 16:19:45 +04:00
package redis
import (
2015-09-03 17:55:31 +03:00
"bufio"
"bytes"
2014-07-04 16:19:45 +04:00
"testing"
)
func BenchmarkParseReplyStatus(b *testing.B) {
benchmarkParseReply(b, "+OK\r\n", nil, false)
}
func BenchmarkParseReplyInt(b *testing.B) {
benchmarkParseReply(b, ":1\r\n", nil, false)
}
func BenchmarkParseReplyError(b *testing.B) {
benchmarkParseReply(b, "-Error message\r\n", nil, true)
}
func BenchmarkParseReplyString(b *testing.B) {
benchmarkParseReply(b, "$5\r\nhello\r\n", nil, false)
}
func BenchmarkParseReplySlice(b *testing.B) {
2015-10-07 17:09:20 +03:00
benchmarkParseReply(b, "*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", sliceParser, false)
2014-07-04 16:19:45 +04:00
}
func benchmarkParseReply(b *testing.B, reply string, p multiBulkParser, wanterr bool) {
2015-09-03 17:55:31 +03:00
buf := &bytes.Buffer{}
2014-07-04 16:19:45 +04:00
for i := 0; i < b.N; i++ {
buf.WriteString(reply)
}
2015-09-03 17:55:31 +03:00
cn := &conn{
rd: bufio.NewReader(buf),
buf: make([]byte, 0, defaultBufSize),
}
2014-07-04 16:19:45 +04:00
2015-09-03 17:55:31 +03:00
b.ResetTimer()
2014-07-04 16:19:45 +04:00
for i := 0; i < b.N; i++ {
2015-10-07 17:09:20 +03:00
_, err := readReply(cn, p)
2014-07-04 16:19:45 +04:00
if !wanterr && err != nil {
2015-09-03 17:55:31 +03:00
b.Fatal(err)
2014-07-04 16:19:45 +04:00
}
}
}
func BenchmarkAppendArgs(b *testing.B) {
buf := make([]byte, 0, 64)
args := []interface{}{"hello", "world", "foo", "bar"}
2014-07-04 16:19:45 +04:00
for i := 0; i < b.N; i++ {
appendArgs(buf, args)
}
}