-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks_test.go
248 lines (230 loc) · 6.24 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package netstring_test
import (
"bytes"
"io"
"strconv"
"testing"
"github.com/markdingo/netstring"
)
type benchNullWriter struct {
}
func (w *benchNullWriter) Write(b []byte) (int, error) {
return len(b), nil
}
// Baseline performance
func BenchmarkEncodeBaseline(b *testing.B) {
sa := []byte{'a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e'}
enc := netstring.NewEncoder(&benchNullWriter{})
for i := 0; i < b.N; i++ {
err := enc.EncodeBytes('A', sa)
if err != nil {
b.Fatal(err)
}
}
}
// Shows cost of range iteration
func BenchmarkEncodeBytes(b *testing.B) {
sa := []byte{'a'}
sb := []byte{'b'}
sc := []byte{'c'}
sd := []byte{'d'}
se := []byte{'e'}
enc := netstring.NewEncoder(&benchNullWriter{})
for i := 0; i < b.N; i++ {
err := enc.EncodeBytes('A', sa, sb, sc, sd, se, sa, sb, sc, sd, se)
if err != nil {
b.Fatal(err)
}
}
}
// Compare with Baseline to show cost of wrapped function and string conversion
func BenchmarkEncodeString1(b *testing.B) {
enc := netstring.NewEncoder(&benchNullWriter{})
s := "abcdeabcde"
for i := 0; i < b.N; i++ {
err := enc.EncodeString('A', s)
if err != nil {
b.Fatal(err)
}
}
}
// Get a relative cost of Encoding vs Decoding
func BenchmarkCompareEncode(b *testing.B) {
wBuf := bytes.NewBuffer(make([]byte, 0, 200))
enc := netstring.NewEncoder(wBuf)
ab := []byte{'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'}
bb := []byte{'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'}
cb := []byte{'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c'}
db := []byte{'d', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd'}
eb := []byte{'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'}
fb := []byte{'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'}
gb := []byte{'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g'}
hb := []byte{'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h'}
ib := []byte{'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'}
jb := []byte{'j', 'j', 'j', 'j', 'j', 'j', 'j', 'j', 'j', 'j'}
for i := 0; i < b.N; i++ {
wBuf.Reset()
enc.EncodeBytes('A', ab)
enc.EncodeBytes('B', bb)
enc.EncodeBytes('C', cb)
enc.EncodeBytes('D', db)
enc.EncodeBytes('E', eb)
enc.EncodeBytes('F', fb)
enc.EncodeBytes('G', gb)
enc.EncodeBytes('H', hb)
enc.EncodeBytes('I', ib)
enc.EncodeBytes('J', jb)
enc.EncodeBytes('z')
}
}
// Decoding is less expensive than encoding - which is a bit of a surprise.
func BenchmarkCompareDecode(b *testing.B) {
var wBuf bytes.Buffer
enc := netstring.NewEncoder(&wBuf)
enc.EncodeString('A', "aaaaaaaaaa")
enc.EncodeString('B', "bbbbbbbbbb")
enc.EncodeString('C', "cccccccccc")
enc.EncodeString('D', "dddddddddd")
enc.EncodeString('E', "eeeeeeeeee")
enc.EncodeString('F', "ffffffffff")
enc.EncodeString('G', "gggggggggg")
enc.EncodeString('H', "hhhhhhhhhh")
enc.EncodeString('I', "iiiiiiiiii")
enc.EncodeString('J', "jjjjjjjjjj")
enc.EncodeBytes('z')
rBuf := bytes.NewReader(wBuf.Bytes())
for i := 0; i < b.N; i++ {
rBuf.Seek(0, io.SeekStart)
dec := netstring.NewDecoder(rBuf)
for j := 'A'; j < 'J'; j++ {
k, buf, err := dec.DecodeKeyed()
if err != nil {
b.Fatal(err)
}
if int(k) != int(j) {
b.Fatal("Wrong Key", j, k)
}
if len(buf) != 10 {
b.Fatal("Wrong length. Expected 10, got", len(buf))
}
}
}
}
type bmStruct struct {
Age int `netstring:"a"`
Country string `netstring:"c"`
TLD []byte `netstring:"t"`
CountryCode []byte `netstring:"C"`
Name string `netstring:"n"`
Height uint16 `netstring:"H"`
Key int64 `netstring:"K"`
}
// The higher-level Marshal function turns out to be about 4 times slower than manually encoding.
func BenchmarkMarshalAuto(b *testing.B) {
s := bmStruct{21, "Iceland", []byte{'i', 'c'}, []byte("354"), "Bjorn", 183, 123456}
enc := netstring.NewEncoder(&benchNullWriter{})
for i := 0; i < b.N; i++ {
err := enc.Marshal('Z', s)
if err != nil {
b.Fatal(err)
}
}
}
// Do what MarshalAuto does, but using the simpler and lower-level Encoders directly. The
// extra cost of Marshal is largely due to the tag fetching, deduping and reflection
// aspects.
func BenchmarkMarshalManual(b *testing.B) {
s := bmStruct{21, "Australia", []byte{'a', 'u'}, []byte("354"), "Bruce", 200, 987654}
enc := netstring.NewEncoder(&benchNullWriter{})
for i := 0; i < b.N; i++ {
err := enc.EncodeInt('a', s.Age)
if err != nil {
b.Fatal(err)
}
err = enc.EncodeString('c', s.Country)
if err != nil {
b.Fatal(err)
}
err = enc.EncodeBytes('t', s.TLD)
if err != nil {
b.Fatal(err)
}
err = enc.EncodeBytes('C', s.CountryCode)
if err != nil {
b.Fatal(err)
}
err = enc.EncodeString('n', s.Name)
if err != nil {
b.Fatal(err)
}
err = enc.EncodeUint('H', uint(s.Height))
if err != nil {
b.Fatal(err)
}
err = enc.EncodeInt64('K', s.Key)
if err != nil {
b.Fatal(err)
}
err = enc.EncodeBytes('Z')
if err != nil {
b.Fatal(err)
}
}
}
// As with marshal, the higher levvel Unmarshal turns out to be about 3-4 times slower
// than manually decoding.
func BenchmarkUnmarshalAuto(b *testing.B) {
in := "3:a99,10:cAustralia,3:tau,4:C354,6:nBruce,4:H200,7:K987654,1:Z,"
rBuf := bytes.NewReader([]byte(in))
for i := 0; i < b.N; i++ {
rBuf.Seek(0, io.SeekStart)
dec := netstring.NewDecoder(rBuf)
var s bmStruct
unk, err := dec.Unmarshal('Z', &s)
if err != nil {
b.Fatal("iter", i, "unmarshal returned", err)
}
if unk != 0 {
b.Fatal("Unknown key returned", unk)
}
}
}
// Do what UnmarshalAuto does, but using the simpler and lower-level Decoders directly.
func BenchmarkUnmarshalManual(b *testing.B) {
in := "3:a99,10:cAustralia,3:tau,4:C354,6:nBruce,4:H200,7:K987654,1:Z,"
rBuf := bytes.NewReader([]byte(in))
for i := 0; i < b.N; i++ {
rBuf.Seek(0, io.SeekStart)
dec := netstring.NewDecoder(rBuf)
var s bmStruct
for {
k, v, e := dec.DecodeKeyed()
if e != nil {
b.Fatal(e)
}
if k == 'Z' {
break
}
switch k {
case 'a':
s.Age, _ = strconv.Atoi(string(v))
case 'c':
s.Country = string(v)
case 't':
s.TLD = v
case 'C':
s.CountryCode = v
case 'n':
s.Name = string(v)
case 'H':
u16, _ := strconv.Atoi(string(v))
s.Height = uint16(u16)
case 'K':
i64, _ := strconv.Atoi(string(v))
s.Key = int64(i64)
default:
b.Fatal("Unexpected Key type", k)
}
}
}
}