bench_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package json_test
  5. import (
  6. "testing"
  7. "google.golang.org/protobuf/internal/encoding/json"
  8. )
  9. func BenchmarkFloat(b *testing.B) {
  10. input := []byte(`1.797693134862315708145274237317043567981e+308`)
  11. for i := 0; i < b.N; i++ {
  12. dec := json.NewDecoder(input)
  13. val, err := dec.Read()
  14. if err != nil {
  15. b.Fatal(err)
  16. }
  17. _, err = val.Float(64)
  18. if err != nil {
  19. b.Fatal(err)
  20. }
  21. }
  22. }
  23. func BenchmarkInt(b *testing.B) {
  24. input := []byte(`922337203.6854775807e+10`)
  25. for i := 0; i < b.N; i++ {
  26. dec := json.NewDecoder(input)
  27. val, err := dec.Read()
  28. if err != nil {
  29. b.Fatal(err)
  30. }
  31. _, err = val.Int(64)
  32. if err != nil {
  33. b.Fatal(err)
  34. }
  35. }
  36. }
  37. func BenchmarkString(b *testing.B) {
  38. input := []byte(`"abcdefghijklmnopqrstuvwxyz0123456789\\n\\t"`)
  39. for i := 0; i < b.N; i++ {
  40. dec := json.NewDecoder(input)
  41. val, err := dec.Read()
  42. if err != nil {
  43. b.Fatal(err)
  44. }
  45. _ = val.String()
  46. }
  47. }
  48. func BenchmarkBool(b *testing.B) {
  49. input := []byte(`true`)
  50. for i := 0; i < b.N; i++ {
  51. dec := json.NewDecoder(input)
  52. val, err := dec.Read()
  53. if err != nil {
  54. b.Fatal(err)
  55. }
  56. _, err = val.Bool()
  57. if err != nil {
  58. b.Fatal(err)
  59. }
  60. }
  61. }