File: parser_test.go

package info (click to toggle)
golang-gopkg-redis.v2 2.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 280 kB
  • sloc: makefile: 6
file content (54 lines) | stat: -rw-r--r-- 1,111 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package redis

import (
	"testing"

	"gopkg.in/bufio.v1"
)

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) {
	benchmarkParseReply(b, "*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", parseSlice, false)
}

func benchmarkParseReply(b *testing.B, reply string, p multiBulkParser, wanterr bool) {
	b.StopTimer()

	buf := &bufio.Buffer{}
	rd := bufio.NewReader(buf)
	for i := 0; i < b.N; i++ {
		buf.WriteString(reply)
	}

	b.StartTimer()

	for i := 0; i < b.N; i++ {
		_, err := parseReply(rd, p)
		if !wanterr && err != nil {
			panic(err)
		}
	}
}

func BenchmarkAppendArgs(b *testing.B) {
	buf := make([]byte, 0, 64)
	args := []string{"hello", "world", "foo", "bar"}
	for i := 0; i < b.N; i++ {
		appendArgs(buf, args)
	}
}