...
1
2
3
4
5 package proto_test
6
7 import (
8 "flag"
9 "fmt"
10 "reflect"
11 "testing"
12
13 "google.golang.org/protobuf/proto"
14 )
15
16
17
18
19
20 var (
21 allowPartial = flag.Bool("allow_partial", false, "set AllowPartial")
22 )
23
24
25 func BenchmarkEncode(b *testing.B) {
26 for _, test := range testValidMessages {
27 for _, want := range test.decodeTo {
28 opts := proto.MarshalOptions{AllowPartial: *allowPartial}
29 b.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(b *testing.B) {
30 b.RunParallel(func(pb *testing.PB) {
31 for pb.Next() {
32 _, err := opts.Marshal(want)
33 if err != nil && !test.partial {
34 b.Fatal(err)
35 }
36 }
37 })
38 })
39 }
40 }
41 }
42
43
44 func BenchmarkDecode(b *testing.B) {
45 for _, test := range testValidMessages {
46 for _, want := range test.decodeTo {
47 opts := proto.UnmarshalOptions{AllowPartial: *allowPartial}
48 b.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(b *testing.B) {
49 b.RunParallel(func(pb *testing.PB) {
50 for pb.Next() {
51 m := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
52 err := opts.Unmarshal(test.wire, m)
53 if err != nil && !test.partial {
54 b.Fatal(err)
55 }
56 }
57 })
58 })
59 }
60 }
61 }
62
View as plain text