diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/responsewriters/writers_test.go b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/responsewriters/writers_test.go index 555478a615d..10bd42fcd93 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/responsewriters/writers_test.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/responsewriters/writers_test.go @@ -709,16 +709,16 @@ func toJSON(b *testing.B, list *v1.PodList) []byte { return out } -func benchmarkSerializeObject(b *testing.B, payload []byte) { - input, output := len(payload), len(gzipContent(payload, defaultGzipContentEncodingLevel)) - b.Logf("Payload size: %d, expected output size: %d, ratio: %.2f", input, output, float64(output)/float64(input)) - +func benchmarkSerializeObject(b *testing.B, payload []byte, gzip bool) { req := &http.Request{ - Header: http.Header{ - "Accept-Encoding": []string{"gzip"}, - }, URL: &url.URL{Path: "/path"}, } + if gzip { + req.Header = http.Header{ + "Accept-Encoding": []string{"gzip"}, + } + } + featuregatetesting.SetFeatureGateDuringTest(b, utilfeature.DefaultFeatureGate, features.APIResponseCompression, true) encoder := &fakeEncoder{ @@ -726,34 +726,42 @@ func benchmarkSerializeObject(b *testing.B, payload []byte) { } b.ResetTimer() - for i := 0; i < b.N; i++ { + responseBytesTotal := 0 + for b.Loop() { recorder := httptest.NewRecorder() SerializeObject("application/json", encoder, recorder, req, http.StatusOK, nil /* object */) result := recorder.Result() if result.StatusCode != http.StatusOK { b.Fatalf("incorrect status code: got %v; want: %v", result.StatusCode, http.StatusOK) } + responseBytesTotal += recorder.Body.Len() } + b.ReportMetric(float64(responseBytesTotal/b.N), "writtenBytes/op") } -func BenchmarkSerializeObject1000PodsPB(b *testing.B) { - benchmarkSerializeObject(b, toProtoBuf(b, benchmarkItems(b, "testdata/pod.json", 1000))) -} -func BenchmarkSerializeObject10000PodsPB(b *testing.B) { - benchmarkSerializeObject(b, toProtoBuf(b, benchmarkItems(b, "testdata/pod.json", 10000))) -} -func BenchmarkSerializeObject100000PodsPB(b *testing.B) { - benchmarkSerializeObject(b, toProtoBuf(b, benchmarkItems(b, "testdata/pod.json", 100000))) -} - -func BenchmarkSerializeObject1000PodsJSON(b *testing.B) { - benchmarkSerializeObject(b, toJSON(b, benchmarkItems(b, "testdata/pod.json", 1000))) -} -func BenchmarkSerializeObject10000PodsJSON(b *testing.B) { - benchmarkSerializeObject(b, toJSON(b, benchmarkItems(b, "testdata/pod.json", 10000))) -} -func BenchmarkSerializeObject100000PodsJSON(b *testing.B) { - benchmarkSerializeObject(b, toJSON(b, benchmarkItems(b, "testdata/pod.json", 100000))) +func BenchmarkSerializeObject(b *testing.B) { + for _, count := range []int{1_000, 10_000, 100_000} { + b.Run(fmt.Sprintf("Count=%d", count), func(b *testing.B) { + medias := []struct { + name string + convert func(*testing.B, *v1.PodList) []byte + }{ + {"Json", toJSON}, + {"Protobuf", toProtoBuf}, + } + podList := benchmarkItems(b, "testdata/pod.json", count) + for _, media := range medias { + b.Run(fmt.Sprintf("MediaType=%s", media.name), func(b *testing.B) { + payload := media.convert(b, podList) + for _, gzip := range []bool{true, false} { + b.Run(fmt.Sprintf("Compression=%v", gzip), func(b *testing.B) { + benchmarkSerializeObject(b, payload, gzip) + }) + } + }) + } + }) + } } type fakeResponseRecorder struct {