decode_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  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
  15. // distribution.
  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.
  19. //
  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.
  31. package proto_test
  32. import (
  33. "fmt"
  34. "testing"
  35. "github.com/golang/protobuf/proto"
  36. tpb "github.com/golang/protobuf/proto/proto3_proto"
  37. )
  38. var (
  39. bytesBlackhole []byte
  40. msgBlackhole = new(tpb.Message)
  41. )
  42. func BenchmarkVarint32ArraySmall(b *testing.B) {
  43. for i := uint(1); i <= 10; i++ {
  44. dist := genInt32Dist([7]int{0, 3, 1}, 1<<i)
  45. raw, err := proto.Marshal(&tpb.Message{
  46. ShortKey: dist,
  47. })
  48. if err != nil {
  49. b.Error("wrong encode", err)
  50. }
  51. b.Run(fmt.Sprintf("Len%v", len(dist)), func(b *testing.B) {
  52. scratchBuf := proto.NewBuffer(nil)
  53. b.ResetTimer()
  54. for k := 0; k < b.N; k++ {
  55. scratchBuf.SetBuf(raw)
  56. msgBlackhole.Reset()
  57. if err := scratchBuf.Unmarshal(msgBlackhole); err != nil {
  58. b.Error("wrong decode", err)
  59. }
  60. }
  61. })
  62. }
  63. }
  64. func BenchmarkVarint32ArrayLarge(b *testing.B) {
  65. for i := uint(1); i <= 10; i++ {
  66. dist := genInt32Dist([7]int{0, 1, 2, 4, 8, 1, 1}, 1<<i)
  67. raw, err := proto.Marshal(&tpb.Message{
  68. ShortKey: dist,
  69. })
  70. if err != nil {
  71. b.Error("wrong encode", err)
  72. }
  73. b.Run(fmt.Sprintf("Len%v", len(dist)), func(b *testing.B) {
  74. scratchBuf := proto.NewBuffer(nil)
  75. b.ResetTimer()
  76. for k := 0; k < b.N; k++ {
  77. scratchBuf.SetBuf(raw)
  78. msgBlackhole.Reset()
  79. if err := scratchBuf.Unmarshal(msgBlackhole); err != nil {
  80. b.Error("wrong decode", err)
  81. }
  82. }
  83. })
  84. }
  85. }
  86. func BenchmarkVarint64ArraySmall(b *testing.B) {
  87. for i := uint(1); i <= 10; i++ {
  88. dist := genUint64Dist([11]int{0, 3, 1}, 1<<i)
  89. raw, err := proto.Marshal(&tpb.Message{
  90. Key: dist,
  91. })
  92. if err != nil {
  93. b.Error("wrong encode", err)
  94. }
  95. b.Run(fmt.Sprintf("Len%v", len(dist)), func(b *testing.B) {
  96. scratchBuf := proto.NewBuffer(nil)
  97. b.ResetTimer()
  98. for k := 0; k < b.N; k++ {
  99. scratchBuf.SetBuf(raw)
  100. msgBlackhole.Reset()
  101. if err := scratchBuf.Unmarshal(msgBlackhole); err != nil {
  102. b.Error("wrong decode", err)
  103. }
  104. }
  105. })
  106. }
  107. }
  108. func BenchmarkVarint64ArrayLarge(b *testing.B) {
  109. for i := uint(1); i <= 10; i++ {
  110. dist := genUint64Dist([11]int{0, 1, 1, 2, 4, 8, 16, 32, 16, 1, 1}, 1<<i)
  111. raw, err := proto.Marshal(&tpb.Message{
  112. Key: dist,
  113. })
  114. if err != nil {
  115. b.Error("wrong encode", err)
  116. }
  117. b.Run(fmt.Sprintf("Len%v", len(dist)), func(b *testing.B) {
  118. scratchBuf := proto.NewBuffer(nil)
  119. b.ResetTimer()
  120. for k := 0; k < b.N; k++ {
  121. scratchBuf.SetBuf(raw)
  122. msgBlackhole.Reset()
  123. if err := scratchBuf.Unmarshal(msgBlackhole); err != nil {
  124. b.Error("wrong decode", err)
  125. }
  126. }
  127. })
  128. }
  129. }
  130. // genInt32Dist generates a slice of ints that will match the size distribution of dist.
  131. // A size of 6 corresponds to a max length varint32, which is 10 bytes. The distribution
  132. // is 1-indexed. (i.e. the value at index 1 is how many 1 byte ints to create).
  133. func genInt32Dist(dist [7]int, count int) (dest []int32) {
  134. for i := 0; i < count; i++ {
  135. for k := 0; k < len(dist); k++ {
  136. var num int32
  137. switch k {
  138. case 1:
  139. num = 1<<7 - 1
  140. case 2:
  141. num = 1<<14 - 1
  142. case 3:
  143. num = 1<<21 - 1
  144. case 4:
  145. num = 1<<28 - 1
  146. case 5:
  147. num = 1<<29 - 1
  148. case 6:
  149. num = -1
  150. }
  151. for m := 0; m < dist[k]; m++ {
  152. dest = append(dest, num)
  153. }
  154. }
  155. }
  156. return
  157. }
  158. // genUint64Dist generates a slice of ints that will match the size distribution of dist.
  159. // The distribution is 1-indexed. (i.e. the value at index 1 is how many 1 byte ints to create).
  160. func genUint64Dist(dist [11]int, count int) (dest []uint64) {
  161. for i := 0; i < count; i++ {
  162. for k := 0; k < len(dist); k++ {
  163. var num uint64
  164. switch k {
  165. case 1:
  166. num = 1<<7 - 1
  167. case 2:
  168. num = 1<<14 - 1
  169. case 3:
  170. num = 1<<21 - 1
  171. case 4:
  172. num = 1<<28 - 1
  173. case 5:
  174. num = 1<<35 - 1
  175. case 6:
  176. num = 1<<42 - 1
  177. case 7:
  178. num = 1<<49 - 1
  179. case 8:
  180. num = 1<<56 - 1
  181. case 9:
  182. num = 1<<63 - 1
  183. case 10:
  184. num = 1<<64 - 1
  185. }
  186. for m := 0; m < dist[k]; m++ {
  187. dest = append(dest, num)
  188. }
  189. }
  190. }
  191. return
  192. }
  193. // BenchmarkDecodeEmpty measures the overhead of doing the minimal possible decode.
  194. func BenchmarkDecodeEmpty(b *testing.B) {
  195. raw, err := proto.Marshal(&tpb.Message{})
  196. if err != nil {
  197. b.Error("wrong encode", err)
  198. }
  199. b.ResetTimer()
  200. for i := 0; i < b.N; i++ {
  201. if err := proto.Unmarshal(raw, msgBlackhole); err != nil {
  202. b.Error("wrong decode", err)
  203. }
  204. }
  205. }