| 1 |
package benchmark |
| 2 |
|
| 3 |
import ( |
| 4 |
stdjson "encoding/json" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/bytedance/sonic" |
| 8 |
gojson "github.com/goccy/go-json" |
| 9 |
) |
| 10 |
|
| 11 |
// The decode benchmarks of go-json beside sonic, on the payloads of the other decode benchmarks and on |
| 12 |
// the Twitter payload of sonic ( sonic_bench_test.go ), decoded into its struct ( Binding ) and into |
| 13 |
// interface{} ( Generic ). Sonic is sonic.ConfigDefault and SonicStd is sonic.ConfigStd, which validates |
| 14 |
// the strings as encoding/json does; the types are compiled before the benchmarks ( sonic_test.go ). |
| 15 |
// |
| 16 |
// SonicFastest is sonic at its fastest: sonic.ConfigFastest, which skips the values of no field without |
| 17 |
// validating them, decoding the input as a string ( UnmarshalFromString ), which sonic doesn't copy, while |
| 18 |
// Unmarshal copies the bytes it is given into a string first. Its strings refer to the input, as the ones of |
| 19 |
// sonic do by default. GoJsonUnmarshalOfNoCopyString is go-json at its fastest: UnmarshalOf, with the strings |
| 20 |
// referring to the input ( DecodeNoCopyString ). SonicFastestValidating is SonicFastest with the checks of |
| 21 |
// go-json ( see sonicFastestValidating ), which is the one to compare it with. |
| 22 |
// |
| 23 |
// Sonic has two decoders on amd64, the JIT one and the one of SONIC_USE_OPTDEC=1, which the environment chooses |
| 24 |
// for the process: the comparison is run with both ( see the CI ). |
| 25 |
|
| 26 |
func benchDecode[T any](b *testing.B, data []byte, unmarshal func([]byte, any) error) { |
| 27 |
b.SetBytes(int64(len(data))) |
| 28 |
b.ReportAllocs() |
| 29 |
b.ResetTimer() |
| 30 |
for i := 0; i < b.N; i++ { |
| 31 |
var v T |
| 32 |
if err := unmarshal(data, &v); err != nil { |
| 33 |
b.Fatal(err) |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func Benchmark_Decode_SmallStruct_Unmarshal_Sonic(b *testing.B) { |
| 39 |
pretouchSonic() |
| 40 |
benchDecode[SmallPayload](b, SmallFixture, sonic.ConfigDefault.Unmarshal) |
| 41 |
} |
| 42 |
|
| 43 |
func Benchmark_Decode_SmallStruct_Unmarshal_SonicStd(b *testing.B) { |
| 44 |
pretouchSonic() |
| 45 |
benchDecode[SmallPayload](b, SmallFixture, sonic.ConfigStd.Unmarshal) |
| 46 |
} |
| 47 |
|
| 48 |
func Benchmark_Decode_MediumStruct_Unmarshal_Sonic(b *testing.B) { |
| 49 |
pretouchSonic() |
| 50 |
benchDecode[MediumPayload](b, MediumFixture, sonic.ConfigDefault.Unmarshal) |
| 51 |
} |
| 52 |
|
| 53 |
func Benchmark_Decode_MediumStruct_Unmarshal_SonicStd(b *testing.B) { |
| 54 |
pretouchSonic() |
| 55 |
benchDecode[MediumPayload](b, MediumFixture, sonic.ConfigStd.Unmarshal) |
| 56 |
} |
| 57 |
|
| 58 |
func Benchmark_Decode_LargeStruct_Unmarshal_Sonic(b *testing.B) { |
| 59 |
pretouchSonic() |
| 60 |
benchDecode[LargePayload](b, LargeFixture, sonic.ConfigDefault.Unmarshal) |
| 61 |
} |
| 62 |
|
| 63 |
func Benchmark_Decode_LargeStruct_Unmarshal_SonicStd(b *testing.B) { |
| 64 |
pretouchSonic() |
| 65 |
benchDecode[LargePayload](b, LargeFixture, sonic.ConfigStd.Unmarshal) |
| 66 |
} |
| 67 |
|
| 68 |
func Benchmark_Decode_TwitterBinding_Unmarshal_EncodingJson(b *testing.B) { |
| 69 |
benchDecode[TwitterStruct](b, []byte(TwitterJson), stdjson.Unmarshal) |
| 70 |
} |
| 71 |
|
| 72 |
func Benchmark_Decode_TwitterBinding_Unmarshal_GoJson(b *testing.B) { |
| 73 |
benchDecode[TwitterStruct](b, []byte(TwitterJson), gojson.Unmarshal) |
| 74 |
} |
| 75 |
|
| 76 |
func Benchmark_Decode_TwitterBinding_Unmarshal_GoJsonUnmarshalOf(b *testing.B) { |
| 77 |
data := []byte(TwitterJson) |
| 78 |
b.SetBytes(int64(len(data))) |
| 79 |
b.ReportAllocs() |
| 80 |
for i := 0; i < b.N; i++ { |
| 81 |
var v TwitterStruct |
| 82 |
if err := gojson.UnmarshalOf(data, &v); err != nil { |
| 83 |
b.Fatal(err) |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// GoJsonLikeSonic is go-json with the option which does what sonic does by default: |
| 89 |
// the strings refer to the input instead of copies. |
| 90 |
func Benchmark_Decode_TwitterBinding_Unmarshal_GoJsonLikeSonic(b *testing.B) { |
| 91 |
benchDecode[TwitterStruct](b, []byte(TwitterJson), func(data []byte, v any) error { |
| 92 |
return gojson.UnmarshalWithOption(data, v, gojson.DecodeNoCopyString()) |
| 93 |
}) |
| 94 |
} |
| 95 |
|
| 96 |
func Benchmark_Decode_TwitterGeneric_Unmarshal_GoJsonLikeSonic(b *testing.B) { |
| 97 |
benchDecode[any](b, []byte(TwitterJson), func(data []byte, v any) error { |
| 98 |
return gojson.UnmarshalWithOption(data, v, gojson.DecodeNoCopyString()) |
| 99 |
}) |
| 100 |
} |
| 101 |
|
| 102 |
func Benchmark_Decode_TwitterBinding_Unmarshal_Sonic(b *testing.B) { |
| 103 |
pretouchSonic() |
| 104 |
benchDecode[TwitterStruct](b, []byte(TwitterJson), sonic.ConfigDefault.Unmarshal) |
| 105 |
} |
| 106 |
|
| 107 |
func Benchmark_Decode_TwitterBinding_Unmarshal_SonicStd(b *testing.B) { |
| 108 |
pretouchSonic() |
| 109 |
benchDecode[TwitterStruct](b, []byte(TwitterJson), sonic.ConfigStd.Unmarshal) |
| 110 |
} |
| 111 |
|
| 112 |
func Benchmark_Decode_TwitterGeneric_Unmarshal_EncodingJson(b *testing.B) { |
| 113 |
benchDecode[any](b, []byte(TwitterJson), stdjson.Unmarshal) |
| 114 |
} |
| 115 |
|
| 116 |
func Benchmark_Decode_TwitterGeneric_Unmarshal_GoJson(b *testing.B) { |
| 117 |
benchDecode[any](b, []byte(TwitterJson), gojson.Unmarshal) |
| 118 |
} |
| 119 |
|
| 120 |
func Benchmark_Decode_TwitterGeneric_Unmarshal_Sonic(b *testing.B) { |
| 121 |
pretouchSonic() |
| 122 |
benchDecode[any](b, []byte(TwitterJson), sonic.ConfigDefault.Unmarshal) |
| 123 |
} |
| 124 |
|
| 125 |
func Benchmark_Decode_TwitterGeneric_Unmarshal_SonicStd(b *testing.B) { |
| 126 |
pretouchSonic() |
| 127 |
benchDecode[any](b, []byte(TwitterJson), sonic.ConfigStd.Unmarshal) |
| 128 |
} |
| 129 |
|
| 130 |
// benchDecodeSonicFastest decodes the data by sonic at its fastest: the data is made a string once, outside of |
| 131 |
// the loop, as a program which has the input as a string does. |
| 132 |
func benchDecodeSonicFastest[T any](b *testing.B, data []byte) { |
| 133 |
benchDecodeSonicString[T](b, sonic.ConfigFastest, data) |
| 134 |
} |
| 135 |
|
| 136 |
// sonicFastestValidating is sonic at its fastest with the checks which encoding/json and go-json do: the values |
| 137 |
// which are skipped are validated ( NoValidateJSONSkip is not set ), and so are the strings ( ValidateString: a |
| 138 |
// control character is an error, and invalid UTF-8 is replaced ). Its strings refer to the input, as the ones of |
| 139 |
// GoJsonUnmarshalOfNoCopyString, which does the same checks. |
| 140 |
var sonicFastestValidating = sonic.Config{ValidateString: true}.Froze() |
| 141 |
|
| 142 |
// benchDecodeSonicFastestValidating decodes the data as benchDecodeSonicFastest does, with the checks of go-json. |
| 143 |
func benchDecodeSonicFastestValidating[T any](b *testing.B, data []byte) { |
| 144 |
benchDecodeSonicString[T](b, sonicFastestValidating, data) |
| 145 |
} |
| 146 |
|
| 147 |
// benchDecodeSonicString decodes the data by api from a string, which is made once, outside of the loop. |
| 148 |
func benchDecodeSonicString[T any](b *testing.B, api sonic.API, data []byte) { |
| 149 |
s := string(data) |
| 150 |
b.SetBytes(int64(len(data))) |
| 151 |
b.ReportAllocs() |
| 152 |
b.ResetTimer() |
| 153 |
for i := 0; i < b.N; i++ { |
| 154 |
var v T |
| 155 |
if err := api.UnmarshalFromString(s, &v); err != nil { |
| 156 |
b.Fatal(err) |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// benchDecodeNoCopy decodes the data by go-json at its fastest. |
| 162 |
func benchDecodeNoCopy[T any](b *testing.B, data []byte) { |
| 163 |
b.SetBytes(int64(len(data))) |
| 164 |
b.ReportAllocs() |
| 165 |
b.ResetTimer() |
| 166 |
for i := 0; i < b.N; i++ { |
| 167 |
var v T |
| 168 |
if err := gojson.UnmarshalOf(data, &v, gojson.DecodeNoCopyString()); err != nil { |
| 169 |
b.Fatal(err) |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func Benchmark_Decode_SmallStruct_Unmarshal_SonicFastest(b *testing.B) { |
| 175 |
pretouchSonic() |
| 176 |
benchDecodeSonicFastest[SmallPayload](b, SmallFixture) |
| 177 |
} |
| 178 |
|
| 179 |
func Benchmark_Decode_SmallStruct_Unmarshal_SonicFastestValidating(b *testing.B) { |
| 180 |
pretouchSonic() |
| 181 |
benchDecodeSonicFastestValidating[SmallPayload](b, SmallFixture) |
| 182 |
} |
| 183 |
|
| 184 |
func Benchmark_Decode_MediumStruct_Unmarshal_SonicFastest(b *testing.B) { |
| 185 |
pretouchSonic() |
| 186 |
benchDecodeSonicFastest[MediumPayload](b, MediumFixture) |
| 187 |
} |
| 188 |
|
| 189 |
func Benchmark_Decode_MediumStruct_Unmarshal_SonicFastestValidating(b *testing.B) { |
| 190 |
pretouchSonic() |
| 191 |
benchDecodeSonicFastestValidating[MediumPayload](b, MediumFixture) |
| 192 |
} |
| 193 |
|
| 194 |
func Benchmark_Decode_LargeStruct_Unmarshal_SonicFastest(b *testing.B) { |
| 195 |
pretouchSonic() |
| 196 |
benchDecodeSonicFastest[LargePayload](b, LargeFixture) |
| 197 |
} |
| 198 |
|
| 199 |
func Benchmark_Decode_LargeStruct_Unmarshal_SonicFastestValidating(b *testing.B) { |
| 200 |
pretouchSonic() |
| 201 |
benchDecodeSonicFastestValidating[LargePayload](b, LargeFixture) |
| 202 |
} |
| 203 |
|
| 204 |
func Benchmark_Decode_TwitterBinding_Unmarshal_GoJsonUnmarshalOfNoCopyString(b *testing.B) { |
| 205 |
benchDecodeNoCopy[TwitterStruct](b, []byte(TwitterJson)) |
| 206 |
} |
| 207 |
|
| 208 |
func Benchmark_Decode_TwitterBinding_Unmarshal_SonicFastest(b *testing.B) { |
| 209 |
pretouchSonic() |
| 210 |
benchDecodeSonicFastest[TwitterStruct](b, []byte(TwitterJson)) |
| 211 |
} |
| 212 |
|
| 213 |
func Benchmark_Decode_TwitterBinding_Unmarshal_SonicFastestValidating(b *testing.B) { |
| 214 |
pretouchSonic() |
| 215 |
benchDecodeSonicFastestValidating[TwitterStruct](b, []byte(TwitterJson)) |
| 216 |
} |
| 217 |
|
| 218 |
func Benchmark_Decode_TwitterGeneric_Unmarshal_SonicFastest(b *testing.B) { |
| 219 |
pretouchSonic() |
| 220 |
benchDecodeSonicFastest[any](b, []byte(TwitterJson)) |
| 221 |
} |
| 222 |
|
| 223 |
func Benchmark_Decode_TwitterGeneric_Unmarshal_SonicFastestValidating(b *testing.B) { |
| 224 |
pretouchSonic() |
| 225 |
benchDecodeSonicFastestValidating[any](b, []byte(TwitterJson)) |
| 226 |
} |