Debian Codegraph @

benchmarks/decode_test.go

raw
1 package benchmark
2
3 import (
4 "bytes"
5 "encoding/json"
6 "testing"
7
8 gojay "github.com/francoispqt/gojay"
9 gojson "github.com/goccy/go-json"
10 jsoniter "github.com/json-iterator/go"
11 segmentiojson "github.com/segmentio/encoding/json"
12 fastjson "github.com/valyala/fastjson"
13 )
14
15 func Benchmark_Decode_SmallStruct_Unmarshal_EncodingJson(b *testing.B) {
16 b.ReportAllocs()
17 for n := 0; n < b.N; n++ {
18 result := SmallPayload{}
19 if err := json.Unmarshal(SmallFixture, &result); err != nil {
20 b.Fatal(err)
21 }
22 }
23 }
24
25 func Benchmark_Decode_SmallStruct_Unmarshal_FastJson(b *testing.B) {
26 smallFixture := string(SmallFixture)
27 b.ReportAllocs()
28 for n := 0; n < b.N; n++ {
29 var p fastjson.Parser
30 if _, err := p.Parse(smallFixture); err != nil {
31 b.Fatal(err)
32 }
33 }
34 }
35
36 func Benchmark_Decode_SmallStruct_Unmarshal_SegmentioJson(b *testing.B) {
37 b.ReportAllocs()
38 for n := 0; n < b.N; n++ {
39 result := SmallPayload{}
40 if err := segmentiojson.Unmarshal(SmallFixture, &result); err != nil {
41 b.Fatal(err)
42 }
43 }
44 }
45
46 func Benchmark_Decode_SmallStruct_Unmarshal_JsonIter(b *testing.B) {
47 b.ReportAllocs()
48 for n := 0; n < b.N; n++ {
49 result := SmallPayload{}
50 if err := jsoniter.Unmarshal(SmallFixture, &result); err != nil {
51 b.Fatal(err)
52 }
53 }
54 }
55
56 func Benchmark_Decode_SmallStruct_Unmarshal_GoJay(b *testing.B) {
57 b.ReportAllocs()
58 for n := 0; n < b.N; n++ {
59 result := SmallPayload{}
60 if err := gojay.UnmarshalJSONObject(SmallFixture, &result); err != nil {
61 b.Fatal(err)
62 }
63 }
64 }
65
66 func Benchmark_Decode_SmallStruct_Unmarshal_GoJayUnsafe(b *testing.B) {
67 b.ReportAllocs()
68 for i := 0; i < b.N; i++ {
69 result := SmallPayload{}
70 if err := gojay.Unsafe.UnmarshalJSONObject(SmallFixture, &result); err != nil {
71 b.Fatal(err)
72 }
73 }
74 }
75
76 func Benchmark_Decode_SmallStruct_Unmarshal_GoJson(b *testing.B) {
77 b.ReportAllocs()
78 for i := 0; i < b.N; i++ {
79 result := SmallPayload{}
80 if err := gojson.Unmarshal(SmallFixture, &result); err != nil {
81 b.Fatal(err)
82 }
83 }
84 }
85
86 func Benchmark_Decode_SmallStruct_Unmarshal_GoJsonUnmarshalOf(b *testing.B) {
87 b.ReportAllocs()
88 for i := 0; i < b.N; i++ {
89 result := SmallPayload{}
90 if err := gojson.UnmarshalOf(SmallFixture, &result); err != nil {
91 b.Fatal(err)
92 }
93 }
94 }
95
96 func Benchmark_Decode_SmallStruct_Unmarshal_GoJsonUnmarshalOfNoCopyString(b *testing.B) {
97 b.ReportAllocs()
98 for i := 0; i < b.N; i++ {
99 result := SmallPayload{}
100 if err := gojson.UnmarshalOf(SmallFixture, &result, gojson.DecodeNoCopyString()); err != nil {
101 b.Fatal(err)
102 }
103 }
104 }
105
106 func Benchmark_Decode_SmallStruct_Stream_EncodingJson(b *testing.B) {
107 b.ReportAllocs()
108 reader := bytes.NewReader(SmallFixture)
109 for i := 0; i < b.N; i++ {
110 result := SmallPayload{}
111 reader.Reset(SmallFixture)
112 if err := json.NewDecoder(reader).Decode(&result); err != nil {
113 b.Fatal(err)
114 }
115 }
116 }
117
118 func Benchmark_Decode_SmallStruct_Stream_SegmentioJson(b *testing.B) {
119 b.ReportAllocs()
120 reader := bytes.NewReader(SmallFixture)
121 for i := 0; i < b.N; i++ {
122 result := SmallPayload{}
123 reader.Reset(SmallFixture)
124 if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil {
125 b.Fatal(err)
126 }
127 }
128 }
129
130 func Benchmark_Decode_SmallStruct_Stream_JsonIter(b *testing.B) {
131 b.ReportAllocs()
132 reader := bytes.NewReader(SmallFixture)
133 for i := 0; i < b.N; i++ {
134 result := SmallPayload{}
135 reader.Reset(SmallFixture)
136 if err := jsoniter.NewDecoder(reader).Decode(&result); err != nil {
137 b.Fatal(err)
138 }
139 }
140 }
141
142 func Benchmark_Decode_SmallStruct_Stream_GoJay(b *testing.B) {
143 b.ReportAllocs()
144 reader := bytes.NewReader(SmallFixture)
145 for n := 0; n < b.N; n++ {
146 reader.Reset(SmallFixture)
147 result := SmallPayload{}
148 if err := gojay.NewDecoder(reader).DecodeObject(&result); err != nil {
149 b.Fatal(err)
150 }
151 }
152 }
153
154 func Benchmark_Decode_SmallStruct_Stream_GoJson(b *testing.B) {
155 b.ReportAllocs()
156 reader := bytes.NewReader(SmallFixture)
157 for i := 0; i < b.N; i++ {
158 result := SmallPayload{}
159 reader.Reset(SmallFixture)
160 if err := gojson.NewDecoder(reader).Decode(&result); err != nil {
161 b.Fatal(err)
162 }
163 }
164 }
165
166 func Benchmark_Decode_MediumStruct_Unmarshal_EncodingJson(b *testing.B) {
167 b.ReportAllocs()
168 for n := 0; n < b.N; n++ {
169 result := MediumPayload{}
170 if err := json.Unmarshal(MediumFixture, &result); err != nil {
171 b.Fatal(err)
172 }
173 }
174 }
175
176 func Benchmark_Decode_MediumStruct_Unmarshal_FastJson(b *testing.B) {
177 mediumFixture := string(MediumFixture)
178 b.ReportAllocs()
179 for n := 0; n < b.N; n++ {
180 var p fastjson.Parser
181 if _, err := p.Parse(mediumFixture); err != nil {
182 b.Fatal(err)
183 }
184 }
185 }
186
187 func Benchmark_Decode_MediumStruct_Unmarshal_SegmentioJson(b *testing.B) {
188 b.ReportAllocs()
189 for i := 0; i < b.N; i++ {
190 result := MediumPayload{}
191 if err := segmentiojson.Unmarshal(MediumFixture, &result); err != nil {
192 b.Fatal(err)
193 }
194 }
195 }
196
197 func Benchmark_Decode_MediumStruct_Unmarshal_JsonIter(b *testing.B) {
198 b.ReportAllocs()
199 for n := 0; n < b.N; n++ {
200 result := MediumPayload{}
201 if err := jsoniter.Unmarshal(MediumFixture, &result); err != nil {
202 b.Fatal(err)
203 }
204 }
205 }
206
207 func Benchmark_Decode_MediumStruct_Unmarshal_GoJay(b *testing.B) {
208 b.ReportAllocs()
209 for n := 0; n < b.N; n++ {
210 result := MediumPayload{}
211 if err := gojay.UnmarshalJSONObject(MediumFixture, &result); err != nil {
212 b.Fatal(err)
213 }
214 }
215 }
216
217 func Benchmark_Decode_MediumStruct_Unmarshal_GoJayUnsafe(b *testing.B) {
218 b.ReportAllocs()
219 for i := 0; i < b.N; i++ {
220 result := MediumPayload{}
221 if err := gojay.Unsafe.UnmarshalJSONObject(MediumFixture, &result); err != nil {
222 b.Fatal(err)
223 }
224 }
225 }
226
227 func Benchmark_Decode_MediumStruct_Unmarshal_GoJson(b *testing.B) {
228 b.ReportAllocs()
229 for i := 0; i < b.N; i++ {
230 result := MediumPayload{}
231 if err := gojson.Unmarshal(MediumFixture, &result); err != nil {
232 b.Fatal(err)
233 }
234 }
235 }
236
237 func Benchmark_Decode_MediumStruct_Unmarshal_GoJsonUnmarshalOf(b *testing.B) {
238 b.ReportAllocs()
239 for i := 0; i < b.N; i++ {
240 result := MediumPayload{}
241 if err := gojson.UnmarshalOf(MediumFixture, &result); err != nil {
242 b.Fatal(err)
243 }
244 }
245 }
246
247 func Benchmark_Decode_MediumStruct_Unmarshal_GoJsonUnmarshalOfNoCopyString(b *testing.B) {
248 b.ReportAllocs()
249 for i := 0; i < b.N; i++ {
250 result := MediumPayload{}
251 if err := gojson.UnmarshalOf(MediumFixture, &result, gojson.DecodeNoCopyString()); err != nil {
252 b.Fatal(err)
253 }
254 }
255 }
256
257 func Benchmark_Decode_MediumStruct_Stream_EncodingJson(b *testing.B) {
258 b.ReportAllocs()
259 reader := bytes.NewReader(MediumFixture)
260 for i := 0; i < b.N; i++ {
261 result := MediumPayload{}
262 reader.Reset(MediumFixture)
263 if err := json.NewDecoder(reader).Decode(&result); err != nil {
264 b.Fatal(err)
265 }
266 }
267 }
268
269 func Benchmark_Decode_MediumStruct_Stream_SegmentioJson(b *testing.B) {
270 b.ReportAllocs()
271 reader := bytes.NewReader(MediumFixture)
272 for n := 0; n < b.N; n++ {
273 reader.Reset(MediumFixture)
274 result := MediumPayload{}
275 if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil {
276 b.Fatal(err)
277 }
278 }
279 }
280
281 func Benchmark_Decode_MediumStruct_Stream_JsonIter(b *testing.B) {
282 b.ReportAllocs()
283 reader := bytes.NewReader(MediumFixture)
284 for i := 0; i < b.N; i++ {
285 result := MediumPayload{}
286 reader.Reset(MediumFixture)
287 if err := jsoniter.NewDecoder(reader).Decode(&result); err != nil {
288 b.Fatal(err)
289 }
290 }
291 }
292
293 func Benchmark_Decode_MediumStruct_Stream_GoJay(b *testing.B) {
294 b.ReportAllocs()
295 reader := bytes.NewReader(MediumFixture)
296 for n := 0; n < b.N; n++ {
297 reader.Reset(MediumFixture)
298 result := MediumPayload{}
299 if err := gojay.NewDecoder(reader).DecodeObject(&result); err != nil {
300 b.Fatal(err)
301 }
302 }
303 }
304
305 func Benchmark_Decode_MediumStruct_Stream_GoJson(b *testing.B) {
306 b.ReportAllocs()
307 reader := bytes.NewReader(MediumFixture)
308 for i := 0; i < b.N; i++ {
309 result := MediumPayload{}
310 reader.Reset(MediumFixture)
311 if err := gojson.NewDecoder(reader).Decode(&result); err != nil {
312 b.Fatal(err)
313 }
314 }
315 }
316
317 func Benchmark_Decode_LargeStruct_Unmarshal_EncodingJson(b *testing.B) {
318 b.ReportAllocs()
319 for n := 0; n < b.N; n++ {
320 result := LargePayload{}
321 if err := json.Unmarshal(LargeFixture, &result); err != nil {
322 b.Fatal(err)
323 }
324 }
325 }
326
327 func Benchmark_Decode_LargeStruct_Unmarshal_FastJson(b *testing.B) {
328 largeFixture := string(LargeFixture)
329 b.ReportAllocs()
330 for n := 0; n < b.N; n++ {
331 var p fastjson.Parser
332 if _, err := p.Parse(largeFixture); err != nil {
333 b.Fatal(err)
334 }
335 }
336 }
337
338 func Benchmark_Decode_LargeStruct_Unmarshal_SegmentioJson(b *testing.B) {
339 b.ReportAllocs()
340 for i := 0; i < b.N; i++ {
341 result := LargePayload{}
342 if err := segmentiojson.Unmarshal(LargeFixture, &result); err != nil {
343 b.Fatal(err)
344 }
345 }
346 }
347
348 func Benchmark_Decode_LargeStruct_Unmarshal_JsonIter(b *testing.B) {
349 b.ReportAllocs()
350 for n := 0; n < b.N; n++ {
351 result := LargePayload{}
352 if err := jsoniter.Unmarshal(LargeFixture, &result); err != nil {
353 b.Fatal(err)
354 }
355 }
356 }
357
358 func Benchmark_Decode_LargeStruct_Unmarshal_GoJay(b *testing.B) {
359 b.ReportAllocs()
360 for n := 0; n < b.N; n++ {
361 result := LargePayload{}
362 if err := gojay.UnmarshalJSONObject(LargeFixture, &result); err != nil {
363 b.Fatal(err)
364 }
365 }
366 }
367
368 func Benchmark_Decode_LargeStruct_Unmarshal_GoJayUnsafe(b *testing.B) {
369 b.ReportAllocs()
370 for i := 0; i < b.N; i++ {
371 result := LargePayload{}
372 if err := gojay.Unsafe.UnmarshalJSONObject(LargeFixture, &result); err != nil {
373 b.Fatal(err)
374 }
375 }
376 }
377
378 func Benchmark_Decode_LargeStruct_Unmarshal_GoJson(b *testing.B) {
379 b.ReportAllocs()
380 for i := 0; i < b.N; i++ {
381 result := LargePayload{}
382 if err := gojson.Unmarshal(LargeFixture, &result); err != nil {
383 b.Fatal(err)
384 }
385 }
386 }
387
388 func Benchmark_Decode_LargeStruct_Unmarshal_GoJsonUnmarshalOf(b *testing.B) {
389 b.ReportAllocs()
390 for i := 0; i < b.N; i++ {
391 result := LargePayload{}
392 if err := gojson.UnmarshalOf(LargeFixture, &result); err != nil {
393 b.Fatal(err)
394 }
395 }
396 }
397
398 func Benchmark_Decode_LargeStruct_Unmarshal_GoJsonUnmarshalOfNoCopyString(b *testing.B) {
399 b.ReportAllocs()
400 for i := 0; i < b.N; i++ {
401 result := LargePayload{}
402 if err := gojson.UnmarshalOf(LargeFixture, &result, gojson.DecodeNoCopyString()); err != nil {
403 b.Fatal(err)
404 }
405 }
406 }
407
408 func Benchmark_Decode_LargeStruct_Unmarshal_GoJsonFirstWinMode(b *testing.B) {
409 b.ReportAllocs()
410 for i := 0; i < b.N; i++ {
411 result := LargePayload{}
412 if err := gojson.UnmarshalWithOption(
413 LargeFixture,
414 &result,
415 gojson.DecodeFieldPriorityFirstWin(),
416 ); err != nil {
417 b.Fatal(err)
418 }
419 }
420 }
421
422 func Benchmark_Decode_LargeStruct_Unmarshal_GoJsonUnmarshalOfFirstWinMode(b *testing.B) {
423 b.ReportAllocs()
424 for i := 0; i < b.N; i++ {
425 result := LargePayload{}
426 if err := gojson.UnmarshalOf(
427 LargeFixture,
428 &result,
429 gojson.DecodeFieldPriorityFirstWin(),
430 ); err != nil {
431 b.Fatal(err)
432 }
433 }
434 }
435
436 func Benchmark_Decode_LargeStruct_Stream_EncodingJson(b *testing.B) {
437 b.ReportAllocs()
438 reader := bytes.NewReader(LargeFixture)
439 for i := 0; i < b.N; i++ {
440 result := LargePayload{}
441 reader.Reset(LargeFixture)
442 if err := json.NewDecoder(reader).Decode(&result); err != nil {
443 b.Fatal(err)
444 }
445 }
446 }
447
448 func Benchmark_Decode_LargeStruct_Stream_SegmentioJson(b *testing.B) {
449 b.ReportAllocs()
450 reader := bytes.NewReader(LargeFixture)
451 for i := 0; i < b.N; i++ {
452 result := LargePayload{}
453 reader.Reset(LargeFixture)
454 if err := segmentiojson.NewDecoder(reader).Decode(&result); err != nil {
455 b.Fatal(err)
456 }
457 }
458 }
459
460 func Benchmark_Decode_LargeStruct_Stream_JsonIter(b *testing.B) {
461 b.ReportAllocs()
462 reader := bytes.NewReader(LargeFixture)
463 for i := 0; i < b.N; i++ {
464 result := LargePayload{}
465 reader.Reset(LargeFixture)
466 if err := jsoniter.NewDecoder(reader).Decode(&result); err != nil {
467 b.Fatal(err)
468 }
469 }
470 }
471
472 func Benchmark_Decode_LargeStruct_Stream_GoJay(b *testing.B) {
473 b.ReportAllocs()
474 reader := bytes.NewReader(LargeFixture)
475 for n := 0; n < b.N; n++ {
476 reader.Reset(LargeFixture)
477 result := LargePayload{}
478 if err := gojay.NewDecoder(reader).DecodeObject(&result); err != nil {
479 b.Fatal(err)
480 }
481 }
482 }
483
484 func Benchmark_Decode_LargeStruct_Stream_GoJson(b *testing.B) {
485 b.ReportAllocs()
486 reader := bytes.NewReader(LargeFixture)
487 for i := 0; i < b.N; i++ {
488 result := LargePayload{}
489 reader.Reset(LargeFixture)
490 if err := gojson.NewDecoder(reader).Decode(&result); err != nil {
491 b.Fatal(err)
492 }
493 }
494 }
495
496 func Benchmark_Decode_LargeStruct_Stream_GoJsonFirstWinMode(b *testing.B) {
497 b.ReportAllocs()
498 reader := bytes.NewReader(LargeFixture)
499 for i := 0; i < b.N; i++ {
500 result := LargePayload{}
501 reader.Reset(LargeFixture)
502 if err := gojson.NewDecoder(reader).DecodeWithOption(
503 &result,
504 gojson.DecodeFieldPriorityFirstWin(),
505 ); err != nil {
506 b.Fatal(err)
507 }
508 }
509 }
510
511 func Benchmark_Decode_LargeSlice_EscapedString_GoJson(b *testing.B) {
512 b.ReportAllocs()
513 for i := 0; i < b.N; i++ {
514 var v []string
515 if err := gojson.Unmarshal(LargeSliceEscapedString, &v); err != nil {
516 b.Fatal(err)
517 }
518 }
519 }