bench_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2018 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. package encoding_test
  5. import (
  6. "flag"
  7. "fmt"
  8. "testing"
  9. jsonpbV1 "github.com/golang/protobuf/jsonpb"
  10. protoV1 "github.com/golang/protobuf/proto"
  11. "google.golang.org/protobuf/encoding/protojson"
  12. "google.golang.org/protobuf/encoding/prototext"
  13. pref "google.golang.org/protobuf/reflect/protoreflect"
  14. tpb "google.golang.org/protobuf/internal/testprotos/test"
  15. )
  16. // The results of these microbenchmarks are unlikely to correspond well
  17. // to real world peformance. They are mainly useful as a quick check to
  18. // detect unexpected regressions and for profiling specific cases.
  19. var benchV1 = flag.Bool("v1", false, "benchmark the v1 implementation")
  20. const maxRecurseLevel = 3
  21. func makeProto() *tpb.TestAllTypes {
  22. m := &tpb.TestAllTypes{}
  23. fillMessage(m.ProtoReflect(), 0)
  24. return m
  25. }
  26. func fillMessage(m pref.Message, level int) {
  27. if level > maxRecurseLevel {
  28. return
  29. }
  30. fieldDescs := m.Descriptor().Fields()
  31. for i := 0; i < fieldDescs.Len(); i++ {
  32. fd := fieldDescs.Get(i)
  33. switch {
  34. case fd.IsList():
  35. setList(m.Mutable(fd).List(), fd, level)
  36. case fd.IsMap():
  37. setMap(m.Mutable(fd).Map(), fd, level)
  38. default:
  39. setScalarField(m, fd, level)
  40. }
  41. }
  42. }
  43. func setScalarField(m pref.Message, fd pref.FieldDescriptor, level int) {
  44. switch fd.Kind() {
  45. case pref.MessageKind, pref.GroupKind:
  46. val := m.NewField(fd)
  47. fillMessage(val.Message(), level+1)
  48. m.Set(fd, val)
  49. default:
  50. m.Set(fd, scalarField(fd.Kind()))
  51. }
  52. }
  53. func scalarField(kind pref.Kind) pref.Value {
  54. switch kind {
  55. case pref.BoolKind:
  56. return pref.ValueOfBool(true)
  57. case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
  58. return pref.ValueOfInt32(1 << 30)
  59. case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
  60. return pref.ValueOfInt64(1 << 30)
  61. case pref.Uint32Kind, pref.Fixed32Kind:
  62. return pref.ValueOfUint32(1 << 30)
  63. case pref.Uint64Kind, pref.Fixed64Kind:
  64. return pref.ValueOfUint64(1 << 30)
  65. case pref.FloatKind:
  66. return pref.ValueOfFloat32(3.14159265)
  67. case pref.DoubleKind:
  68. return pref.ValueOfFloat64(3.14159265)
  69. case pref.BytesKind:
  70. return pref.ValueOfBytes([]byte("hello world"))
  71. case pref.StringKind:
  72. return pref.ValueOfString("hello world")
  73. case pref.EnumKind:
  74. return pref.ValueOfEnum(42)
  75. }
  76. panic(fmt.Sprintf("FieldDescriptor.Kind %v is not valid", kind))
  77. }
  78. func setList(list pref.List, fd pref.FieldDescriptor, level int) {
  79. switch fd.Kind() {
  80. case pref.MessageKind, pref.GroupKind:
  81. for i := 0; i < 10; i++ {
  82. val := list.NewElement()
  83. fillMessage(val.Message(), level+1)
  84. list.Append(val)
  85. }
  86. default:
  87. for i := 0; i < 100; i++ {
  88. list.Append(scalarField(fd.Kind()))
  89. }
  90. }
  91. }
  92. func setMap(mmap pref.Map, fd pref.FieldDescriptor, level int) {
  93. fields := fd.Message().Fields()
  94. keyDesc := fields.ByNumber(1)
  95. valDesc := fields.ByNumber(2)
  96. pkey := scalarField(keyDesc.Kind())
  97. switch kind := valDesc.Kind(); kind {
  98. case pref.MessageKind, pref.GroupKind:
  99. val := mmap.NewValue()
  100. fillMessage(val.Message(), level+1)
  101. mmap.Set(pkey.MapKey(), val)
  102. default:
  103. mmap.Set(pkey.MapKey(), scalarField(kind))
  104. }
  105. }
  106. func BenchmarkTextEncode(b *testing.B) {
  107. m := makeProto()
  108. for i := 0; i < b.N; i++ {
  109. if *benchV1 {
  110. protoV1.MarshalTextString(m)
  111. } else {
  112. _, err := prototext.MarshalOptions{Indent: " "}.Marshal(m)
  113. if err != nil {
  114. b.Fatal(err)
  115. }
  116. }
  117. }
  118. }
  119. func BenchmarkTextDecode(b *testing.B) {
  120. m := makeProto()
  121. in, err := prototext.MarshalOptions{Indent: " "}.Marshal(m)
  122. if err != nil {
  123. b.Fatal(err)
  124. }
  125. for i := 0; i < b.N; i++ {
  126. m := &tpb.TestAllTypes{}
  127. var err error
  128. if *benchV1 {
  129. err = protoV1.UnmarshalText(string(in), m)
  130. } else {
  131. err = prototext.Unmarshal(in, m)
  132. }
  133. if err != nil {
  134. b.Fatal(err)
  135. }
  136. }
  137. }
  138. func BenchmarkJSONEncode(b *testing.B) {
  139. m := makeProto()
  140. for i := 0; i < b.N; i++ {
  141. var err error
  142. if *benchV1 {
  143. jsm := &jsonpbV1.Marshaler{Indent: " "}
  144. _, err = jsm.MarshalToString(m)
  145. } else {
  146. _, err = protojson.MarshalOptions{Indent: " "}.Marshal(m)
  147. }
  148. if err != nil {
  149. b.Fatal(err)
  150. }
  151. }
  152. }
  153. func BenchmarkJSONDecode(b *testing.B) {
  154. m := makeProto()
  155. out, err := protojson.MarshalOptions{Indent: " "}.Marshal(m)
  156. if err != nil {
  157. b.Fatal(err)
  158. }
  159. for i := 0; i < b.N; i++ {
  160. m := &tpb.TestAllTypes{}
  161. var err error
  162. if *benchV1 {
  163. err = jsonpbV1.UnmarshalString(string(out), m)
  164. } else {
  165. err = protojson.Unmarshal(out, m)
  166. }
  167. if err != nil {
  168. b.Fatal(err)
  169. }
  170. }
  171. }