Merge pull request #134155 from serathius/gzip-benchmark

Improve BenchmarkSerializeObject benchmark
This commit is contained in:
Kubernetes Prow Robot 2025-09-24 06:34:18 -07:00 committed by GitHub
commit f24a967c42
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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 {