-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparco_compiler_test.go
178 lines (161 loc) · 4.19 KB
/
parco_compiler_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
package parco
import (
"bytes"
"encoding/binary"
"encoding/json"
"github.com/vmihailenco/msgpack/v5"
"math"
"math/rand"
"strconv"
"strings"
"testing"
)
type compileFuncType func(t TestStruct) (int, error)
type compileFuncFactory func(t TestStruct) compileFuncType
type TestStruct struct {
Name string
Str string `json:"str"`
Num uint16 `json:"num"`
Arr []uint16 `json:"arr"`
Map map[string]uint8 `json:"map"`
Bool bool `json:"bool"`
Float32 float32 `json:"float32"`
Float64 float64 `json:"float64"`
}
func fillSeq(le int) []uint16 {
r := make([]uint16, le)
for i := 0; i < le; i++ {
r[i] = uint16(rand.Intn(math.MaxUint16))
}
return r
}
func fillMap(le int) map[string]uint8 {
r := make(map[string]uint8, le)
for i := 0; i < le; i++ {
r[strconv.FormatInt(int64(i), 10)] = uint8(rand.Intn(math.MaxUint8))
}
return r
}
func newCompiler(arrLen int) *Compiler[TestStruct] {
var SliceHeadType IntType
if arrLen < 256 {
SliceHeadType = UInt8Header()
} else {
SliceHeadType = UInt16HeaderLE()
}
return CompilerModel[TestStruct]().
Varchar(func(ts *TestStruct) string { return ts.Name }).
Varchar(func(ts *TestStruct) string { return ts.Str }).
UInt16LE(func(ts *TestStruct) uint16 { return ts.Num }).
Slice(SliceField[TestStruct, uint16](
SliceHeadType,
UInt16LE(),
nil,
func(ts *TestStruct) SliceView[uint16] {
return ts.Arr
},
)).
Map(MapField[TestStruct, string, uint8](
SliceHeadType,
SmallVarchar(),
UInt8(),
nil,
func(ts *TestStruct) map[string]uint8 {
return ts.Map
},
)).
Bool(func(ts *TestStruct) bool { return ts.Bool }).
Float32(binary.LittleEndian, func(ts *TestStruct) float32 { return ts.Float32 }).
Float64(binary.LittleEndian, func(ts *TestStruct) float64 { return ts.Float64 })
}
var tests = []TestStruct{
{
Name: "small size",
Str: "oh hi Mark",
Num: 42,
Arr: fillSeq(10),
Map: fillMap(10),
Bool: true,
Float32: math.MaxFloat32,
Float64: math.MaxFloat64,
},
{
Name: "medium size",
Str: strings.Repeat("oh hi Mark! ", 10),
Num: 42134,
Arr: fillSeq(100),
Map: fillMap(100),
Bool: true,
Float32: math.MaxFloat32,
Float64: math.MaxFloat64,
},
{
Name: "large size",
Str: strings.Repeat("oh hi Mark! ", 100),
Num: math.MaxUint16,
Arr: fillSeq(1000),
Map: fillMap(1000),
Bool: true,
Float32: math.MaxFloat32,
Float64: math.MaxFloat64,
},
}
func jsonCompilerFactory(_ TestStruct) compileFuncType {
return func(t TestStruct) (int, error) {
bts, err := json.Marshal(t)
return len(bts), err
}
}
func msgPackCompilerFactory(_ TestStruct) compileFuncType {
return func(t TestStruct) (int, error) {
bts, err := msgpack.Marshal(t)
return len(bts), err
}
}
func parcoCompilerFactory(t TestStruct) compileFuncType {
compiler := newCompiler(len(t.Arr))
buf := bytes.NewBuffer(nil)
return func(ts TestStruct) (int, error) {
defer buf.Reset()
err := compiler.Compile(t, buf)
return buf.Len(), err
}
}
func parcoDiscardCompilerFactory(t TestStruct) compileFuncType {
compiler := newCompiler(len(t.Arr))
w := new(discard)
return func(ts TestStruct) (int, error) {
defer w.Reset()
err := compiler.Compile(t, w)
return w.Size(), err
}
}
func benchmarkCompile(b *testing.B, tests []TestStruct, compileFuncFactory compileFuncFactory) {
for _, test := range tests {
// creating Compiler needs different field types as per different test payloads
compileFunc := compileFuncFactory(test)
b.Run(test.Name, func(b *testing.B) {
var totalBytes int
for i := 0; i < b.N; i++ {
n, err := compileFunc(test)
if err != nil {
b.Error(err)
}
totalBytes += n
}
b.ReportMetric(float64(totalBytes/b.N), "payload_bytes/op")
})
}
}
func BenchmarkParcoAlloc_Compile(b *testing.B) {
benchmarkCompile(b, tests, parcoCompilerFactory)
}
func BenchmarkParcoDiscard_Compile(b *testing.B) {
benchmarkCompile(b, tests, parcoDiscardCompilerFactory)
}
func BenchmarkJson_Compile(b *testing.B) {
benchmarkCompile(b, tests, jsonCompilerFactory)
}
func BenchmarkMsgpack_Compile(b *testing.B) {
benchmarkCompile(b, tests, msgPackCompilerFactory)
}