2019-03-21 18:12:26 -07:00
|
|
|
// Copyright 2019 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_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-05-13 23:55:40 -07:00
|
|
|
"google.golang.org/protobuf/internal/encoding/json"
|
2019-03-21 18:12:26 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkFloat(b *testing.B) {
|
2019-03-31 19:10:33 -07:00
|
|
|
input := []byte(`1.797693134862315708145274237317043567981e+308`)
|
2019-03-21 18:12:26 -07:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
dec := json.NewDecoder(input)
|
|
|
|
val, err := dec.Read()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2020-01-07 16:45:24 -08:00
|
|
|
if _, ok := val.Float(64); !ok {
|
2022-05-17 17:29:33 -07:00
|
|
|
b.Fatal("not a float")
|
2019-03-21 18:12:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkInt(b *testing.B) {
|
2019-03-31 19:10:33 -07:00
|
|
|
input := []byte(`922337203.6854775807e+10`)
|
2019-03-21 18:12:26 -07:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
dec := json.NewDecoder(input)
|
|
|
|
val, err := dec.Read()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2020-01-07 16:45:24 -08:00
|
|
|
if _, ok := val.Int(64); !ok {
|
|
|
|
b.Fatal("not an int64")
|
2019-03-21 18:12:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-31 19:10:33 -07:00
|
|
|
|
|
|
|
func BenchmarkString(b *testing.B) {
|
|
|
|
input := []byte(`"abcdefghijklmnopqrstuvwxyz0123456789\\n\\t"`)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
dec := json.NewDecoder(input)
|
|
|
|
val, err := dec.Read()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2020-01-07 16:45:24 -08:00
|
|
|
_ = val.ParsedString()
|
2019-03-31 19:10:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkBool(b *testing.B) {
|
|
|
|
input := []byte(`true`)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
dec := json.NewDecoder(input)
|
|
|
|
val, err := dec.Read()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2020-01-07 16:45:24 -08:00
|
|
|
_ = val.Bool()
|
2019-03-31 19:10:33 -07:00
|
|
|
}
|
|
|
|
}
|