mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-01 03:14:16 +00:00
42cfff4a76
Move the benchmarks using the common protobuf datasets out of proto/ and into their own directory. Add benchmarks for text and JSON. Move initialization out of the Benchmark function to avoid including it in CPU/memory profiles. We could put benchmarks in each individual package (proto, prototext, etc.), but the need for common infrastructure around managing the test data makes it simpler to keep the benchmarks together. Also, it's nice to have a one-stop overview of performance. Change-Id: I17c37efb91b2413fc43ab1b4c35bff2e1330bc0a Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/183245 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
// 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 proto_test
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
|
|
protoV1 "github.com/golang/protobuf/proto"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// The results of these microbenchmarks are unlikely to correspond well
|
|
// to real world peformance. They are mainly useful as a quick check to
|
|
// detect unexpected regressions and for profiling specific cases.
|
|
|
|
var (
|
|
benchV1 = flag.Bool("v1", false, "benchmark the v1 implementation")
|
|
allowPartial = flag.Bool("allow_partial", false, "set AllowPartial")
|
|
)
|
|
|
|
// BenchmarkEncode benchmarks encoding all the test messages.
|
|
func BenchmarkEncode(b *testing.B) {
|
|
for _, test := range testProtos {
|
|
for _, want := range test.decodeTo {
|
|
v1 := want.(protoV1.Message)
|
|
opts := proto.MarshalOptions{AllowPartial: *allowPartial}
|
|
b.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(b *testing.B) {
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
for pb.Next() {
|
|
var err error
|
|
if *benchV1 {
|
|
_, err = protoV1.Marshal(v1)
|
|
} else {
|
|
_, err = opts.Marshal(want)
|
|
}
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// BenchmarkDecode benchmarks decoding all the test messages.
|
|
func BenchmarkDecode(b *testing.B) {
|
|
for _, test := range testProtos {
|
|
for _, want := range test.decodeTo {
|
|
opts := proto.UnmarshalOptions{AllowPartial: *allowPartial}
|
|
b.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(b *testing.B) {
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
m := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
|
|
v1 := m.(protoV1.Message)
|
|
for pb.Next() {
|
|
var err error
|
|
if *benchV1 {
|
|
err = protoV1.Unmarshal(test.wire, v1)
|
|
} else {
|
|
err = opts.Unmarshal(test.wire, m)
|
|
}
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|