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