mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-01 03:14:16 +00:00
ec00e32a8d
Remove support for running benchmarks with APIv1. The comparisons have served their purpose, and this removes the last dependency on the github.com/golang/protobuf module. Fixes golang/protobuf#962. Change-Id: I55758e19451fcd16ab1a5d66244eb8214ceb9fa7 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/214040 Reviewed-by: Joe Tsai <joetsai@google.com>
62 lines
1.6 KiB
Go
62 lines
1.6 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"
|
|
|
|
"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 (
|
|
allowPartial = flag.Bool("allow_partial", false, "set AllowPartial")
|
|
)
|
|
|
|
// BenchmarkEncode benchmarks encoding all the test messages.
|
|
func BenchmarkEncode(b *testing.B) {
|
|
for _, test := range testValidMessages {
|
|
for _, want := range test.decodeTo {
|
|
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() {
|
|
_, err := opts.Marshal(want)
|
|
if err != nil && !test.partial {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// BenchmarkDecode benchmarks decoding all the test messages.
|
|
func BenchmarkDecode(b *testing.B) {
|
|
for _, test := range testValidMessages {
|
|
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) {
|
|
for pb.Next() {
|
|
m := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
|
|
err := opts.Unmarshal(test.wire, m)
|
|
if err != nil && !test.partial {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|