| 1 |
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package benchmarks |
| 5 |
|
| 6 |
import ( |
| 7 |
"embed" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/go-openapi/swag/jsonutils" |
| 11 |
"github.com/go-openapi/swag/jsonutils/adapters" |
| 12 |
easyjson "github.com/go-openapi/swag/jsonutils/adapters/easyjson/json" |
| 13 |
fixtures "github.com/go-openapi/swag/jsonutils/fixtures_test" |
| 14 |
) |
| 15 |
|
| 16 |
//go:embed fixtures/*.json |
| 17 |
var EmbeddedFixtures embed.FS |
| 18 |
|
| 19 |
var ( |
| 20 |
smallJSON []byte |
| 21 |
mediumJSON []byte |
| 22 |
largeJSON []byte |
| 23 |
) |
| 24 |
|
| 25 |
type benchmarkContext struct { |
| 26 |
small *SmallPayload |
| 27 |
medium *MediumPayload |
| 28 |
large *LargePayload |
| 29 |
} |
| 30 |
|
| 31 |
func (c *benchmarkContext) Small() *SmallPayload { |
| 32 |
return c.small |
| 33 |
} |
| 34 |
func (c *benchmarkContext) Medium() *MediumPayload { |
| 35 |
return c.medium |
| 36 |
} |
| 37 |
func (c *benchmarkContext) Large() *LargePayload { |
| 38 |
return c.large |
| 39 |
} |
| 40 |
|
| 41 |
func BenchmarkJSON(b *testing.B) { |
| 42 |
ctx := initBenchmarks(b) |
| 43 |
|
| 44 |
b.ResetTimer() |
| 45 |
|
| 46 |
// stdlib is registered by default: it ignores MarshalEasyJSON and UnmarshalEasyJSON |
| 47 |
b.Run("with standard library", allBenchs(ctx, "standard")) |
| 48 |
|
| 49 |
adapters.Registry.Reset() |
| 50 |
easyjson.Register(adapters.Registry) |
| 51 |
|
| 52 |
// now easyjson is registered: it prioritizes MarshalEasyJSON and UnmarshalEasyJSON |
| 53 |
b.Run("with easyjson library", allBenchs(ctx, "easyjson")) |
| 54 |
} |
| 55 |
|
| 56 |
func initBenchmarks(b *testing.B) *benchmarkContext { |
| 57 |
smallJSON = fixtures.ShouldLoadFixture(b, EmbeddedFixtures, "fixtures/small_sample.json") |
| 58 |
mediumJSON = fixtures.ShouldLoadFixture(b, EmbeddedFixtures, "fixtures/medium_sample.json") |
| 59 |
largeJSON = fixtures.ShouldLoadFixture(b, EmbeddedFixtures, "fixtures/large_sample.json") |
| 60 |
|
| 61 |
return &benchmarkContext{ |
| 62 |
small: NewSmallPayload(), |
| 63 |
medium: NewMediumPayload(), |
| 64 |
large: NewLargePayload(), |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func allBenchs(ctx *benchmarkContext, library string) func(*testing.B) { |
| 69 |
return func(b *testing.B) { |
| 70 |
b.Run(library+" ReadJSON - small", benchRead[SmallPayload](smallJSON)) |
| 71 |
b.Run(library+" WriteJSON - small", benchWrite(ctx.Small)) |
| 72 |
b.Run(library+" FromDynamicJSON - small", benchFromDynamic(ctx.Small)) |
| 73 |
b.Run(library+" ReadJSON - medium", benchRead[MediumPayload](mediumJSON)) |
| 74 |
b.Run(library+" WriteJSON - medium", benchWrite(ctx.Medium)) |
| 75 |
b.Run(library+" FromDynamicJSON - medium", benchFromDynamic(ctx.Medium)) |
| 76 |
b.Run(library+" ReadJSON - large", benchRead[LargePayload](largeJSON)) |
| 77 |
b.Run(library+" WriteJSON - large", benchWrite(ctx.Large)) |
| 78 |
b.Run(library+" FromDynamicJSON - large", benchFromDynamic(ctx.Large)) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func benchRead[T any](jazon []byte) func(*testing.B) { |
| 83 |
return func(b *testing.B) { |
| 84 |
b.SetBytes(int64(len(jazon))) |
| 85 |
|
| 86 |
for b.Loop() { |
| 87 |
var data T |
| 88 |
if err := jsonutils.ReadJSON(jazon, &data); err != nil { |
| 89 |
b.Logf("unexpected error: %v", err) |
| 90 |
b.FailNow() |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func benchWrite[T any](constructor func() *T) func(*testing.B) { |
| 97 |
data := constructor() |
| 98 |
return func(b *testing.B) { |
| 99 |
for b.Loop() { |
| 100 |
jazon, err := jsonutils.WriteJSON(data) |
| 101 |
if err != nil { |
| 102 |
b.Logf("unexpected error: %v", err) |
| 103 |
b.FailNow() |
| 104 |
} |
| 105 |
b.SetBytes(int64(len(jazon))) |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func benchFromDynamic[T any](constructor func() *T) func(*testing.B) { |
| 111 |
source := constructor() |
| 112 |
return func(b *testing.B) { |
| 113 |
for b.Loop() { |
| 114 |
var target T |
| 115 |
|
| 116 |
if err := jsonutils.FromDynamicJSON(source, &target); err != nil { |
| 117 |
b.Logf("unexpected error: %v", err) |
| 118 |
b.FailNow() |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |