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