protobuf-go/proto/bench_test.go
Kir Kolyshkin bf9455640d all: fix typos
Brought to you by codespell v2.1.0, using the command

	codespell -S .cache,vendor -L ot,ba,fo,unparseable -w

Note that the misspelled "unparseable" comes from the
github.com/protocolbuffers/protobuf, where it is explicitly ignored
(see [1] and some explanation at [2]), so we ignore it here, too.

[1] https://github.com/protocolbuffers/protobuf/pull/7752
[2] https://github.com/protocolbuffers/protobuf/pull/7751#discussion_r460170422

Change-Id: Ie1ca705db4f11df8ec8b22fdc22b6a6ee667ae5b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/406845
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
2022-05-19 09:32:38 +00:00

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 performance. 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)
}
}
})
})
}
}
}