2016-07-02 15:52:10 +03:00
|
|
|
package proto_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-10-09 16:06:47 +03:00
|
|
|
"io"
|
2016-07-02 15:52:10 +03:00
|
|
|
"testing"
|
|
|
|
|
2020-03-11 17:29:16 +03:00
|
|
|
"github.com/go-redis/redis/v8/internal/proto"
|
2016-07-02 15:52:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkReader_ParseReply_Status(b *testing.B) {
|
|
|
|
benchmarkParseReply(b, "+OK\r\n", nil, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkReader_ParseReply_Int(b *testing.B) {
|
|
|
|
benchmarkParseReply(b, ":1\r\n", nil, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkReader_ParseReply_Error(b *testing.B) {
|
|
|
|
benchmarkParseReply(b, "-Error message\r\n", nil, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkReader_ParseReply_String(b *testing.B) {
|
|
|
|
benchmarkParseReply(b, "$5\r\nhello\r\n", nil, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkReader_ParseReply_Slice(b *testing.B) {
|
|
|
|
benchmarkParseReply(b, "*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", multiBulkParse, false)
|
|
|
|
}
|
|
|
|
|
2020-10-09 12:35:14 +03:00
|
|
|
func TestReader_ReadLine(t *testing.T) {
|
|
|
|
original := bytes.Repeat([]byte("a"), 8192)
|
2020-10-09 16:06:47 +03:00
|
|
|
original[len(original)-2] = '\r'
|
|
|
|
original[len(original)-1] = '\n'
|
2020-10-09 12:35:14 +03:00
|
|
|
r := proto.NewReader(bytes.NewReader(original))
|
|
|
|
read, err := r.ReadLine()
|
2020-10-09 16:06:47 +03:00
|
|
|
if err != nil && err != io.EOF {
|
2020-10-09 12:35:14 +03:00
|
|
|
t.Errorf("Should be able to read the full buffer: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-10-09 16:06:47 +03:00
|
|
|
if bytes.Compare(read, original[:len(original)-2]) != 0 {
|
|
|
|
t.Errorf("Values must be equal: %d expected %d", len(read), len(original[:len(original)-2]))
|
2020-10-09 12:35:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-02 15:52:10 +03:00
|
|
|
func benchmarkParseReply(b *testing.B, reply string, m proto.MultiBulkParse, wanterr bool) {
|
2017-02-08 12:24:09 +03:00
|
|
|
buf := new(bytes.Buffer)
|
2016-07-02 15:52:10 +03:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
buf.WriteString(reply)
|
|
|
|
}
|
2018-08-17 13:56:37 +03:00
|
|
|
p := proto.NewReader(buf)
|
2016-07-02 15:52:10 +03:00
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := p.ReadReply(m)
|
|
|
|
if !wanterr && err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 13:56:37 +03:00
|
|
|
func multiBulkParse(p *proto.Reader, n int64) (interface{}, error) {
|
2016-07-02 15:52:10 +03:00
|
|
|
vv := make([]interface{}, 0, n)
|
|
|
|
for i := int64(0); i < n; i++ {
|
|
|
|
v, err := p.ReadReply(multiBulkParse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vv = append(vv, v)
|
|
|
|
}
|
|
|
|
return vv, nil
|
|
|
|
}
|