1
0
mirror of https://github.com/redis/go-redis.git synced 2025-11-04 02:33:24 +03:00
Files
go-redis/internal/proto/reader_test.go
iliya 9c77386b08 chore(tests): internal/proto/peek_push_notification_test : Refactor test helpers to… (#3563)
* internal/proto/peek_push_notification_test : Refactor test helpers to use fmt.Fprintf for buffers

Replaced buf.WriteString(fmt.Sprintf(...)) with fmt.Fprintf or fmt.Fprint in test helper functions for improved clarity and efficiency. This change affects push notification and RESP3 test utilities.

* peek_push_notification_test: revert prev formatting

* all: replace buf.WriteString with fmt.FprintF for consistency

---------

Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com>
2025-10-28 11:41:45 +02:00

102 lines
2.6 KiB
Go

package proto_test
import (
"bytes"
"fmt"
"io"
"testing"
"github.com/redis/go-redis/v9/internal/proto"
)
func BenchmarkReader_ParseReply_Status(b *testing.B) {
benchmarkParseReply(b, "+OK\r\n", false)
}
func BenchmarkReader_ParseReply_Int(b *testing.B) {
benchmarkParseReply(b, ":1\r\n", false)
}
func BenchmarkReader_ParseReply_Float(b *testing.B) {
benchmarkParseReply(b, ",123.456\r\n", false)
}
func BenchmarkReader_ParseReply_Bool(b *testing.B) {
benchmarkParseReply(b, "#t\r\n", false)
}
func BenchmarkReader_ParseReply_BigInt(b *testing.B) {
benchmarkParseReply(b, "(3492890328409238509324850943850943825024385\r\n", false)
}
func BenchmarkReader_ParseReply_Error(b *testing.B) {
benchmarkParseReply(b, "-Error message\r\n", true)
}
func BenchmarkReader_ParseReply_Nil(b *testing.B) {
benchmarkParseReply(b, "_\r\n", true)
}
func BenchmarkReader_ParseReply_BlobError(b *testing.B) {
benchmarkParseReply(b, "!21\r\nSYNTAX invalid syntax", true)
}
func BenchmarkReader_ParseReply_String(b *testing.B) {
benchmarkParseReply(b, "$5\r\nhello\r\n", false)
}
func BenchmarkReader_ParseReply_Verb(b *testing.B) {
benchmarkParseReply(b, "$9\r\ntxt:hello\r\n", false)
}
func BenchmarkReader_ParseReply_Slice(b *testing.B) {
benchmarkParseReply(b, "*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", false)
}
func BenchmarkReader_ParseReply_Set(b *testing.B) {
benchmarkParseReply(b, "~2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", false)
}
func BenchmarkReader_ParseReply_Push(b *testing.B) {
benchmarkParseReply(b, ">2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", false)
}
func BenchmarkReader_ParseReply_Map(b *testing.B) {
benchmarkParseReply(b, "%2\r\n$5\r\nhello\r\n$5\r\nworld\r\n+key\r\n+value\r\n", false)
}
func BenchmarkReader_ParseReply_Attr(b *testing.B) {
benchmarkParseReply(b, "%1\r\n+key\r\n+value\r\n+hello\r\n", false)
}
func TestReader_ReadLine(t *testing.T) {
original := bytes.Repeat([]byte("a"), 8192)
original[len(original)-2] = '\r'
original[len(original)-1] = '\n'
r := proto.NewReader(bytes.NewReader(original))
read, err := r.ReadLine()
if err != nil && err != io.EOF {
t.Errorf("Should be able to read the full buffer: %v", err)
}
if !bytes.Equal(read, original[:len(original)-2]) {
t.Errorf("Values must be equal: %d expected %d", len(read), len(original[:len(original)-2]))
}
}
func benchmarkParseReply(b *testing.B, reply string, wanterr bool) {
buf := new(bytes.Buffer)
for i := 0; i < b.N; i++ {
fmt.Fprint(buf, reply)
}
p := proto.NewReader(buf)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := p.ReadReply()
if !wanterr && err != nil {
b.Fatal(err)
}
}
}