1package benchmark 2 3import ( 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 "github.com/pquerna/ffjson/ffjson" 12 segmentiojson "github.com/segmentio/encoding/json" 13 "github.com/wI2L/jettison" 14) 15 16func Benchmark_Encode_SmallStruct_EncodingJson(b *testing.B) { 17 b.ReportAllocs() 18 for i := 0; i < b.N; i++ { 19 if _, err := json.Marshal(NewSmallPayload()); err != nil { 20 b.Fatal(err) 21 } 22 } 23} 24 25func Benchmark_Encode_SmallStruct_FFJson(b *testing.B) { 26 b.ReportAllocs() 27 for i := 0; i < b.N; i++ { 28 if _, err := ffjson.Marshal(NewSmallPayloadFFJson()); err != nil { 29 b.Fatal(err) 30 } 31 } 32} 33 34func Benchmark_Encode_SmallStruct_JsonIter(b *testing.B) { 35 var json = jsoniter.ConfigCompatibleWithStandardLibrary 36 b.ReportAllocs() 37 for i := 0; i < b.N; i++ { 38 if _, err := json.Marshal(NewSmallPayload()); err != nil { 39 b.Fatal(err) 40 } 41 } 42} 43 44func Benchmark_Encode_SmallStruct_EasyJson(b *testing.B) { 45 b.ReportAllocs() 46 for i := 0; i < b.N; i++ { 47 if _, err := NewSmallPayloadEasyJson().MarshalJSON(); err != nil { 48 b.Fatal(err) 49 } 50 } 51} 52 53func Benchmark_Encode_SmallStruct_Jettison(b *testing.B) { 54 b.ReportAllocs() 55 for i := 0; i < b.N; i++ { 56 if _, err := jettison.Marshal(NewSmallPayload()); err != nil { 57 b.Fatal(err) 58 } 59 } 60} 61 62func Benchmark_Encode_SmallStruct_GoJay(b *testing.B) { 63 b.ReportAllocs() 64 for i := 0; i < b.N; i++ { 65 if _, err := gojay.MarshalJSONObject(NewSmallPayload()); err != nil { 66 b.Fatal(err) 67 } 68 } 69} 70 71func Benchmark_Encode_SmallStruct_SegmentioJson(b *testing.B) { 72 b.ReportAllocs() 73 for i := 0; i < b.N; i++ { 74 if _, err := segmentiojson.Marshal(NewSmallPayload()); err != nil { 75 b.Fatal(err) 76 } 77 } 78} 79 80func Benchmark_Encode_SmallStruct_GoJsonColored(b *testing.B) { 81 colorOpt := gojson.Colorize(gojson.DefaultColorScheme) 82 b.ReportAllocs() 83 for i := 0; i < b.N; i++ { 84 if _, err := gojson.MarshalWithOption(NewSmallPayload(), colorOpt); err != nil { 85 b.Fatal(err) 86 } 87 } 88} 89 90func Benchmark_Encode_SmallStruct_GoJson(b *testing.B) { 91 b.ReportAllocs() 92 for i := 0; i < b.N; i++ { 93 if _, err := gojson.Marshal(NewSmallPayload()); err != nil { 94 b.Fatal(err) 95 } 96 } 97} 98 99func Benchmark_Encode_SmallStruct_GoJsonNoEscape(b *testing.B) { 100 b.ReportAllocs() 101 for i := 0; i < b.N; i++ { 102 if _, err := gojson.MarshalNoEscape(NewSmallPayload()); err != nil { 103 b.Fatal(err) 104 } 105 } 106} 107 108func Benchmark_Encode_SmallStructCached_EncodingJson(b *testing.B) { 109 cached := NewSmallPayload() 110 b.ReportAllocs() 111 for i := 0; i < b.N; i++ { 112 if _, err := json.Marshal(cached); err != nil { 113 b.Fatal(err) 114 } 115 } 116} 117 118func Benchmark_Encode_SmallStructCached_FFJson(b *testing.B) { 119 cached := NewSmallPayloadFFJson() 120 b.ReportAllocs() 121 for i := 0; i < b.N; i++ { 122 if _, err := ffjson.Marshal(cached); err != nil { 123 b.Fatal(err) 124 } 125 } 126} 127 128func Benchmark_Encode_SmallStructCached_JsonIter(b *testing.B) { 129 var json = jsoniter.ConfigCompatibleWithStandardLibrary 130 cached := NewSmallPayload() 131 b.ReportAllocs() 132 for i := 0; i < b.N; i++ { 133 if _, err := json.Marshal(cached); err != nil { 134 b.Fatal(err) 135 } 136 } 137} 138 139func Benchmark_Encode_SmallStructCached_EasyJson(b *testing.B) { 140 cached := NewSmallPayloadEasyJson() 141 b.ReportAllocs() 142 for i := 0; i < b.N; i++ { 143 if _, err := cached.MarshalJSON(); err != nil { 144 b.Fatal(err) 145 } 146 } 147} 148 149func Benchmark_Encode_SmallStructCached_Jettison(b *testing.B) { 150 cached := NewSmallPayload() 151 b.ReportAllocs() 152 for i := 0; i < b.N; i++ { 153 if _, err := jettison.Marshal(cached); err != nil { 154 b.Fatal(err) 155 } 156 } 157} 158 159func Benchmark_Encode_SmallStructCached_GoJay(b *testing.B) { 160 cached := NewSmallPayload() 161 b.ReportAllocs() 162 for i := 0; i < b.N; i++ { 163 if _, err := gojay.MarshalJSONObject(cached); err != nil { 164 b.Fatal(err) 165 } 166 } 167} 168 169func Benchmark_Encode_SmallStructCached_SegmentioJson(b *testing.B) { 170 cached := NewSmallPayload() 171 b.ReportAllocs() 172 for i := 0; i < b.N; i++ { 173 if _, err := segmentiojson.Marshal(cached); err != nil { 174 b.Fatal(err) 175 } 176 } 177} 178 179func Benchmark_Encode_SmallStructCached_GoJsonColored(b *testing.B) { 180 cached := NewSmallPayload() 181 colorOpt := gojson.Colorize(gojson.DefaultColorScheme) 182 b.ReportAllocs() 183 for i := 0; i < b.N; i++ { 184 if _, err := gojson.MarshalWithOption(cached, colorOpt); err != nil { 185 b.Fatal(err) 186 } 187 } 188} 189 190func Benchmark_Encode_SmallStructCached_GoJson(b *testing.B) { 191 cached := NewSmallPayload() 192 b.ReportAllocs() 193 for i := 0; i < b.N; i++ { 194 if _, err := gojson.Marshal(cached); err != nil { 195 b.Fatal(err) 196 } 197 } 198} 199 200func Benchmark_Encode_SmallStructCached_GoJsonNoEscape(b *testing.B) { 201 cached := NewSmallPayload() 202 b.ReportAllocs() 203 for i := 0; i < b.N; i++ { 204 if _, err := gojson.MarshalNoEscape(cached); err != nil { 205 b.Fatal(err) 206 } 207 } 208} 209 210func Benchmark_Encode_MediumStruct_EncodingJson(b *testing.B) { 211 b.ReportAllocs() 212 for i := 0; i < b.N; i++ { 213 if _, err := json.Marshal(NewMediumPayload()); err != nil { 214 b.Fatal(err) 215 } 216 } 217} 218 219func Benchmark_Encode_MediumStruct_JsonIter(b *testing.B) { 220 var json = jsoniter.ConfigCompatibleWithStandardLibrary 221 b.ReportAllocs() 222 for i := 0; i < b.N; i++ { 223 if _, err := json.Marshal(NewMediumPayload()); err != nil { 224 b.Fatal(err) 225 } 226 } 227} 228 229func Benchmark_Encode_MediumStruct_EasyJson(b *testing.B) { 230 b.ReportAllocs() 231 for i := 0; i < b.N; i++ { 232 if _, err := NewMediumPayloadEasyJson().MarshalJSON(); err != nil { 233 b.Fatal(err) 234 } 235 } 236} 237 238func Benchmark_Encode_MediumStruct_Jettison(b *testing.B) { 239 b.ReportAllocs() 240 for i := 0; i < b.N; i++ { 241 if _, err := jettison.Marshal(NewMediumPayload()); err != nil { 242 b.Fatal(err) 243 } 244 } 245} 246 247func Benchmark_Encode_MediumStruct_GoJay(b *testing.B) { 248 b.ReportAllocs() 249 for i := 0; i < b.N; i++ { 250 if _, err := gojay.MarshalJSONObject(NewMediumPayload()); err != nil { 251 b.Fatal(err) 252 } 253 } 254} 255 256func Benchmark_Encode_MediumStruct_SegmentioJson(b *testing.B) { 257 b.ReportAllocs() 258 for i := 0; i < b.N; i++ { 259 if _, err := segmentiojson.Marshal(NewMediumPayload()); err != nil { 260 b.Fatal(err) 261 } 262 } 263} 264 265func Benchmark_Encode_MediumStruct_GoJsonColored(b *testing.B) { 266 colorOpt := gojson.Colorize(gojson.DefaultColorScheme) 267 b.ReportAllocs() 268 for i := 0; i < b.N; i++ { 269 if _, err := gojson.MarshalWithOption(NewMediumPayload(), colorOpt); err != nil { 270 b.Fatal(err) 271 } 272 } 273} 274 275func Benchmark_Encode_MediumStruct_GoJson(b *testing.B) { 276 b.ReportAllocs() 277 for i := 0; i < b.N; i++ { 278 if _, err := gojson.Marshal(NewMediumPayload()); err != nil { 279 b.Fatal(err) 280 } 281 } 282} 283 284func Benchmark_Encode_MediumStruct_GoJsonNoEscape(b *testing.B) { 285 b.ReportAllocs() 286 for i := 0; i < b.N; i++ { 287 if _, err := gojson.MarshalNoEscape(NewMediumPayload()); err != nil { 288 b.Fatal(err) 289 } 290 } 291} 292 293func Benchmark_Encode_MediumStructCached_EncodingJson(b *testing.B) { 294 cached := NewMediumPayload() 295 b.ReportAllocs() 296 for i := 0; i < b.N; i++ { 297 if _, err := json.Marshal(cached); err != nil { 298 b.Fatal(err) 299 } 300 } 301} 302 303func Benchmark_Encode_MediumStructCached_JsonIter(b *testing.B) { 304 var json = jsoniter.ConfigCompatibleWithStandardLibrary 305 cached := NewMediumPayload() 306 b.ReportAllocs() 307 for i := 0; i < b.N; i++ { 308 if _, err := json.Marshal(cached); err != nil { 309 b.Fatal(err) 310 } 311 } 312} 313 314func Benchmark_Encode_MediumStructCached_EasyJson(b *testing.B) { 315 cached := NewMediumPayloadEasyJson() 316 b.ReportAllocs() 317 for i := 0; i < b.N; i++ { 318 if _, err := cached.MarshalJSON(); err != nil { 319 b.Fatal(err) 320 } 321 } 322} 323 324func Benchmark_Encode_MediumStructCached_Jettison(b *testing.B) { 325 cached := NewMediumPayload() 326 b.ReportAllocs() 327 for i := 0; i < b.N; i++ { 328 if _, err := jettison.Marshal(cached); err != nil { 329 b.Fatal(err) 330 } 331 } 332} 333 334func Benchmark_Encode_MediumStructCached_GoJay(b *testing.B) { 335 cached := NewMediumPayload() 336 b.ReportAllocs() 337 for i := 0; i < b.N; i++ { 338 if _, err := gojay.MarshalJSONObject(cached); err != nil { 339 b.Fatal(err) 340 } 341 } 342} 343 344func Benchmark_Encode_MediumStructCached_SegmentioJson(b *testing.B) { 345 cached := NewMediumPayload() 346 b.ReportAllocs() 347 for i := 0; i < b.N; i++ { 348 if _, err := segmentiojson.Marshal(cached); err != nil { 349 b.Fatal(err) 350 } 351 } 352} 353 354func Benchmark_Encode_MediumStructCached_GoJsonColored(b *testing.B) { 355 cached := NewMediumPayload() 356 colorOpt := gojson.Colorize(gojson.DefaultColorScheme) 357 b.ReportAllocs() 358 for i := 0; i < b.N; i++ { 359 if _, err := gojson.MarshalWithOption(cached, colorOpt); err != nil { 360 b.Fatal(err) 361 } 362 } 363} 364 365func Benchmark_Encode_MediumStructCached_GoJson(b *testing.B) { 366 cached := NewMediumPayload() 367 b.ReportAllocs() 368 for i := 0; i < b.N; i++ { 369 if _, err := gojson.Marshal(cached); err != nil { 370 b.Fatal(err) 371 } 372 } 373} 374 375func Benchmark_Encode_MediumStructCached_GoJsonNoEscape(b *testing.B) { 376 cached := NewMediumPayload() 377 b.ReportAllocs() 378 for i := 0; i < b.N; i++ { 379 if _, err := gojson.MarshalNoEscape(cached); err != nil { 380 b.Fatal(err) 381 } 382 } 383} 384 385func Benchmark_Encode_LargeStruct_EncodingJson(b *testing.B) { 386 b.ReportAllocs() 387 for i := 0; i < b.N; i++ { 388 if _, err := json.Marshal(NewLargePayload()); err != nil { 389 b.Fatal(err) 390 } 391 } 392} 393 394func Benchmark_Encode_LargeStruct_JsonIter(b *testing.B) { 395 var json = jsoniter.ConfigCompatibleWithStandardLibrary 396 b.ReportAllocs() 397 for i := 0; i < b.N; i++ { 398 if _, err := json.Marshal(NewLargePayload()); err != nil { 399 b.Fatal(err) 400 } 401 } 402} 403 404func Benchmark_Encode_LargeStruct_EasyJson(b *testing.B) { 405 b.ReportAllocs() 406 for i := 0; i < b.N; i++ { 407 if _, err := NewLargePayloadEasyJson().MarshalJSON(); err != nil { 408 b.Fatal(err) 409 } 410 } 411} 412 413func Benchmark_Encode_LargeStruct_Jettison(b *testing.B) { 414 b.ReportAllocs() 415 for i := 0; i < b.N; i++ { 416 if _, err := jettison.Marshal(NewLargePayload()); err != nil { 417 b.Fatal(err) 418 } 419 } 420} 421 422func Benchmark_Encode_LargeStruct_GoJay(b *testing.B) { 423 b.ReportAllocs() 424 for i := 0; i < b.N; i++ { 425 if _, err := gojay.MarshalJSONObject(NewLargePayload()); err != nil { 426 b.Fatal(err) 427 } 428 } 429} 430 431func Benchmark_Encode_LargeStruct_SegmentioJson(b *testing.B) { 432 b.ReportAllocs() 433 for i := 0; i < b.N; i++ { 434 if _, err := segmentiojson.Marshal(NewLargePayload()); err != nil { 435 b.Fatal(err) 436 } 437 } 438} 439 440func Benchmark_Encode_LargeStruct_GoJsonColored(b *testing.B) { 441 colorOpt := gojson.Colorize(gojson.DefaultColorScheme) 442 b.ReportAllocs() 443 for i := 0; i < b.N; i++ { 444 if _, err := gojson.MarshalWithOption(NewLargePayload(), colorOpt); err != nil { 445 b.Fatal(err) 446 } 447 } 448} 449 450func Benchmark_Encode_LargeStruct_GoJson(b *testing.B) { 451 b.ReportAllocs() 452 for i := 0; i < b.N; i++ { 453 if _, err := gojson.Marshal(NewLargePayload()); err != nil { 454 b.Fatal(err) 455 } 456 } 457} 458 459func Benchmark_Encode_LargeStruct_GoJsonNoEscape(b *testing.B) { 460 b.ReportAllocs() 461 for i := 0; i < b.N; i++ { 462 if _, err := gojson.MarshalNoEscape(NewLargePayload()); err != nil { 463 b.Fatal(err) 464 } 465 } 466} 467 468func Benchmark_Encode_LargeStructCached_EncodingJson(b *testing.B) { 469 cached := NewLargePayload() 470 b.ReportAllocs() 471 for i := 0; i < b.N; i++ { 472 if _, err := json.Marshal(cached); err != nil { 473 b.Fatal(err) 474 } 475 } 476} 477 478func Benchmark_Encode_LargeStructCached_JsonIter(b *testing.B) { 479 var json = jsoniter.ConfigCompatibleWithStandardLibrary 480 cached := NewLargePayload() 481 b.ReportAllocs() 482 for i := 0; i < b.N; i++ { 483 if _, err := json.Marshal(cached); err != nil { 484 b.Fatal(err) 485 } 486 } 487} 488 489func Benchmark_Encode_LargeStructCached_EasyJson(b *testing.B) { 490 cached := NewLargePayloadEasyJson() 491 b.ReportAllocs() 492 for i := 0; i < b.N; i++ { 493 if _, err := cached.MarshalJSON(); err != nil { 494 b.Fatal(err) 495 } 496 } 497} 498 499func Benchmark_Encode_LargeStructCached_Jettison(b *testing.B) { 500 cached := NewLargePayload() 501 b.ReportAllocs() 502 for i := 0; i < b.N; i++ { 503 if _, err := jettison.Marshal(cached); err != nil { 504 b.Fatal(err) 505 } 506 } 507} 508 509func Benchmark_Encode_LargeStructCached_GoJay(b *testing.B) { 510 cached := NewLargePayload() 511 b.ReportAllocs() 512 for i := 0; i < b.N; i++ { 513 if _, err := gojay.MarshalJSONObject(cached); err != nil { 514 b.Fatal(err) 515 } 516 } 517} 518 519func Benchmark_Encode_LargeStructCached_SegmentioJson(b *testing.B) { 520 cached := NewLargePayload() 521 b.ReportAllocs() 522 for i := 0; i < b.N; i++ { 523 if _, err := segmentiojson.Marshal(cached); err != nil { 524 b.Fatal(err) 525 } 526 } 527} 528 529func Benchmark_Encode_LargeStructCached_GoJsonColored(b *testing.B) { 530 cached := NewLargePayload() 531 colorOpt := gojson.Colorize(gojson.DefaultColorScheme) 532 b.ReportAllocs() 533 for i := 0; i < b.N; i++ { 534 if _, err := gojson.MarshalWithOption(cached, colorOpt); err != nil { 535 b.Fatal(err) 536 } 537 } 538} 539 540func Benchmark_Encode_LargeStructCached_GoJson(b *testing.B) { 541 cached := NewLargePayload() 542 b.ReportAllocs() 543 for i := 0; i < b.N; i++ { 544 if _, err := gojson.Marshal(cached); err != nil { 545 b.Fatal(err) 546 } 547 } 548} 549 550func Benchmark_Encode_LargeStructCached_GoJsonNoEscape(b *testing.B) { 551 cached := NewLargePayload() 552 b.ReportAllocs() 553 for i := 0; i < b.N; i++ { 554 if _, err := gojson.MarshalNoEscape(cached); err != nil { 555 b.Fatal(err) 556 } 557 } 558} 559 560func benchMapValue() map[string]interface{} { 561 return map[string]interface{}{ 562 "a": 1, 563 "b": 2.1, 564 "c": "hello", 565 "d": struct { 566 V int 567 }{ 568 V: 1, 569 }, 570 "e": true, 571 } 572} 573 574func Benchmark_Encode_MapInterface_EncodingJson(b *testing.B) { 575 v := benchMapValue() 576 b.ReportAllocs() 577 for i := 0; i < b.N; i++ { 578 if _, err := json.Marshal(v); err != nil { 579 b.Fatal(err) 580 } 581 } 582} 583 584func Benchmark_Encode_MapInterface_JsonIter(b *testing.B) { 585 v := benchMapValue() 586 var json = jsoniter.ConfigCompatibleWithStandardLibrary 587 b.ReportAllocs() 588 for i := 0; i < b.N; i++ { 589 if _, err := json.Marshal(v); err != nil { 590 b.Fatal(err) 591 } 592 } 593} 594 595func Benchmark_Encode_MapInterface_Jettison(b *testing.B) { 596 v := benchMapValue() 597 b.ReportAllocs() 598 for i := 0; i < b.N; i++ { 599 if _, err := jettison.Marshal(v); err != nil { 600 b.Fatal(err) 601 } 602 } 603} 604 605func Benchmark_Encode_MapInterface_SegmentioJson(b *testing.B) { 606 v := benchMapValue() 607 b.ReportAllocs() 608 for i := 0; i < b.N; i++ { 609 if _, err := segmentiojson.Marshal(v); err != nil { 610 b.Fatal(err) 611 } 612 } 613} 614 615func Benchmark_Encode_MapInterface_GoJson(b *testing.B) { 616 v := benchMapValue() 617 b.ReportAllocs() 618 for i := 0; i < b.N; i++ { 619 if _, err := gojson.Marshal(v); err != nil { 620 b.Fatal(err) 621 } 622 } 623} 624 625func Benchmark_Encode_Interface_SegmentioJson(b *testing.B) { 626 v := []interface{}{1} 627 b.ReportAllocs() 628 for i := 0; i < b.N; i++ { 629 if _, err := segmentiojson.Marshal(v); err != nil { 630 b.Fatal(err) 631 } 632 } 633} 634 635func Benchmark_Encode_Interface_GoJson(b *testing.B) { 636 v := []interface{}{1} 637 b.ReportAllocs() 638 for i := 0; i < b.N; i++ { 639 if _, err := gojson.Marshal(v); err != nil { 640 b.Fatal(err) 641 } 642 } 643} 644 645func Benchmark_Encode_Bool_EncodingJson(b *testing.B) { 646 b.ReportAllocs() 647 var buf bytes.Buffer 648 enc := json.NewEncoder(&buf) 649 for i := 0; i < b.N; i++ { 650 if err := enc.Encode(true); err != nil { 651 b.Fatal(err) 652 } 653 } 654} 655 656func Benchmark_Encode_Bool_JsonIter(b *testing.B) { 657 var json = jsoniter.ConfigCompatibleWithStandardLibrary 658 b.ReportAllocs() 659 var buf bytes.Buffer 660 enc := json.NewEncoder(&buf) 661 for i := 0; i < b.N; i++ { 662 if err := enc.Encode(true); err != nil { 663 b.Fatal(err) 664 } 665 } 666} 667 668func Benchmark_Encode_Bool_SegmentioJson(b *testing.B) { 669 b.ReportAllocs() 670 var buf bytes.Buffer 671 enc := segmentiojson.NewEncoder(&buf) 672 for i := 0; i < b.N; i++ { 673 if err := enc.Encode(true); err != nil { 674 b.Fatal(err) 675 } 676 } 677} 678 679func Benchmark_Encode_Bool_GoJson(b *testing.B) { 680 b.ReportAllocs() 681 var buf bytes.Buffer 682 enc := gojson.NewEncoder(&buf) 683 for i := 0; i < b.N; i++ { 684 if err := enc.Encode(true); err != nil { 685 b.Fatal(err) 686 } 687 } 688} 689 690func Benchmark_Marshal_Bool_EncodingJson(b *testing.B) { 691 b.ReportAllocs() 692 for i := 0; i < b.N; i++ { 693 if _, err := json.Marshal(true); err != nil { 694 b.Fatal(err) 695 } 696 } 697} 698 699func Benchmark_Marshal_Bool_JsonIter(b *testing.B) { 700 var json = jsoniter.ConfigCompatibleWithStandardLibrary 701 b.ReportAllocs() 702 for i := 0; i < b.N; i++ { 703 if _, err := json.Marshal(true); err != nil { 704 b.Fatal(err) 705 } 706 } 707} 708 709func Benchmark_Marshal_Bool_Jettison(b *testing.B) { 710 b.ReportAllocs() 711 for i := 0; i < b.N; i++ { 712 if _, err := jettison.Marshal(true); err != nil { 713 b.Fatal(err) 714 } 715 } 716} 717 718func Benchmark_Marshal_Bool_SegmentioJson(b *testing.B) { 719 b.ReportAllocs() 720 for i := 0; i < b.N; i++ { 721 if _, err := segmentiojson.Marshal(true); err != nil { 722 b.Fatal(err) 723 } 724 } 725} 726 727func Benchmark_Marshal_Bool_GoJson(b *testing.B) { 728 b.ReportAllocs() 729 for i := 0; i < b.N; i++ { 730 if _, err := gojson.Marshal(true); err != nil { 731 b.Fatal(err) 732 } 733 } 734} 735 736func Benchmark_Encode_Int_EncodingJson(b *testing.B) { 737 b.ReportAllocs() 738 for i := 0; i < b.N; i++ { 739 if _, err := json.Marshal(1); err != nil { 740 b.Fatal(err) 741 } 742 } 743} 744 745func Benchmark_Encode_Int_JsonIter(b *testing.B) { 746 var json = jsoniter.ConfigCompatibleWithStandardLibrary 747 b.ReportAllocs() 748 for i := 0; i < b.N; i++ { 749 if _, err := json.Marshal(1); err != nil { 750 b.Fatal(err) 751 } 752 } 753} 754 755func Benchmark_Encode_Int_Jettison(b *testing.B) { 756 b.ReportAllocs() 757 for i := 0; i < b.N; i++ { 758 if _, err := jettison.Marshal(1); err != nil { 759 b.Fatal(err) 760 } 761 } 762} 763 764func Benchmark_Encode_Int_SegmentioJson(b *testing.B) { 765 b.ReportAllocs() 766 for i := 0; i < b.N; i++ { 767 if _, err := segmentiojson.Marshal(1); err != nil { 768 b.Fatal(err) 769 } 770 } 771} 772 773func Benchmark_Encode_Int_GoJson(b *testing.B) { 774 b.ReportAllocs() 775 for i := 0; i < b.N; i++ { 776 if _, err := gojson.Marshal(1); err != nil { 777 b.Fatal(err) 778 } 779 } 780} 781 782type marshaler struct{} 783 784func (*marshaler) MarshalJSON() ([]byte, error) { 785 return []byte(`"hello"`), nil 786} 787 788func Benchmark_Encode_MarshalJSON_EncodingJson(b *testing.B) { 789 v := &marshaler{} 790 b.ReportAllocs() 791 for i := 0; i < b.N; i++ { 792 if _, err := json.Marshal(v); err != nil { 793 b.Fatal(err) 794 } 795 } 796} 797 798func Benchmark_Encode_MarshalJSON_JsonIter(b *testing.B) { 799 v := &marshaler{} 800 var json = jsoniter.ConfigCompatibleWithStandardLibrary 801 b.ReportAllocs() 802 for i := 0; i < b.N; i++ { 803 if _, err := json.Marshal(v); err != nil { 804 b.Fatal(err) 805 } 806 } 807} 808 809func Benchmark_Encode_MarshalJSON_Jettison(b *testing.B) { 810 v := &marshaler{} 811 b.ReportAllocs() 812 for i := 0; i < b.N; i++ { 813 if _, err := jettison.Marshal(v); err != nil { 814 b.Fatal(err) 815 } 816 } 817} 818 819func Benchmark_Encode_MarshalJSON_SegmentioJson(b *testing.B) { 820 v := &marshaler{} 821 b.ReportAllocs() 822 for i := 0; i < b.N; i++ { 823 if _, err := segmentiojson.Marshal(v); err != nil { 824 b.Fatal(err) 825 } 826 } 827} 828 829func Benchmark_Encode_MarshalJSON_GoJson(b *testing.B) { 830 v := &marshaler{} 831 b.ReportAllocs() 832 for i := 0; i < b.N; i++ { 833 if _, err := gojson.Marshal(v); err != nil { 834 b.Fatal(err) 835 } 836 } 837} 838