...
1 package proto_test
2
3 import (
4 "bytes"
5 "io"
6 "testing"
7
8 "github.com/redis/go-redis/v9/internal/proto"
9 )
10
11 func BenchmarkReader_ParseReply_Status(b *testing.B) {
12 benchmarkParseReply(b, "+OK\r\n", false)
13 }
14
15 func BenchmarkReader_ParseReply_Int(b *testing.B) {
16 benchmarkParseReply(b, ":1\r\n", false)
17 }
18
19 func BenchmarkReader_ParseReply_Float(b *testing.B) {
20 benchmarkParseReply(b, ",123.456\r\n", false)
21 }
22
23 func BenchmarkReader_ParseReply_Bool(b *testing.B) {
24 benchmarkParseReply(b, "#t\r\n", false)
25 }
26
27 func BenchmarkReader_ParseReply_BigInt(b *testing.B) {
28 benchmarkParseReply(b, "(3492890328409238509324850943850943825024385\r\n", false)
29 }
30
31 func BenchmarkReader_ParseReply_Error(b *testing.B) {
32 benchmarkParseReply(b, "-Error message\r\n", true)
33 }
34
35 func BenchmarkReader_ParseReply_Nil(b *testing.B) {
36 benchmarkParseReply(b, "_\r\n", true)
37 }
38
39 func BenchmarkReader_ParseReply_BlobError(b *testing.B) {
40 benchmarkParseReply(b, "!21\r\nSYNTAX invalid syntax", true)
41 }
42
43 func BenchmarkReader_ParseReply_String(b *testing.B) {
44 benchmarkParseReply(b, "$5\r\nhello\r\n", false)
45 }
46
47 func BenchmarkReader_ParseReply_Verb(b *testing.B) {
48 benchmarkParseReply(b, "$9\r\ntxt:hello\r\n", false)
49 }
50
51 func BenchmarkReader_ParseReply_Slice(b *testing.B) {
52 benchmarkParseReply(b, "*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", false)
53 }
54
55 func BenchmarkReader_ParseReply_Set(b *testing.B) {
56 benchmarkParseReply(b, "~2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", false)
57 }
58
59 func BenchmarkReader_ParseReply_Push(b *testing.B) {
60 benchmarkParseReply(b, ">2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", false)
61 }
62
63 func BenchmarkReader_ParseReply_Map(b *testing.B) {
64 benchmarkParseReply(b, "%2\r\n$5\r\nhello\r\n$5\r\nworld\r\n+key\r\n+value\r\n", false)
65 }
66
67 func BenchmarkReader_ParseReply_Attr(b *testing.B) {
68 benchmarkParseReply(b, "%1\r\n+key\r\n+value\r\n+hello\r\n", false)
69 }
70
71 func TestReader_ReadLine(t *testing.T) {
72 original := bytes.Repeat([]byte("a"), 8192)
73 original[len(original)-2] = '\r'
74 original[len(original)-1] = '\n'
75 r := proto.NewReader(bytes.NewReader(original))
76 read, err := r.ReadLine()
77 if err != nil && err != io.EOF {
78 t.Errorf("Should be able to read the full buffer: %v", err)
79 }
80
81 if !bytes.Equal(read, original[:len(original)-2]) {
82 t.Errorf("Values must be equal: %d expected %d", len(read), len(original[:len(original)-2]))
83 }
84 }
85
86 func benchmarkParseReply(b *testing.B, reply string, wanterr bool) {
87 buf := new(bytes.Buffer)
88 for i := 0; i < b.N; i++ {
89 buf.WriteString(reply)
90 }
91 p := proto.NewReader(buf)
92 b.ResetTimer()
93
94 for i := 0; i < b.N; i++ {
95 _, err := p.ReadReply()
96 if !wanterr && err != nil {
97 b.Fatal(err)
98 }
99 }
100 }
101
View as plain text