-
Notifications
You must be signed in to change notification settings - Fork 13
/
benchmarks_test.go
93 lines (80 loc) · 1.96 KB
/
benchmarks_test.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package csproto_test
import (
"testing"
"github.com/CrowdStrike/csproto"
)
func BenchmarkEncodeString(b *testing.B) {
var (
s = "this is a reasonably long string so that the encoder function has to actually do some work"
buf = make([]byte, len(s))
)
for n := 0; n < b.N; n++ {
enc := csproto.NewEncoder(buf)
enc.EncodeString(1, s)
}
}
func BenchmarkSafeDecodeString(b *testing.B) {
var (
// protobuf length-delimited encoding of the string "this is a test"
d = []byte{0x12, 0xE, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74}
str string
)
dec := csproto.NewDecoder(d)
b.ResetTimer()
for n := 0; n < b.N; n++ {
dec.Reset()
_, _, _ = dec.DecodeTag()
s, _ := dec.DecodeString()
str = s // use s it's not optimized away
}
_ = str
}
func BenchmarkFastDecodeString(b *testing.B) {
var (
// protobuf length-delimited encoding of the string "this is a test"
d = []byte{0x12, 0xE, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74}
str string
)
dec := csproto.NewDecoder(d)
dec.SetMode(csproto.DecoderModeFast)
b.ResetTimer()
for n := 0; n < b.N; n++ {
dec.Reset()
_, _, _ = dec.DecodeTag()
s, _ := dec.DecodeString()
str = s // use s it's not optimized away
}
_ = str
}
func BenchmarkDecodeFixed32(b *testing.B) {
var (
// protobuf encoding of 1138
d = []byte{0x25, 0x72, 0x04, 0x00, 0x00}
val uint32
)
dec := csproto.NewDecoder(d)
b.ResetTimer()
for n := 0; n < b.N; n++ {
dec.Reset()
_, _, _ = dec.DecodeTag() // consume the tag/wire type
v, _ := dec.DecodeFixed32()
val = v
}
_ = val
}
func BenchmarkDecodeFixed64(b *testing.B) {
var (
// protobuf encoding of 1138
d = []byte{0x21, 0x72, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
val uint64
)
dec := csproto.NewDecoder(d)
b.ResetTimer()
for n := 0; n < b.N; n++ {
dec.Reset()
_, _, _ = dec.DecodeTag() // consume the tag/wire type
v, _ := dec.DecodeFixed64()
val = v
}
_ = val
}