bench_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. if _, ok := val.Float(64); !ok {
  18. b.Fatal("not a float")
  19. }
  20. }
  21. }
  22. func BenchmarkInt(b *testing.B) {
  23. input := []byte(`922337203.6854775807e+10`)
  24. for i := 0; i < b.N; i++ {
  25. dec := json.NewDecoder(input)
  26. val, err := dec.Read()
  27. if err != nil {
  28. b.Fatal(err)
  29. }
  30. if _, ok := val.Int(64); !ok {
  31. b.Fatal("not an int64")
  32. }
  33. }
  34. }
  35. func BenchmarkString(b *testing.B) {
  36. input := []byte(`"abcdefghijklmnopqrstuvwxyz0123456789\\n\\t"`)
  37. for i := 0; i < b.N; i++ {
  38. dec := json.NewDecoder(input)
  39. val, err := dec.Read()
  40. if err != nil {
  41. b.Fatal(err)
  42. }
  43. _ = val.ParsedString()
  44. }
  45. }
  46. func BenchmarkBool(b *testing.B) {
  47. input := []byte(`true`)
  48. for i := 0; i < b.N; i++ {
  49. dec := json.NewDecoder(input)
  50. val, err := dec.Read()
  51. if err != nil {
  52. b.Fatal(err)
  53. }
  54. _ = val.Bool()
  55. }
  56. }