1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2010 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 "github.com/golang/protobuf/proto"
41 tpb "github.com/golang/protobuf/proto/proto3_proto"
46 msgBlackhole = new(tpb.Message)
49 // BenchmarkVarint32ArraySmall shows the performance on an array of small int32 fields (1 and
51 func BenchmarkVarint32ArraySmall(b *testing.B) {
52 for i := uint(1); i <= 10; i++ {
53 dist := genInt32Dist([7]int{0, 3, 1}, 1<<i)
54 raw, err := proto.Marshal(&tpb.Message{
58 b.Error("wrong encode", err)
60 b.Run(fmt.Sprintf("Len%v", len(dist)), func(b *testing.B) {
61 scratchBuf := proto.NewBuffer(nil)
63 for k := 0; k < b.N; k++ {
64 scratchBuf.SetBuf(raw)
66 if err := scratchBuf.Unmarshal(msgBlackhole); err != nil {
67 b.Error("wrong decode", err)
74 // BenchmarkVarint32ArrayLarge shows the performance on an array of large int32 fields (3 and
75 // 4 bytes long, with a small number of 1, 2, 5 and 10 byte long versions).
76 func BenchmarkVarint32ArrayLarge(b *testing.B) {
77 for i := uint(1); i <= 10; i++ {
78 dist := genInt32Dist([7]int{0, 1, 2, 4, 8, 1, 1}, 1<<i)
79 raw, err := proto.Marshal(&tpb.Message{
83 b.Error("wrong encode", err)
85 b.Run(fmt.Sprintf("Len%v", len(dist)), func(b *testing.B) {
86 scratchBuf := proto.NewBuffer(nil)
88 for k := 0; k < b.N; k++ {
89 scratchBuf.SetBuf(raw)
91 if err := scratchBuf.Unmarshal(msgBlackhole); err != nil {
92 b.Error("wrong decode", err)
99 // BenchmarkVarint64ArraySmall shows the performance on an array of small int64 fields (1 and
101 func BenchmarkVarint64ArraySmall(b *testing.B) {
102 for i := uint(1); i <= 10; i++ {
103 dist := genUint64Dist([11]int{0, 3, 1}, 1<<i)
104 raw, err := proto.Marshal(&tpb.Message{
108 b.Error("wrong encode", err)
110 b.Run(fmt.Sprintf("Len%v", len(dist)), func(b *testing.B) {
111 scratchBuf := proto.NewBuffer(nil)
113 for k := 0; k < b.N; k++ {
114 scratchBuf.SetBuf(raw)
116 if err := scratchBuf.Unmarshal(msgBlackhole); err != nil {
117 b.Error("wrong decode", err)
124 // BenchmarkVarint64ArrayLarge shows the performance on an array of large int64 fields (6, 7,
125 // and 8 bytes long with a small number of the other sizes).
126 func BenchmarkVarint64ArrayLarge(b *testing.B) {
127 for i := uint(1); i <= 10; i++ {
128 dist := genUint64Dist([11]int{0, 1, 1, 2, 4, 8, 16, 32, 16, 1, 1}, 1<<i)
129 raw, err := proto.Marshal(&tpb.Message{
133 b.Error("wrong encode", err)
135 b.Run(fmt.Sprintf("Len%v", len(dist)), func(b *testing.B) {
136 scratchBuf := proto.NewBuffer(nil)
138 for k := 0; k < b.N; k++ {
139 scratchBuf.SetBuf(raw)
141 if err := scratchBuf.Unmarshal(msgBlackhole); err != nil {
142 b.Error("wrong decode", err)
149 // BenchmarkVarint64ArrayMixed shows the performance of lots of small messages, each
150 // containing a small number of large (3, 4, and 5 byte) repeated int64s.
151 func BenchmarkVarint64ArrayMixed(b *testing.B) {
152 for i := uint(1); i <= 1<<5; i <<= 1 {
153 dist := genUint64Dist([11]int{0, 0, 0, 4, 6, 4, 0, 0, 0, 0, 0}, int(i))
154 // number of sub fields
155 for k := uint(1); k <= 1<<10; k <<= 2 {
156 msg := &tpb.Message{}
157 for m := uint(0); m < k; m++ {
158 msg.Children = append(msg.Children, &tpb.Message{
162 raw, err := proto.Marshal(msg)
164 b.Error("wrong encode", err)
166 b.Run(fmt.Sprintf("Fields%vLen%v", k, i), func(b *testing.B) {
167 scratchBuf := proto.NewBuffer(nil)
169 for k := 0; k < b.N; k++ {
170 scratchBuf.SetBuf(raw)
172 if err := scratchBuf.Unmarshal(msgBlackhole); err != nil {
173 b.Error("wrong decode", err)
181 // genInt32Dist generates a slice of ints that will match the size distribution of dist.
182 // A size of 6 corresponds to a max length varint32, which is 10 bytes. The distribution
183 // is 1-indexed. (i.e. the value at index 1 is how many 1 byte ints to create).
184 func genInt32Dist(dist [7]int, count int) (dest []int32) {
185 for i := 0; i < count; i++ {
186 for k := 0; k < len(dist); k++ {
202 for m := 0; m < dist[k]; m++ {
203 dest = append(dest, num)
210 // genUint64Dist generates a slice of ints that will match the size distribution of dist.
211 // The distribution is 1-indexed. (i.e. the value at index 1 is how many 1 byte ints to create).
212 func genUint64Dist(dist [11]int, count int) (dest []uint64) {
213 for i := 0; i < count; i++ {
214 for k := 0; k < len(dist); k++ {
238 for m := 0; m < dist[k]; m++ {
239 dest = append(dest, num)
246 // BenchmarkDecodeEmpty measures the overhead of doing the minimal possible decode.
247 func BenchmarkDecodeEmpty(b *testing.B) {
248 raw, err := proto.Marshal(&tpb.Message{})
250 b.Error("wrong encode", err)
253 for i := 0; i < b.N; i++ {
254 if err := proto.Unmarshal(raw, msgBlackhole); err != nil {
255 b.Error("wrong decode", err)