Debian Codegraph @

benchmarks/sonic_test.go

raw
1 package benchmark
2
3 import (
4 "bytes"
5 "io"
6 "os"
7 "reflect"
8 "strconv"
9 "sync"
10 "testing"
11
12 "github.com/bytedance/sonic"
13 "github.com/bytedance/sonic/option"
14 gojson "github.com/goccy/go-json"
15 )
16
17 // sonic is measured in the way which is the best for it, so that the comparison is fair:
18 //
19 // - Every type is compiled before the first benchmark of sonic by sonic.Pretouch ( see pretouchSonic ). SONIC_MAX_INLINE_DEPTH sets how
20 // deep the nested structs are inlined into the code of a type ( the default of sonic is 3 ).
21 // - Sonic is sonic.ConfigDefault, which doesn't escape HTML, doesn't normalize UTF-8 and doesn't sort the keys
22 // of a map. GoJsonLikeSonic is go-json with the options which do the same.
23 // - SonicFastest is sonic.ConfigFastest, which also skips the validation of what a marshaler returns.
24 // - SonicStd is sonic.ConfigStd, which does what encoding/json and go-json do by default.
25 //
26 // sonicTypes are the types of the benchmarks, which sonic compiles before its first benchmark: the files of the
27 // benchmarks add theirs in their init.
28 var sonicTypes = []reflect.Type{
29 reflect.TypeOf(NewSmallPayload()),
30 reflect.TypeOf(NewMediumPayload()),
31 reflect.TypeOf(NewLargePayload()),
32 reflect.TypeOf(benchMapValue()),
33 reflect.TypeOf([]any{}),
34 reflect.TypeOf(&codeStruct),
35 }
36
37 var sonicPretouched sync.Once
38
39 // pretouchSonic compiles every type of sonicTypes by sonic.Pretouch, once, before the first benchmark of sonic
40 // in the process. It is not done in init: a process which runs only the benchmarks of go-json, as the ones of
41 // benchcheck do, would spend the time of the compilation at every start.
42 func pretouchSonic() {
43 sonicPretouched.Do(func() {
44 opts := []option.CompileOption{option.WithCompileRecursiveDepth(10)}
45 if depth, err := strconv.Atoi(os.Getenv("SONIC_MAX_INLINE_DEPTH")); err == nil {
46 opts = append(opts, option.WithCompileMaxInlineDepth(depth))
47 }
48 if err := sonic.PretouchMany(sonicTypes, opts...); err != nil {
49 panic(err)
50 }
51 })
52 }
53
54 func Benchmark_Encode_SmallStruct_Sonic(b *testing.B) {
55 pretouchSonic()
56 b.ReportAllocs()
57 for i := 0; i < b.N; i++ {
58 if _, err := sonic.ConfigDefault.Marshal(NewSmallPayload()); err != nil {
59 b.Fatal(err)
60 }
61 }
62 }
63
64 func Benchmark_Encode_SmallStruct_SonicFastest(b *testing.B) {
65 pretouchSonic()
66 b.ReportAllocs()
67 for i := 0; i < b.N; i++ {
68 if _, err := sonic.ConfigFastest.Marshal(NewSmallPayload()); err != nil {
69 b.Fatal(err)
70 }
71 }
72 }
73
74 func Benchmark_Encode_SmallStruct_SonicStd(b *testing.B) {
75 pretouchSonic()
76 b.ReportAllocs()
77 for i := 0; i < b.N; i++ {
78 if _, err := sonic.ConfigStd.Marshal(NewSmallPayload()); err != nil {
79 b.Fatal(err)
80 }
81 }
82 }
83
84 func Benchmark_Encode_SmallStructCached_Sonic(b *testing.B) {
85 pretouchSonic()
86 cached := NewSmallPayload()
87 b.ReportAllocs()
88 for i := 0; i < b.N; i++ {
89 if _, err := sonic.ConfigDefault.Marshal(cached); err != nil {
90 b.Fatal(err)
91 }
92 }
93 }
94
95 func Benchmark_Encode_SmallStructCached_SonicFastest(b *testing.B) {
96 pretouchSonic()
97 cached := NewSmallPayload()
98 b.ReportAllocs()
99 for i := 0; i < b.N; i++ {
100 if _, err := sonic.ConfigFastest.Marshal(cached); err != nil {
101 b.Fatal(err)
102 }
103 }
104 }
105
106 func Benchmark_Encode_SmallStructCached_SonicStd(b *testing.B) {
107 pretouchSonic()
108 cached := NewSmallPayload()
109 b.ReportAllocs()
110 for i := 0; i < b.N; i++ {
111 if _, err := sonic.ConfigStd.Marshal(cached); err != nil {
112 b.Fatal(err)
113 }
114 }
115 }
116
117 func Benchmark_Encode_MediumStruct_Sonic(b *testing.B) {
118 pretouchSonic()
119 b.ReportAllocs()
120 for i := 0; i < b.N; i++ {
121 if _, err := sonic.ConfigDefault.Marshal(NewMediumPayload()); err != nil {
122 b.Fatal(err)
123 }
124 }
125 }
126
127 func Benchmark_Encode_MediumStruct_SonicFastest(b *testing.B) {
128 pretouchSonic()
129 b.ReportAllocs()
130 for i := 0; i < b.N; i++ {
131 if _, err := sonic.ConfigFastest.Marshal(NewMediumPayload()); err != nil {
132 b.Fatal(err)
133 }
134 }
135 }
136
137 func Benchmark_Encode_MediumStruct_SonicStd(b *testing.B) {
138 pretouchSonic()
139 b.ReportAllocs()
140 for i := 0; i < b.N; i++ {
141 if _, err := sonic.ConfigStd.Marshal(NewMediumPayload()); err != nil {
142 b.Fatal(err)
143 }
144 }
145 }
146
147 func Benchmark_Encode_MediumStructCached_Sonic(b *testing.B) {
148 pretouchSonic()
149 cached := NewMediumPayload()
150 b.ReportAllocs()
151 for i := 0; i < b.N; i++ {
152 if _, err := sonic.ConfigDefault.Marshal(cached); err != nil {
153 b.Fatal(err)
154 }
155 }
156 }
157
158 func Benchmark_Encode_MediumStructCached_SonicFastest(b *testing.B) {
159 pretouchSonic()
160 cached := NewMediumPayload()
161 b.ReportAllocs()
162 for i := 0; i < b.N; i++ {
163 if _, err := sonic.ConfigFastest.Marshal(cached); err != nil {
164 b.Fatal(err)
165 }
166 }
167 }
168
169 func Benchmark_Encode_MediumStructCached_SonicStd(b *testing.B) {
170 pretouchSonic()
171 cached := NewMediumPayload()
172 b.ReportAllocs()
173 for i := 0; i < b.N; i++ {
174 if _, err := sonic.ConfigStd.Marshal(cached); err != nil {
175 b.Fatal(err)
176 }
177 }
178 }
179
180 func Benchmark_Encode_LargeStruct_Sonic(b *testing.B) {
181 pretouchSonic()
182 b.ReportAllocs()
183 for i := 0; i < b.N; i++ {
184 if _, err := sonic.ConfigDefault.Marshal(NewLargePayload()); err != nil {
185 b.Fatal(err)
186 }
187 }
188 }
189
190 func Benchmark_Encode_LargeStruct_SonicFastest(b *testing.B) {
191 pretouchSonic()
192 b.ReportAllocs()
193 for i := 0; i < b.N; i++ {
194 if _, err := sonic.ConfigFastest.Marshal(NewLargePayload()); err != nil {
195 b.Fatal(err)
196 }
197 }
198 }
199
200 func Benchmark_Encode_LargeStruct_SonicStd(b *testing.B) {
201 pretouchSonic()
202 b.ReportAllocs()
203 for i := 0; i < b.N; i++ {
204 if _, err := sonic.ConfigStd.Marshal(NewLargePayload()); err != nil {
205 b.Fatal(err)
206 }
207 }
208 }
209
210 func Benchmark_Encode_LargeStructCached_Sonic(b *testing.B) {
211 pretouchSonic()
212 cached := NewLargePayload()
213 b.ReportAllocs()
214 for i := 0; i < b.N; i++ {
215 if _, err := sonic.ConfigDefault.Marshal(cached); err != nil {
216 b.Fatal(err)
217 }
218 }
219 }
220
221 func Benchmark_Encode_LargeStructCached_SonicFastest(b *testing.B) {
222 pretouchSonic()
223 cached := NewLargePayload()
224 b.ReportAllocs()
225 for i := 0; i < b.N; i++ {
226 if _, err := sonic.ConfigFastest.Marshal(cached); err != nil {
227 b.Fatal(err)
228 }
229 }
230 }
231
232 func Benchmark_Encode_LargeStructCached_SonicStd(b *testing.B) {
233 pretouchSonic()
234 cached := NewLargePayload()
235 b.ReportAllocs()
236 for i := 0; i < b.N; i++ {
237 if _, err := sonic.ConfigStd.Marshal(cached); err != nil {
238 b.Fatal(err)
239 }
240 }
241 }
242
243 func Benchmark_Encode_MapInterface_Sonic(b *testing.B) {
244 pretouchSonic()
245 v := benchMapValue()
246 b.ReportAllocs()
247 for i := 0; i < b.N; i++ {
248 if _, err := sonic.ConfigDefault.Marshal(v); err != nil {
249 b.Fatal(err)
250 }
251 }
252 }
253
254 func Benchmark_Encode_MapInterface_SonicFastest(b *testing.B) {
255 pretouchSonic()
256 v := benchMapValue()
257 b.ReportAllocs()
258 for i := 0; i < b.N; i++ {
259 if _, err := sonic.ConfigFastest.Marshal(v); err != nil {
260 b.Fatal(err)
261 }
262 }
263 }
264
265 func Benchmark_Encode_MapInterface_SonicStd(b *testing.B) {
266 pretouchSonic()
267 v := benchMapValue()
268 b.ReportAllocs()
269 for i := 0; i < b.N; i++ {
270 if _, err := sonic.ConfigStd.Marshal(v); err != nil {
271 b.Fatal(err)
272 }
273 }
274 }
275
276 func Benchmark_Encode_Interface_Sonic(b *testing.B) {
277 pretouchSonic()
278 v := []any{1}
279 b.ReportAllocs()
280 for i := 0; i < b.N; i++ {
281 if _, err := sonic.ConfigDefault.Marshal(v); err != nil {
282 b.Fatal(err)
283 }
284 }
285 }
286
287 func Benchmark_Encode_Interface_SonicFastest(b *testing.B) {
288 pretouchSonic()
289 v := []any{1}
290 b.ReportAllocs()
291 for i := 0; i < b.N; i++ {
292 if _, err := sonic.ConfigFastest.Marshal(v); err != nil {
293 b.Fatal(err)
294 }
295 }
296 }
297
298 func Benchmark_Encode_Interface_SonicStd(b *testing.B) {
299 pretouchSonic()
300 v := []any{1}
301 b.ReportAllocs()
302 for i := 0; i < b.N; i++ {
303 if _, err := sonic.ConfigStd.Marshal(v); err != nil {
304 b.Fatal(err)
305 }
306 }
307 }
308
309 func Benchmark_Encode_Bool_Sonic(b *testing.B) {
310 pretouchSonic()
311 b.ReportAllocs()
312 var buf bytes.Buffer
313 enc := sonic.ConfigDefault.NewEncoder(&buf)
314 for i := 0; i < b.N; i++ {
315 if err := enc.Encode(true); err != nil {
316 b.Fatal(err)
317 }
318 }
319 }
320
321 func Benchmark_Encode_Bool_SonicFastest(b *testing.B) {
322 pretouchSonic()
323 b.ReportAllocs()
324 var buf bytes.Buffer
325 enc := sonic.ConfigFastest.NewEncoder(&buf)
326 for i := 0; i < b.N; i++ {
327 if err := enc.Encode(true); err != nil {
328 b.Fatal(err)
329 }
330 }
331 }
332
333 func Benchmark_Encode_Bool_SonicStd(b *testing.B) {
334 pretouchSonic()
335 b.ReportAllocs()
336 var buf bytes.Buffer
337 enc := sonic.ConfigStd.NewEncoder(&buf)
338 for i := 0; i < b.N; i++ {
339 if err := enc.Encode(true); err != nil {
340 b.Fatal(err)
341 }
342 }
343 }
344
345 func Benchmark_Marshal_Bool_Sonic(b *testing.B) {
346 pretouchSonic()
347 b.ReportAllocs()
348 for i := 0; i < b.N; i++ {
349 if _, err := sonic.ConfigDefault.Marshal(true); err != nil {
350 b.Fatal(err)
351 }
352 }
353 }
354
355 func Benchmark_Marshal_Bool_SonicFastest(b *testing.B) {
356 pretouchSonic()
357 b.ReportAllocs()
358 for i := 0; i < b.N; i++ {
359 if _, err := sonic.ConfigFastest.Marshal(true); err != nil {
360 b.Fatal(err)
361 }
362 }
363 }
364
365 func Benchmark_Marshal_Bool_SonicStd(b *testing.B) {
366 pretouchSonic()
367 b.ReportAllocs()
368 for i := 0; i < b.N; i++ {
369 if _, err := sonic.ConfigStd.Marshal(true); err != nil {
370 b.Fatal(err)
371 }
372 }
373 }
374
375 func Benchmark_Encode_Int_Sonic(b *testing.B) {
376 pretouchSonic()
377 b.ReportAllocs()
378 for i := 0; i < b.N; i++ {
379 if _, err := sonic.ConfigDefault.Marshal(1); err != nil {
380 b.Fatal(err)
381 }
382 }
383 }
384
385 func Benchmark_Encode_Int_SonicFastest(b *testing.B) {
386 pretouchSonic()
387 b.ReportAllocs()
388 for i := 0; i < b.N; i++ {
389 if _, err := sonic.ConfigFastest.Marshal(1); err != nil {
390 b.Fatal(err)
391 }
392 }
393 }
394
395 func Benchmark_Encode_Int_SonicStd(b *testing.B) {
396 pretouchSonic()
397 b.ReportAllocs()
398 for i := 0; i < b.N; i++ {
399 if _, err := sonic.ConfigStd.Marshal(1); err != nil {
400 b.Fatal(err)
401 }
402 }
403 }
404
405 func Benchmark_Encode_MarshalJSON_Sonic(b *testing.B) {
406 pretouchSonic()
407 v := &marshaler{}
408 b.ReportAllocs()
409 for i := 0; i < b.N; i++ {
410 if _, err := sonic.ConfigDefault.Marshal(v); err != nil {
411 b.Fatal(err)
412 }
413 }
414 }
415
416 func Benchmark_Encode_MarshalJSON_SonicFastest(b *testing.B) {
417 pretouchSonic()
418 v := &marshaler{}
419 b.ReportAllocs()
420 for i := 0; i < b.N; i++ {
421 if _, err := sonic.ConfigFastest.Marshal(v); err != nil {
422 b.Fatal(err)
423 }
424 }
425 }
426
427 func Benchmark_Encode_MarshalJSON_SonicStd(b *testing.B) {
428 pretouchSonic()
429 v := &marshaler{}
430 b.ReportAllocs()
431 for i := 0; i < b.N; i++ {
432 if _, err := sonic.ConfigStd.Marshal(v); err != nil {
433 b.Fatal(err)
434 }
435 }
436 }
437
438 func Benchmark_EncodeBigData_Sonic(b *testing.B) {
439 pretouchSonic()
440 b.ReportAllocs()
441 if codeJSON == nil {
442 b.StopTimer()
443 codeInit()
444 b.StartTimer()
445 }
446 b.RunParallel(func(pb *testing.PB) {
447 enc := sonic.ConfigDefault.NewEncoder(io.Discard)
448 for pb.Next() {
449 if err := enc.Encode(&codeStruct); err != nil {
450 b.Fatal("Encode:", err)
451 }
452 }
453 })
454 b.SetBytes(int64(len(codeJSON)))
455 }
456
457 func Benchmark_EncodeBigData_SonicFastest(b *testing.B) {
458 pretouchSonic()
459 b.ReportAllocs()
460 if codeJSON == nil {
461 b.StopTimer()
462 codeInit()
463 b.StartTimer()
464 }
465 b.RunParallel(func(pb *testing.PB) {
466 enc := sonic.ConfigFastest.NewEncoder(io.Discard)
467 for pb.Next() {
468 if err := enc.Encode(&codeStruct); err != nil {
469 b.Fatal("Encode:", err)
470 }
471 }
472 })
473 b.SetBytes(int64(len(codeJSON)))
474 }
475
476 func Benchmark_EncodeBigData_SonicStd(b *testing.B) {
477 pretouchSonic()
478 b.ReportAllocs()
479 if codeJSON == nil {
480 b.StopTimer()
481 codeInit()
482 b.StartTimer()
483 }
484 b.RunParallel(func(pb *testing.PB) {
485 enc := sonic.ConfigStd.NewEncoder(io.Discard)
486 for pb.Next() {
487 if err := enc.Encode(&codeStruct); err != nil {
488 b.Fatal("Encode:", err)
489 }
490 }
491 })
492 b.SetBytes(int64(len(codeJSON)))
493 }
494
495 func Benchmark_MarshalBigData_Sonic(b *testing.B) {
496 pretouchSonic()
497 b.ReportAllocs()
498 if codeJSON == nil {
499 b.StopTimer()
500 codeInit()
501 b.StartTimer()
502 }
503 b.RunParallel(func(pb *testing.PB) {
504 for pb.Next() {
505 if _, err := sonic.ConfigDefault.Marshal(&codeStruct); err != nil {
506 b.Fatal("Marshal:", err)
507 }
508 }
509 })
510 b.SetBytes(int64(len(codeJSON)))
511 }
512
513 func Benchmark_MarshalBigData_SonicFastest(b *testing.B) {
514 pretouchSonic()
515 b.ReportAllocs()
516 if codeJSON == nil {
517 b.StopTimer()
518 codeInit()
519 b.StartTimer()
520 }
521 b.RunParallel(func(pb *testing.PB) {
522 for pb.Next() {
523 if _, err := sonic.ConfigFastest.Marshal(&codeStruct); err != nil {
524 b.Fatal("Marshal:", err)
525 }
526 }
527 })
528 b.SetBytes(int64(len(codeJSON)))
529 }
530
531 func Benchmark_MarshalBigData_SonicStd(b *testing.B) {
532 pretouchSonic()
533 b.ReportAllocs()
534 if codeJSON == nil {
535 b.StopTimer()
536 codeInit()
537 b.StartTimer()
538 }
539 b.RunParallel(func(pb *testing.PB) {
540 for pb.Next() {
541 if _, err := sonic.ConfigStd.Marshal(&codeStruct); err != nil {
542 b.Fatal("Marshal:", err)
543 }
544 }
545 })
546 b.SetBytes(int64(len(codeJSON)))
547 }
548
549 // likeSonicOptions are the options of go-json which make it do what sonic.ConfigDefault does:
550 // the benchmarks of GoJsonLikeSonic are the ones to compare with the benchmarks of Sonic,
551 // as the ones of GoJson are the ones to compare with the benchmarks of SonicStd.
552 var likeSonicOptions = []gojson.EncodeOptionFunc{
553 gojson.DisableHTMLEscape(),
554 gojson.DisableNormalizeUTF8(),
555 gojson.UnorderedMap(),
556 }
557
558 func benchLikeSonic(b *testing.B, v any) {
559 b.ReportAllocs()
560 for i := 0; i < b.N; i++ {
561 if _, err := gojson.MarshalWithOption(v, likeSonicOptions...); err != nil {
562 b.Fatal(err)
563 }
564 }
565 }
566
567 func Benchmark_Encode_SmallStructCached_GoJsonLikeSonic(b *testing.B) {
568 benchLikeSonic(b, NewSmallPayload())
569 }
570
571 func Benchmark_Encode_MediumStructCached_GoJsonLikeSonic(b *testing.B) {
572 benchLikeSonic(b, NewMediumPayload())
573 }
574
575 func Benchmark_Encode_LargeStructCached_GoJsonLikeSonic(b *testing.B) {
576 benchLikeSonic(b, NewLargePayload())
577 }
578
579 func Benchmark_Encode_MapInterface_GoJsonLikeSonic(b *testing.B) {
580 benchLikeSonic(b, benchMapValue())
581 }