Damien Neil | 53b05a5 | 2019-04-08 07:56:05 -0700 | [diff] [blame] | 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 proto_test |
| 6 | |
| 7 | import ( |
| 8 | "flag" |
| 9 | "fmt" |
| 10 | "reflect" |
| 11 | "testing" |
| 12 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 13 | "google.golang.org/protobuf/proto" |
Damien Neil | 53b05a5 | 2019-04-08 07:56:05 -0700 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | // The results of these microbenchmarks are unlikely to correspond well |
Kir Kolyshkin | bf94556 | 2022-05-17 17:29:33 -0700 | [diff] [blame] | 17 | // to real world performance. They are mainly useful as a quick check to |
Damien Neil | 53b05a5 | 2019-04-08 07:56:05 -0700 | [diff] [blame] | 18 | // detect unexpected regressions and for profiling specific cases. |
| 19 | |
| 20 | var ( |
Damien Neil | 53b05a5 | 2019-04-08 07:56:05 -0700 | [diff] [blame] | 21 | allowPartial = flag.Bool("allow_partial", false, "set AllowPartial") |
| 22 | ) |
| 23 | |
| 24 | // BenchmarkEncode benchmarks encoding all the test messages. |
| 25 | func BenchmarkEncode(b *testing.B) { |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 26 | for _, test := range testValidMessages { |
Damien Neil | 53b05a5 | 2019-04-08 07:56:05 -0700 | [diff] [blame] | 27 | for _, want := range test.decodeTo { |
Damien Neil | 53b05a5 | 2019-04-08 07:56:05 -0700 | [diff] [blame] | 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() { |
Damien Neil | ec00e32 | 2020-01-09 09:23:52 -0800 | [diff] [blame] | 32 | _, err := opts.Marshal(want) |
Damien Neil | dd380ab | 2019-07-08 15:19:51 -0700 | [diff] [blame] | 33 | if err != nil && !test.partial { |
Damien Neil | 53b05a5 | 2019-04-08 07:56:05 -0700 | [diff] [blame] | 34 | b.Fatal(err) |
| 35 | } |
| 36 | } |
| 37 | }) |
| 38 | }) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // BenchmarkDecode benchmarks decoding all the test messages. |
| 44 | func BenchmarkDecode(b *testing.B) { |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 45 | for _, test := range testValidMessages { |
Damien Neil | 53b05a5 | 2019-04-08 07:56:05 -0700 | [diff] [blame] | 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) { |
Damien Neil | 53b05a5 | 2019-04-08 07:56:05 -0700 | [diff] [blame] | 50 | for pb.Next() { |
Damien Neil | dd380ab | 2019-07-08 15:19:51 -0700 | [diff] [blame] | 51 | m := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
Damien Neil | ec00e32 | 2020-01-09 09:23:52 -0800 | [diff] [blame] | 52 | err := opts.Unmarshal(test.wire, m) |
Damien Neil | dd380ab | 2019-07-08 15:19:51 -0700 | [diff] [blame] | 53 | if err != nil && !test.partial { |
Damien Neil | 53b05a5 | 2019-04-08 07:56:05 -0700 | [diff] [blame] | 54 | b.Fatal(err) |
| 55 | } |
| 56 | } |
| 57 | }) |
| 58 | }) |
| 59 | } |
| 60 | } |
| 61 | } |