This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
encoding_test.go
208 lines (180 loc) · 4.59 KB
/
encoding_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
package encoding
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
// Results:
//
// encoding format operation ns/op
// json int encode 187
// json float64 encode 270
// json string encode 192
// json int decode 274
// json float64 decode 356
// json string decode 322
// literal int encode 75.3
// literal float64 encode 187
// literal string encode 45.7
// literal int decode 105
// literal float64 decode 124
// literal string decode 92.8
// gob int encode 1004
// gob float64 encode 1034
// gob string encode 1000
// gob int decode 480
// gob float64 decode 486
// gob string decode 491
//
// Average of operations and types:
// encoding ns/op
// literal 105
// json 267
// gob 749
//
func BenchmarkEncoding(b *testing.B) {
benchmarks := map[string]ValueEncoding{
"gob": NewValueEncoding(GobEncoding),
"json": JSONEncoding,
"literal": NewLiteralEncoding(nil),
}
for name, encoding := range benchmarks {
b.Run(name, func(b *testing.B) {
b.Run("int", func(b *testing.B) {
b.Run("encode", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = encoding.Encode(123)
}
})
b.Run("decode", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var val int
_ = encoding.Decode([]byte("123"), &val)
}
})
})
b.Run("float64", func(b *testing.B) {
b.Run("encode", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = encoding.Encode(12.3)
}
})
b.Run("decode", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var val float64
_ = encoding.Decode([]byte("12.3"), &val)
}
})
})
b.Run("string", func(b *testing.B) {
b.Run("encode", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = encoding.Encode("123")
}
})
b.Run("decode", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var val string
_ = encoding.Decode([]byte("123"), &val)
}
})
})
})
}
}
type testStruct struct {
Foo string
}
func testArbitraryEncoding(t *testing.T, m ValueEncoding) {
t.Run("empty struct", func(t *testing.T) {
item := struct{}{}
enc, err := m.Encode(item)
require.NoError(t, err)
require.NotNil(t, enc)
var dec struct{}
err = m.Decode(enc, &dec)
require.NoError(t, err)
require.EqualValues(t, item, dec)
})
t.Run("test struct", func(t *testing.T) {
item := testStruct{Foo: "bar"}
enc, err := m.Encode(item)
require.NoError(t, err)
require.NotNil(t, enc)
var dec testStruct
err = m.Decode(enc, &dec)
require.NoError(t, err)
require.EqualValues(t, item, dec)
})
t.Run("time struct", func(t *testing.T) {
item, err := time.Parse(time.RFC1123Z, "Mon, 02 Jan 2006 15:04:05 -0700")
require.NoError(t, err)
enc, err := m.Encode(item)
require.NoError(t, err)
require.NotNil(t, enc)
var dec time.Time
err = m.Decode(enc, &dec)
require.NoError(t, err)
require.EqualValues(t, item, dec)
})
t.Run("nil", func(t *testing.T) {
var item interface{}
enc, err := m.Encode(&item)
require.NoError(t, err)
require.NotNil(t, enc)
var dec interface{}
err = m.Decode(enc, &dec)
require.NoError(t, err)
require.EqualValues(t, item, dec)
})
}
func testBasicEncoding(t *testing.T, m ValueEncoding) {
t.Run("integer", func(t *testing.T) {
item := 123
enc, err := m.Encode(item)
require.NoError(t, err)
require.NotNil(t, enc)
var dec int
err = m.Decode(enc, &dec)
require.NoError(t, err)
require.EqualValues(t, item, dec)
})
t.Run("float", func(t *testing.T) {
item := 1.23
enc, err := m.Encode(item)
require.NoError(t, err)
require.NotNil(t, enc)
var dec float64
err = m.Decode(enc, &dec)
require.NoError(t, err)
require.EqualValues(t, item, dec)
})
t.Run("string", func(t *testing.T) {
item := "123"
enc, err := m.Encode(item)
require.NoError(t, err)
require.NotNil(t, enc)
var dec string
err = m.Decode(enc, &dec)
require.NoError(t, err)
require.EqualValues(t, item, dec)
})
t.Run("not a pointer", func(t *testing.T) {
enc, err := m.Encode("123")
require.NoError(t, err)
require.NotNil(t, enc)
var dec string
err = m.Decode(enc, dec)
require.EqualError(t, err, "argument must be a pointer")
require.Zero(t, dec)
})
t.Run("wrong type", func(t *testing.T) {
enc, err := m.Encode("12.3")
require.NoError(t, err)
require.NotNil(t, enc)
var dec int
err = m.Decode(enc, &dec)
require.Error(t, err)
require.Zero(t, dec)
})
}