serialization_proto_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package api_test
  14. import (
  15. "bytes"
  16. "encoding/hex"
  17. "math/rand"
  18. "testing"
  19. "github.com/gogo/protobuf/proto"
  20. "k8s.io/kubernetes/pkg/api"
  21. "k8s.io/kubernetes/pkg/api/testapi"
  22. apitesting "k8s.io/kubernetes/pkg/api/testing"
  23. "k8s.io/kubernetes/pkg/api/unversioned"
  24. "k8s.io/kubernetes/pkg/api/v1"
  25. _ "k8s.io/kubernetes/pkg/apis/extensions"
  26. _ "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
  27. "k8s.io/kubernetes/pkg/runtime"
  28. "k8s.io/kubernetes/pkg/runtime/serializer/protobuf"
  29. "k8s.io/kubernetes/pkg/util/diff"
  30. )
  31. func init() {
  32. codecsToTest = append(codecsToTest, func(version unversioned.GroupVersion, item runtime.Object) (runtime.Codec, error) {
  33. s := protobuf.NewSerializer(api.Scheme, api.Scheme, "application/arbitrary.content.type")
  34. return api.Codecs.CodecForVersions(s, s, testapi.ExternalGroupVersions(), nil), nil
  35. })
  36. }
  37. func TestUniversalDeserializer(t *testing.T) {
  38. expected := &v1.Pod{ObjectMeta: v1.ObjectMeta{Name: "test"}}
  39. d := api.Codecs.UniversalDeserializer()
  40. for _, mediaType := range []string{"application/json", "application/yaml", "application/vnd.kubernetes.protobuf"} {
  41. e, ok := api.Codecs.SerializerForMediaType(mediaType, nil)
  42. if !ok {
  43. t.Fatal(mediaType)
  44. }
  45. buf := &bytes.Buffer{}
  46. if err := e.Encode(expected, buf); err != nil {
  47. t.Fatalf("%s: %v", mediaType, err)
  48. }
  49. obj, _, err := d.Decode(buf.Bytes(), &unversioned.GroupVersionKind{Kind: "Pod", Version: "v1"}, nil)
  50. if err != nil {
  51. t.Fatalf("%s: %v", mediaType, err)
  52. }
  53. if !api.Semantic.DeepEqual(expected, obj) {
  54. t.Fatalf("%s: %#v", mediaType, obj)
  55. }
  56. }
  57. }
  58. func TestProtobufRoundTrip(t *testing.T) {
  59. obj := &v1.Pod{}
  60. apitesting.FuzzerFor(t, v1.SchemeGroupVersion, rand.NewSource(benchmarkSeed)).Fuzz(obj)
  61. // InitContainers are turned into annotations by conversion.
  62. obj.Spec.InitContainers = nil
  63. obj.Status.InitContainerStatuses = nil
  64. data, err := obj.Marshal()
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. out := &v1.Pod{}
  69. if err := out.Unmarshal(data); err != nil {
  70. t.Fatal(err)
  71. }
  72. if !api.Semantic.Equalities.DeepEqual(out, obj) {
  73. t.Logf("marshal\n%s", hex.Dump(data))
  74. t.Fatalf("Unmarshal is unequal\n%s", diff.ObjectGoPrintDiff(out, obj))
  75. }
  76. }
  77. // BenchmarkEncodeCodec measures the cost of performing a codec encode, which includes
  78. // reflection (to clear APIVersion and Kind)
  79. func BenchmarkEncodeCodecProtobuf(b *testing.B) {
  80. items := benchmarkItems()
  81. width := len(items)
  82. s := protobuf.NewSerializer(nil, nil, "application/arbitrary.content.type")
  83. b.ResetTimer()
  84. for i := 0; i < b.N; i++ {
  85. if _, err := runtime.Encode(s, &items[i%width]); err != nil {
  86. b.Fatal(err)
  87. }
  88. }
  89. b.StopTimer()
  90. }
  91. // BenchmarkEncodeCodecFromInternalProtobuf measures the cost of performing a codec encode,
  92. // including conversions and any type setting. This is a "full" encode.
  93. func BenchmarkEncodeCodecFromInternalProtobuf(b *testing.B) {
  94. items := benchmarkItems()
  95. width := len(items)
  96. encodable := make([]api.Pod, width)
  97. for i := range items {
  98. if err := api.Scheme.Convert(&items[i], &encodable[i], nil); err != nil {
  99. b.Fatal(err)
  100. }
  101. }
  102. s := protobuf.NewSerializer(nil, nil, "application/arbitrary.content.type")
  103. codec := api.Codecs.EncoderForVersion(s, v1.SchemeGroupVersion)
  104. b.ResetTimer()
  105. for i := 0; i < b.N; i++ {
  106. if _, err := runtime.Encode(codec, &encodable[i%width]); err != nil {
  107. b.Fatal(err)
  108. }
  109. }
  110. b.StopTimer()
  111. }
  112. func BenchmarkEncodeProtobufGeneratedMarshal(b *testing.B) {
  113. items := benchmarkItems()
  114. width := len(items)
  115. b.ResetTimer()
  116. for i := 0; i < b.N; i++ {
  117. if _, err := items[i%width].Marshal(); err != nil {
  118. b.Fatal(err)
  119. }
  120. }
  121. b.StopTimer()
  122. }
  123. // BenchmarkDecodeCodecToInternalProtobuf measures the cost of performing a codec decode,
  124. // including conversions and any type setting. This is a "full" decode.
  125. func BenchmarkDecodeCodecToInternalProtobuf(b *testing.B) {
  126. items := benchmarkItems()
  127. width := len(items)
  128. s := protobuf.NewSerializer(api.Scheme, api.Scheme, "application/arbitrary.content.type")
  129. encoder := api.Codecs.EncoderForVersion(s, v1.SchemeGroupVersion)
  130. var encoded [][]byte
  131. for i := range items {
  132. data, err := runtime.Encode(encoder, &items[i])
  133. if err != nil {
  134. b.Fatal(err)
  135. }
  136. encoded = append(encoded, data)
  137. }
  138. decoder := api.Codecs.DecoderToVersion(s, api.SchemeGroupVersion)
  139. b.ResetTimer()
  140. for i := 0; i < b.N; i++ {
  141. if _, err := runtime.Decode(decoder, encoded[i%width]); err != nil {
  142. b.Fatal(err)
  143. }
  144. }
  145. b.StopTimer()
  146. }
  147. // BenchmarkDecodeJSON provides a baseline for regular JSON decode performance
  148. func BenchmarkDecodeIntoProtobuf(b *testing.B) {
  149. items := benchmarkItems()
  150. width := len(items)
  151. encoded := make([][]byte, width)
  152. for i := range items {
  153. data, err := (&items[i]).Marshal()
  154. if err != nil {
  155. b.Fatal(err)
  156. }
  157. encoded[i] = data
  158. validate := &v1.Pod{}
  159. if err := proto.Unmarshal(data, validate); err != nil {
  160. b.Fatalf("Failed to unmarshal %d: %v\n%#v", i, err, items[i])
  161. }
  162. }
  163. for i := 0; i < b.N; i++ {
  164. obj := v1.Pod{}
  165. if err := proto.Unmarshal(encoded[i%width], &obj); err != nil {
  166. b.Fatal(err)
  167. }
  168. }
  169. }