1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
|
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package json
import (
"bytes"
"fmt"
"io"
"os"
"path"
"reflect"
"strings"
"testing"
"testing/iotest"
jsonv1 "encoding/json"
)
var benchV1 = os.Getenv("BENCHMARK_V1") != ""
// bytesBuffer is identical to bytes.Buffer,
// but a different type to avoid any optimizations for bytes.Buffer.
type bytesBuffer struct{ *bytes.Buffer }
var arshalTestdata = []struct {
name string
raw []byte
val any
new func() any
skipV1 bool
}{{
name: "Bool",
raw: []byte("true"),
val: addr(true),
new: func() any { return new(bool) },
}, {
name: "String",
raw: []byte(`"hello, world!"`),
val: addr("hello, world!"),
new: func() any { return new(string) },
}, {
name: "Int",
raw: []byte("-1234"),
val: addr(int64(-1234)),
new: func() any { return new(int64) },
}, {
name: "Uint",
raw: []byte("1234"),
val: addr(uint64(1234)),
new: func() any { return new(uint64) },
}, {
name: "Float",
raw: []byte("12.34"),
val: addr(float64(12.34)),
new: func() any { return new(float64) },
}, {
name: "Map/ManyEmpty",
raw: []byte(`[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]`),
val: addr(func() (out []map[string]string) {
for i := 0; i < 100; i++ {
out = append(out, map[string]string{})
}
return out
}()),
new: func() any { return new([]map[string]string) },
}, {
name: "Map/OneLarge",
raw: []byte(`{"A":"A","B":"B","C":"C","D":"D","E":"E","F":"F","G":"G","H":"H","I":"I","J":"J","K":"K","L":"L","M":"M","N":"N","O":"O","P":"P","Q":"Q","R":"R","S":"S","T":"T","U":"U","V":"V","W":"W","X":"X","Y":"Y","Z":"Z"}`),
val: addr(map[string]string{"A": "A", "B": "B", "C": "C", "D": "D", "E": "E", "F": "F", "G": "G", "H": "H", "I": "I", "J": "J", "K": "K", "L": "L", "M": "M", "N": "N", "O": "O", "P": "P", "Q": "Q", "R": "R", "S": "S", "T": "T", "U": "U", "V": "V", "W": "W", "X": "X", "Y": "Y", "Z": "Z"}),
new: func() any { return new(map[string]string) },
}, {
name: "Map/ManySmall",
raw: []byte(`{"A":{"K":"V"},"B":{"K":"V"},"C":{"K":"V"},"D":{"K":"V"},"E":{"K":"V"},"F":{"K":"V"},"G":{"K":"V"},"H":{"K":"V"},"I":{"K":"V"},"J":{"K":"V"},"K":{"K":"V"},"L":{"K":"V"},"M":{"K":"V"},"N":{"K":"V"},"O":{"K":"V"},"P":{"K":"V"},"Q":{"K":"V"},"R":{"K":"V"},"S":{"K":"V"},"T":{"K":"V"},"U":{"K":"V"},"V":{"K":"V"},"W":{"K":"V"},"X":{"K":"V"},"Y":{"K":"V"},"Z":{"K":"V"}}`),
val: addr(map[string]map[string]string{"A": {"K": "V"}, "B": {"K": "V"}, "C": {"K": "V"}, "D": {"K": "V"}, "E": {"K": "V"}, "F": {"K": "V"}, "G": {"K": "V"}, "H": {"K": "V"}, "I": {"K": "V"}, "J": {"K": "V"}, "K": {"K": "V"}, "L": {"K": "V"}, "M": {"K": "V"}, "N": {"K": "V"}, "O": {"K": "V"}, "P": {"K": "V"}, "Q": {"K": "V"}, "R": {"K": "V"}, "S": {"K": "V"}, "T": {"K": "V"}, "U": {"K": "V"}, "V": {"K": "V"}, "W": {"K": "V"}, "X": {"K": "V"}, "Y": {"K": "V"}, "Z": {"K": "V"}}),
new: func() any { return new(map[string]map[string]string) },
}, {
name: "Struct/ManyEmpty",
raw: []byte(`[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]`),
val: addr(make([]struct{}, 100)),
new: func() any {
return new([]struct{})
},
}, {
name: "Struct/OneLarge",
raw: []byte(`{"A":"A","B":"B","C":"C","D":"D","E":"E","F":"F","G":"G","H":"H","I":"I","J":"J","K":"K","L":"L","M":"M","N":"N","O":"O","P":"P","Q":"Q","R":"R","S":"S","T":"T","U":"U","V":"V","W":"W","X":"X","Y":"Y","Z":"Z"}`),
val: addr(struct{ A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z string }{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}),
new: func() any {
return new(struct{ A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z string })
},
}, {
name: "Struct/ManySmall",
raw: []byte(`{"A":{"K":"V"},"B":{"K":"V"},"C":{"K":"V"},"D":{"K":"V"},"E":{"K":"V"},"F":{"K":"V"},"G":{"K":"V"},"H":{"K":"V"},"I":{"K":"V"},"J":{"K":"V"},"K":{"K":"V"},"L":{"K":"V"},"M":{"K":"V"},"N":{"K":"V"},"O":{"K":"V"},"P":{"K":"V"},"Q":{"K":"V"},"R":{"K":"V"},"S":{"K":"V"},"T":{"K":"V"},"U":{"K":"V"},"V":{"K":"V"},"W":{"K":"V"},"X":{"K":"V"},"Y":{"K":"V"},"Z":{"K":"V"}}`),
val: func() any {
V := struct{ K string }{"V"}
return addr(struct{ A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z struct{ K string } }{
V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V,
})
}(),
new: func() any {
return new(struct{ A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z struct{ K string } })
},
}, {
name: "Slice/ManyEmpty",
raw: []byte(`[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]`),
val: addr(func() (out [][]string) {
for i := 0; i < 100; i++ {
out = append(out, []string{})
}
return out
}()),
new: func() any { return new([][]string) },
}, {
name: "Slice/OneLarge",
raw: []byte(`["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]`),
val: addr([]string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}),
new: func() any { return new([]string) },
}, {
name: "Slice/ManySmall",
raw: []byte(`[["A"],["B"],["C"],["D"],["E"],["F"],["G"],["H"],["I"],["J"],["K"],["L"],["M"],["N"],["O"],["P"],["Q"],["R"],["S"],["T"],["U"],["V"],["W"],["X"],["Y"],["Z"]]`),
val: addr([][]string{{"A"}, {"B"}, {"C"}, {"D"}, {"E"}, {"F"}, {"G"}, {"H"}, {"I"}, {"J"}, {"K"}, {"L"}, {"M"}, {"N"}, {"O"}, {"P"}, {"Q"}, {"R"}, {"S"}, {"T"}, {"U"}, {"V"}, {"W"}, {"X"}, {"Y"}, {"Z"}}),
new: func() any { return new([][]string) },
}, {
name: "Array/OneLarge",
raw: []byte(`["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]`),
val: addr([26]string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}),
new: func() any { return new([26]string) },
}, {
name: "Array/ManySmall",
raw: []byte(`[["A"],["B"],["C"],["D"],["E"],["F"],["G"],["H"],["I"],["J"],["K"],["L"],["M"],["N"],["O"],["P"],["Q"],["R"],["S"],["T"],["U"],["V"],["W"],["X"],["Y"],["Z"]]`),
val: addr([26][1]string{{"A"}, {"B"}, {"C"}, {"D"}, {"E"}, {"F"}, {"G"}, {"H"}, {"I"}, {"J"}, {"K"}, {"L"}, {"M"}, {"N"}, {"O"}, {"P"}, {"Q"}, {"R"}, {"S"}, {"T"}, {"U"}, {"V"}, {"W"}, {"X"}, {"Y"}, {"Z"}}),
new: func() any { return new([26][1]string) },
}, {
name: "Bytes/Slice",
raw: []byte(`"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="`),
val: addr([]byte{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}),
new: func() any { return new([]byte) },
}, {
name: "Bytes/Array",
raw: []byte(`"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="`),
val: addr([32]byte{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}),
new: func() any { return new([32]byte) },
skipV1: true,
}, {
name: "Pointer",
raw: []byte("true"),
val: addr(addr(addr(addr(addr(addr(addr(addr(addr(addr(addr(true))))))))))),
new: func() any { return new(**********bool) },
}, {
name: "TextArshal",
raw: []byte(`"method"`),
val: new(textArshaler),
new: func() any { return new(textArshaler) },
}, {
name: "JSONArshalV1",
raw: []byte(`"method"`),
val: new(jsonArshalerV1),
new: func() any { return new(jsonArshalerV1) },
}, {
name: "JSONArshalV2",
raw: []byte(`"method"`),
val: new(jsonArshalerV2),
new: func() any { return new(jsonArshalerV2) },
skipV1: true,
}}
type textArshaler struct{}
func (*textArshaler) MarshalText() ([]byte, error) {
return []byte("method"), nil
}
func (*textArshaler) UnmarshalText(b []byte) error {
if string(b) != "method" {
return fmt.Errorf("UnmarshalText: got %q, want %q", b, "method")
}
return nil
}
type jsonArshalerV1 struct{}
func (*jsonArshalerV1) MarshalJSON() ([]byte, error) {
return []byte(`"method"`), nil
}
func (*jsonArshalerV1) UnmarshalJSON(b []byte) error {
if string(b) != `"method"` {
return fmt.Errorf("UnmarshalJSON: got %q, want %q", b, `"method"`)
}
return nil
}
type jsonArshalerV2 struct{}
func (*jsonArshalerV2) MarshalNextJSON(opts MarshalOptions, enc *Encoder) error {
return enc.WriteToken(String("method"))
}
func (*jsonArshalerV2) UnmarshalNextJSON(opts UnmarshalOptions, dec *Decoder) error {
b, err := dec.ReadValue()
if string(b) != `"method"` {
return fmt.Errorf("UnmarshalNextJSON: got %q, want %q", b, `"method"`)
}
return err
}
func TestBenchmarkUnmarshal(t *testing.T) { runUnmarshal(t) }
func BenchmarkUnmarshal(b *testing.B) { runUnmarshal(b) }
func runUnmarshal(tb testing.TB) {
for _, tt := range arshalTestdata {
// Setup the unmarshal operation.
var val any
run := func(tb testing.TB) {
val = tt.new()
if err := Unmarshal(tt.raw, val); err != nil {
tb.Fatalf("Unmarshal error: %v", err)
}
}
if benchV1 {
run = func(tb testing.TB) {
if tt.skipV1 {
tb.Skip("not supported in v1")
}
val = tt.new()
if err := jsonv1.Unmarshal(tt.raw, val); err != nil {
tb.Fatalf("Unmarshal error: %v", err)
}
}
}
// Verify the results.
if _, ok := tb.(*testing.T); ok {
run0 := run
run = func(tb testing.TB) {
run0(tb)
if !reflect.DeepEqual(val, tt.val) {
tb.Fatalf("Unmarshal output mismatch:\ngot %v\nwant %v", val, tt.val)
}
}
}
runTestOrBench(tb, tt.name, int64(len(tt.raw)), run)
}
}
func TestBenchmarkMarshal(t *testing.T) { runMarshal(t) }
func BenchmarkMarshal(b *testing.B) { runMarshal(b) }
func runMarshal(tb testing.TB) {
for _, tt := range arshalTestdata {
// Setup the marshal operation.
var raw []byte
run := func(tb testing.TB) {
var err error
raw, err = Marshal(tt.val)
if err != nil {
tb.Fatalf("Marshal error: %v", err)
}
}
if benchV1 {
run = func(tb testing.TB) {
if tt.skipV1 {
tb.Skip("not supported in v1")
}
var err error
raw, err = jsonv1.Marshal(tt.val)
if err != nil {
tb.Fatalf("Marshal error: %v", err)
}
}
}
// Verify the results.
if _, ok := tb.(*testing.T); ok {
run0 := run
run = func(tb testing.TB) {
run0(tb)
if !bytes.Equal(raw, tt.raw) {
// Map marshaling in v2 is non-deterministic.
byteHistogram := func(b []byte) (h [256]int) {
for _, c := range b {
h[c]++
}
return h
}
if !(strings.HasPrefix(tt.name, "Map/") && byteHistogram(raw) == byteHistogram(tt.raw)) {
tb.Fatalf("Marshal output mismatch:\ngot %s\nwant %s", raw, tt.raw)
}
}
}
}
runTestOrBench(tb, tt.name, int64(len(tt.raw)), run)
}
}
func TestBenchmarkTestdata(t *testing.T) { runAllTestdata(t) }
func BenchmarkTestdata(b *testing.B) { runAllTestdata(b) }
func runAllTestdata(tb testing.TB) {
for _, td := range jsonTestdata() {
for _, arshalName := range []string{"Marshal", "Unmarshal"} {
for _, typeName := range []string{"Concrete", "Interface"} {
newValue := func() any { return new(any) }
if typeName == "Concrete" {
if td.new == nil {
continue
}
newValue = td.new
}
value := mustUnmarshalValue(tb, td.data, newValue)
name := path.Join(td.name, arshalName, typeName)
runTestOrBench(tb, name, int64(len(td.data)), func(tb testing.TB) {
runArshal(tb, arshalName, newValue, td.data, value)
})
}
}
tokens := mustDecodeTokens(tb, td.data)
buffer := make([]byte, 0, 2*len(td.data))
for _, codeName := range []string{"Encode", "Decode"} {
for _, typeName := range []string{"Token", "Value"} {
for _, modeName := range []string{"Streaming", "Buffered"} {
name := path.Join(td.name, codeName, typeName, modeName)
runTestOrBench(tb, name, int64(len(td.data)), func(tb testing.TB) {
runCode(tb, codeName, typeName, modeName, buffer, td.data, tokens)
})
}
}
}
}
}
func mustUnmarshalValue(t testing.TB, data []byte, newValue func() any) (value any) {
value = newValue()
if err := Unmarshal(data, value); err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
return value
}
func runArshal(t testing.TB, arshalName string, newValue func() any, data []byte, value any) {
if benchV1 {
switch arshalName {
case "Marshal":
if _, err := jsonv1.Marshal(value); err != nil {
t.Fatalf("Marshal error: %v", err)
}
case "Unmarshal":
if err := jsonv1.Unmarshal(data, newValue()); err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
}
return
}
switch arshalName {
case "Marshal":
if _, err := Marshal(value); err != nil {
t.Fatalf("Marshal error: %v", err)
}
case "Unmarshal":
if err := Unmarshal(data, newValue()); err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
}
}
func mustDecodeTokens(t testing.TB, data []byte) []Token {
var tokens []Token
dec := NewDecoder(bytes.NewReader(data))
for {
tok, err := dec.ReadToken()
if err != nil {
if err == io.EOF {
break
}
t.Fatalf("Decoder.ReadToken error: %v", err)
}
// Prefer exact representation for JSON strings and numbers
// since this more closely matches common use cases.
switch tok.Kind() {
case '"':
tokens = append(tokens, String(tok.String()))
case '0':
tokens = append(tokens, Float(tok.Float()))
default:
tokens = append(tokens, tok.Clone())
}
}
return tokens
}
func runCode(t testing.TB, codeName, typeName, modeName string, buffer, data []byte, tokens []Token) {
switch codeName {
case "Encode":
runEncode(t, typeName, modeName, buffer, data, tokens)
case "Decode":
runDecode(t, typeName, modeName, buffer, data, tokens)
}
}
func runEncode(t testing.TB, typeName, modeName string, buffer, data []byte, tokens []Token) {
if benchV1 {
if modeName == "Buffered" {
t.Skip("no support for direct buffered input in v1")
}
enc := jsonv1.NewEncoder(bytes.NewBuffer(buffer[:0]))
switch typeName {
case "Token":
t.Skip("no support for encoding tokens in v1; see https://go.dev/issue/40127")
case "Value":
val := jsonv1.RawMessage(data)
if err := enc.Encode(val); err != nil {
t.Fatalf("Decoder.Encode error: %v", err)
}
}
return
}
var enc *Encoder
switch modeName {
case "Streaming":
enc = NewEncoder(bytesBuffer{bytes.NewBuffer(buffer[:0])})
case "Buffered":
enc = NewEncoder(bytes.NewBuffer(buffer[:0]))
}
switch typeName {
case "Token":
for _, tok := range tokens {
if err := enc.WriteToken(tok); err != nil {
t.Fatalf("Encoder.WriteToken error: %v", err)
}
}
case "Value":
if err := enc.WriteValue(data); err != nil {
t.Fatalf("Encoder.WriteValue error: %v", err)
}
}
}
func runDecode(t testing.TB, typeName, modeName string, buffer, data []byte, tokens []Token) {
if benchV1 {
if modeName == "Buffered" {
t.Skip("no support for direct buffered output in v1")
}
dec := jsonv1.NewDecoder(bytes.NewReader(data))
switch typeName {
case "Token":
for {
if _, err := dec.Token(); err != nil {
if err == io.EOF {
break
}
t.Fatalf("Decoder.Token error: %v", err)
}
}
case "Value":
var val jsonv1.RawMessage
if err := dec.Decode(&val); err != nil {
t.Fatalf("Decoder.Decode error: %v", err)
}
}
return
}
var dec *Decoder
switch modeName {
case "Streaming":
dec = NewDecoder(bytesBuffer{bytes.NewBuffer(data)})
case "Buffered":
dec = NewDecoder(bytes.NewBuffer(data))
}
switch typeName {
case "Token":
for {
if _, err := dec.ReadToken(); err != nil {
if err == io.EOF {
break
}
t.Fatalf("Decoder.ReadToken error: %v", err)
}
}
case "Value":
if _, err := dec.ReadValue(); err != nil {
t.Fatalf("Decoder.ReadValue error: %v", err)
}
}
}
var ws = strings.Repeat(" ", 4<<10)
var slowStreamingDecodeTestdata = []struct {
name string
data []byte
}{
{"LargeString", []byte(`"` + strings.Repeat(" ", 4<<10) + `"`)},
{"LargeNumber", []byte("0." + strings.Repeat("0", 4<<10))},
{"LargeWhitespace/Null", []byte(ws + "null" + ws)},
{"LargeWhitespace/Object", []byte(ws + "{" + ws + `"name1"` + ws + ":" + ws + `"value"` + ws + "," + ws + `"name2"` + ws + ":" + ws + `"value"` + ws + "}" + ws)},
{"LargeWhitespace/Array", []byte(ws + "[" + ws + `"value"` + ws + "," + ws + `"value"` + ws + "]" + ws)},
}
func TestBenchmarkSlowStreamingDecode(t *testing.T) { runAllSlowStreamingDecode(t) }
func BenchmarkSlowStreamingDecode(b *testing.B) { runAllSlowStreamingDecode(b) }
func runAllSlowStreamingDecode(tb testing.TB) {
for _, td := range slowStreamingDecodeTestdata {
for _, typeName := range []string{"Token", "Value"} {
name := path.Join(td.name, typeName)
runTestOrBench(tb, name, int64(len(td.data)), func(tb testing.TB) {
runSlowStreamingDecode(tb, typeName, td.data)
})
}
}
}
// runSlowStreamingDecode tests a streaming Decoder operating on
// a slow io.Reader that only returns 1 byte at a time,
// which tends to exercise pathological behavior.
func runSlowStreamingDecode(t testing.TB, typeName string, data []byte) {
if benchV1 {
dec := jsonv1.NewDecoder(iotest.OneByteReader(bytes.NewReader(data)))
switch typeName {
case "Token":
for dec.More() {
if _, err := dec.Token(); err != nil {
t.Fatalf("Decoder.Token error: %v", err)
}
}
case "Value":
var val jsonv1.RawMessage
if err := dec.Decode(&val); err != nil {
t.Fatalf("Decoder.Decode error: %v", err)
}
}
return
}
dec := NewDecoder(iotest.OneByteReader(bytes.NewReader(data)))
switch typeName {
case "Token":
for dec.PeekKind() > 0 {
if _, err := dec.ReadToken(); err != nil {
t.Fatalf("Decoder.ReadToken error: %v", err)
}
}
case "Value":
if _, err := dec.ReadValue(); err != nil {
t.Fatalf("Decoder.ReadValue error: %v", err)
}
}
}
func TestBenchmarkRawValue(t *testing.T) { runRawValue(t) }
func BenchmarkRawValue(b *testing.B) { runRawValue(b) }
func runRawValue(tb testing.TB) {
if testing.Short() {
tb.Skip() // CitmCatalog is not loaded in short mode
}
var data []byte
for _, ts := range jsonTestdata() {
if ts.name == "CitmCatalog" {
data = ts.data
}
}
runTestOrBench(tb, "IsValid", int64(len(data)), func(tb testing.TB) {
RawValue(data).IsValid()
})
methods := []struct {
name string
format func(*RawValue) error
}{
{"Compact", (*RawValue).Compact},
{"Indent", func(v *RawValue) error { return v.Indent("\t", " ") }},
{"Canonicalize", (*RawValue).Canonicalize},
}
var v RawValue
for _, method := range methods {
runTestOrBench(tb, method.name, int64(len(data)), func(tb testing.TB) {
v = append(v[:0], data...) // reset with original input
if err := method.format(&v); err != nil {
tb.Errorf("RawValue.%v error: %v", method.name, err)
}
})
v = append(v[:0], data...)
method.format(&v)
runTestOrBench(tb, method.name+"/Noop", int64(len(data)), func(tb testing.TB) {
if err := method.format(&v); err != nil {
tb.Errorf("RawValue.%v error: %v", method.name, err)
}
})
}
}
func runTestOrBench(tb testing.TB, name string, numBytes int64, run func(tb testing.TB)) {
switch tb := tb.(type) {
case *testing.T:
tb.Run(name, func(t *testing.T) {
run(t)
})
case *testing.B:
tb.Run(name, func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(numBytes)
for i := 0; i < b.N; i++ {
run(b)
}
})
}
}
|