...

Source file src/google.golang.org/protobuf/internal/encoding/json/bench_test.go

Documentation: google.golang.org/protobuf/internal/encoding/json

     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  
     5  package json_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"google.golang.org/protobuf/internal/encoding/json"
    11  )
    12  
    13  func BenchmarkFloat(b *testing.B) {
    14  	input := []byte(`1.797693134862315708145274237317043567981e+308`)
    15  	for i := 0; i < b.N; i++ {
    16  		dec := json.NewDecoder(input)
    17  		val, err := dec.Read()
    18  		if err != nil {
    19  			b.Fatal(err)
    20  		}
    21  		if _, ok := val.Float(64); !ok {
    22  			b.Fatal("not a float")
    23  		}
    24  	}
    25  }
    26  
    27  func BenchmarkInt(b *testing.B) {
    28  	input := []byte(`922337203.6854775807e+10`)
    29  	for i := 0; i < b.N; i++ {
    30  		dec := json.NewDecoder(input)
    31  		val, err := dec.Read()
    32  		if err != nil {
    33  			b.Fatal(err)
    34  		}
    35  		if _, ok := val.Int(64); !ok {
    36  			b.Fatal("not an int64")
    37  		}
    38  	}
    39  }
    40  
    41  func BenchmarkString(b *testing.B) {
    42  	input := []byte(`"abcdefghijklmnopqrstuvwxyz0123456789\\n\\t"`)
    43  	for i := 0; i < b.N; i++ {
    44  		dec := json.NewDecoder(input)
    45  		val, err := dec.Read()
    46  		if err != nil {
    47  			b.Fatal(err)
    48  		}
    49  		_ = val.ParsedString()
    50  	}
    51  }
    52  
    53  func BenchmarkBool(b *testing.B) {
    54  	input := []byte(`true`)
    55  	for i := 0; i < b.N; i++ {
    56  		dec := json.NewDecoder(input)
    57  		val, err := dec.Read()
    58  		if err != nil {
    59  			b.Fatal(err)
    60  		}
    61  		_ = val.Bool()
    62  	}
    63  }
    64  

View as plain text