-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmarks_test.go
147 lines (112 loc) · 2.5 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package protoenc_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/siderolabs/protoenc"
)
//nolint:govet
type MyStruct struct {
A int32 `protobuf:"1"`
B int64 `protobuf:"2"`
C string `protobuf:"3"`
D bool `protobuf:"4"`
E float64 `protobuf:"5"`
}
var Store []byte
func BenchmarkEncode(b *testing.B) {
var s MyStruct
b.ReportAllocs()
for i := 0; i < b.N; i++ {
s = MyStruct{
A: int32(i),
B: int64(i),
C: "benchmark",
D: true,
E: float64(i),
}
result, err := protoenc.Marshal(&s)
if err != nil {
b.Fatal(err)
}
Store = result
}
}
func BenchmarkCustom(b *testing.B) {
b.Cleanup(func() {
protoenc.CleanEncoderDecoder()
})
o := Value[CustomEncoderStruct]{
V: CustomEncoderStruct{
Value: 150,
},
}
protoenc.RegisterEncoderDecoder(encodeCustomEncoderStruct, decodeCustomEncoderStruct)
encoded, err := protoenc.Marshal(&o)
require.NoError(b, err)
b.ResetTimer()
b.ReportAllocs()
target := &Value[CustomEncoderStruct]{}
for i := 0; i < b.N; i++ {
*target = Value[CustomEncoderStruct]{}
err := protoenc.Unmarshal(encoded, target)
if err != nil {
b.Fatal(err)
}
}
require.Equal(b, o.V.Value+2, target.V.Value)
}
func BenchmarkSlice(b *testing.B) {
type structWithSlice struct {
Field []int `protobuf:"1"`
}
type structType struct {
Field structWithSlice `protobuf:"1"`
}
o := structType{
Field: structWithSlice{
Field: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 1500, 1600},
},
}
encoded, err := protoenc.Marshal(&o)
require.NoError(b, err)
b.ResetTimer()
b.ReportAllocs()
target := &structType{}
for i := 0; i < b.N; i++ {
*target = structType{}
err := protoenc.Unmarshal(encoded, target)
if err != nil {
b.Fatal(err)
}
}
require.Equal(b, o, *target)
}
func BenchmarkString(b *testing.B) {
type structWithString struct {
Field string `protobuf:"1"`
}
type structType struct {
Field structWithString `protobuf:"1"`
}
o := structType{
Field: structWithString{
Field: "stuff to benchmark",
},
}
encoded, err := protoenc.Marshal(&o)
require.NoError(b, err)
b.ResetTimer()
b.ReportAllocs()
target := &structType{}
for i := 0; i < b.N; i++ {
*target = structType{}
err := protoenc.Unmarshal(encoded, target)
if err != nil {
b.Fatal(err)
}
}
require.Equal(b, o, *target)
}