github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/serializer/encoder_with_allocator_test.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package serializer 18 19 import ( 20 "crypto/rand" 21 "io/ioutil" 22 "testing" 23 24 metav1 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/meta/v1" 25 testapigroupv1 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/testapigroup/v1" 26 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime" 27 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema" 28 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/serializer/protobuf" 29 ) 30 31 func BenchmarkProtobufEncoder(b *testing.B) { 32 benchmarkEncodeFor(b, protobuf.NewSerializer(nil, nil)) 33 } 34 35 func BenchmarkProtobufEncodeWithAllocator(b *testing.B) { 36 benchmarkEncodeWithAllocatorFor(b, protobuf.NewSerializer(nil, nil)) 37 } 38 39 func BenchmarkRawProtobufEncoder(b *testing.B) { 40 benchmarkEncodeFor(b, protobuf.NewRawSerializer(nil, nil)) 41 } 42 43 func BenchmarkRawProtobufEncodeWithAllocator(b *testing.B) { 44 benchmarkEncodeWithAllocatorFor(b, protobuf.NewRawSerializer(nil, nil)) 45 } 46 47 func benchmarkEncodeFor(b *testing.B, target runtime.Encoder) { 48 for _, tc := range benchTestCases() { 49 b.Run(tc.name, func(b *testing.B) { 50 b.ReportAllocs() 51 for n := 0; n < b.N; n++ { 52 err := target.Encode(tc.obj, ioutil.Discard) 53 if err != nil { 54 b.Fatal(err) 55 } 56 } 57 }) 58 } 59 } 60 61 func benchmarkEncodeWithAllocatorFor(b *testing.B, target runtime.EncoderWithAllocator) { 62 for _, tc := range benchTestCases() { 63 b.Run(tc.name, func(b *testing.B) { 64 b.ReportAllocs() 65 allocator := &runtime.Allocator{} 66 for n := 0; n < b.N; n++ { 67 err := target.EncodeWithAllocator(tc.obj, ioutil.Discard, allocator) 68 if err != nil { 69 b.Fatal(err) 70 } 71 } 72 }) 73 } 74 } 75 76 type benchTestCase struct { 77 name string 78 obj runtime.Object 79 } 80 81 func benchTestCases() []benchTestCase { 82 return []benchTestCase{ 83 { 84 name: "an obj with 1kB payload", 85 obj: func() runtime.Object { 86 carpPayload := make([]byte, 1000) // 1 kB 87 if _, err := rand.Read(carpPayload); err != nil { 88 panic(err) 89 } 90 return carpWithPayload(carpPayload) 91 }(), 92 }, 93 { 94 name: "an obj with 10kB payload", 95 obj: func() runtime.Object { 96 carpPayload := make([]byte, 10000) // 10 kB 97 if _, err := rand.Read(carpPayload); err != nil { 98 panic(err) 99 } 100 return carpWithPayload(carpPayload) 101 }(), 102 }, 103 { 104 name: "an obj with 100kB payload", 105 obj: func() runtime.Object { 106 carpPayload := make([]byte, 100000) // 100 kB 107 if _, err := rand.Read(carpPayload); err != nil { 108 panic(err) 109 } 110 return carpWithPayload(carpPayload) 111 }(), 112 }, 113 { 114 name: "an obj with 1MB payload", 115 obj: func() runtime.Object { 116 carpPayload := make([]byte, 1000000) // 1 MB 117 if _, err := rand.Read(carpPayload); err != nil { 118 panic(err) 119 } 120 return carpWithPayload(carpPayload) 121 }(), 122 }, 123 } 124 } 125 126 func carpWithPayload(carpPayload []byte) *testapigroupv1.Carp { 127 gvk := &schema.GroupVersionKind{Group: "group", Version: "version", Kind: "Carp"} 128 return &testapigroupv1.Carp{ 129 TypeMeta: metav1.TypeMeta{APIVersion: gvk.GroupVersion().String(), Kind: gvk.Kind}, 130 ObjectMeta: metav1.ObjectMeta{ 131 Name: "name", 132 Namespace: "namespace", 133 }, 134 Spec: testapigroupv1.CarpSpec{ 135 Subdomain: "carp.k8s.io", 136 NodeSelector: map[string]string{"payload": string(carpPayload)}, 137 }, 138 } 139 }