diff --git a/pkg/api/testing/serialization_proto_test.go b/pkg/api/testing/serialization_proto_test.go index 445a2916f81..9e90b3fc15a 100644 --- a/pkg/api/testing/serialization_proto_test.go +++ b/pkg/api/testing/serialization_proto_test.go @@ -25,7 +25,7 @@ import ( "testing" "github.com/gogo/protobuf/proto" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/apitesting/fuzzer" apiequality "k8s.io/apimachinery/pkg/api/equality" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -165,6 +165,39 @@ func BenchmarkEncodeProtobufGeneratedMarshal(b *testing.B) { b.StopTimer() } +func BenchmarkEncodeProtobufGeneratedMarshalList10(b *testing.B) { + item := benchmarkItemsList(b, 10) + b.ResetTimer() + for i := 0; i < b.N; i++ { + if _, err := item.Marshal(); err != nil { + b.Fatal(err) + } + } + b.StopTimer() +} + +func BenchmarkEncodeProtobufGeneratedMarshalList100(b *testing.B) { + item := benchmarkItemsList(b, 100) + b.ResetTimer() + for i := 0; i < b.N; i++ { + if _, err := item.Marshal(); err != nil { + b.Fatal(err) + } + } + b.StopTimer() +} + +func BenchmarkEncodeProtobufGeneratedMarshalList1000(b *testing.B) { + item := benchmarkItemsList(b, 1000) + b.ResetTimer() + for i := 0; i < b.N; i++ { + if _, err := item.Marshal(); err != nil { + b.Fatal(err) + } + } + b.StopTimer() +} + // BenchmarkDecodeCodecToInternalProtobuf measures the cost of performing a codec decode, // including conversions and any type setting. This is a "full" decode. func BenchmarkDecodeCodecToInternalProtobuf(b *testing.B) { diff --git a/pkg/api/testing/serialization_test.go b/pkg/api/testing/serialization_test.go index 6d7bfc89997..e841f956f64 100644 --- a/pkg/api/testing/serialization_test.go +++ b/pkg/api/testing/serialization_test.go @@ -27,7 +27,7 @@ import ( jsoniter "github.com/json-iterator/go" appsv1 "k8s.io/api/apps/v1" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/apitesting/fuzzer" "k8s.io/apimachinery/pkg/api/apitesting/roundtrip" apiequality "k8s.io/apimachinery/pkg/api/equality" @@ -424,6 +424,25 @@ func benchmarkItems(b *testing.B) []v1.Pod { return items } +func benchmarkItemsList(b *testing.B, numItems int) v1.PodList { + apiObjectFuzzer := fuzzer.FuzzerFor(FuzzerFuncs, rand.NewSource(benchmarkSeed), legacyscheme.Codecs) + items := make([]v1.Pod, numItems) + for i := range items { + var pod api.Pod + apiObjectFuzzer.Fuzz(&pod) + pod.Spec.InitContainers, pod.Status.InitContainerStatuses = nil, nil + out, err := legacyscheme.Scheme.ConvertToVersion(&pod, v1.SchemeGroupVersion) + if err != nil { + panic(err) + } + items[i] = *out.(*v1.Pod) + } + + return v1.PodList{ + Items: items, + } +} + // BenchmarkEncodeCodec measures the cost of performing a codec encode, which includes // reflection (to clear APIVersion and Kind) func BenchmarkEncodeCodec(b *testing.B) {