Skip to content

Commit cf7971c

Browse files
Merge pull request #134155 from serathius/gzip-benchmark
Improve BenchmarkSerializeObject benchmark Kubernetes-commit: f24a967c42e5f211356561bb8073b0e28fa5b81d
2 parents c9eb9e8 + 2e0f777 commit cf7971c

File tree

3 files changed

+40
-32
lines changed

3 files changed

+40
-32
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ require (
5050
gopkg.in/go-jose/go-jose.v2 v2.6.3
5151
gopkg.in/natefinch/lumberjack.v2 v2.2.1
5252
k8s.io/api v0.0.0-20250922192300-863819f1e8a7
53-
k8s.io/apimachinery v0.0.0-20250919032000-da5b06e2fb66
54-
k8s.io/client-go v0.0.0-20250919032728-7b6fbf8e600b
53+
k8s.io/apimachinery v0.0.0-20250924032039-65724d7f3dbd
54+
k8s.io/client-go v0.0.0-20250924032830-6cf045f1f42f
5555
k8s.io/component-base v0.0.0-20250919034137-4453c3be72a8
5656
k8s.io/klog/v2 v2.130.1
5757
k8s.io/kms v0.0.0-20250918004306-2fba1b2102ee

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,10 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
297297
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
298298
k8s.io/api v0.0.0-20250922192300-863819f1e8a7 h1:8dwSssHFKuaxv5A+OSlzYcgmWF5vvcH4dMhpJvuAu9I=
299299
k8s.io/api v0.0.0-20250922192300-863819f1e8a7/go.mod h1:TOlAUK/2fXrUHaNvBKydwYAc1gMMrwBVJFbcIFXW3CU=
300-
k8s.io/apimachinery v0.0.0-20250919032000-da5b06e2fb66 h1:jIXLLhQbRs/pl4lqaNMthm/HwXQDjtcp218CAvDxtAw=
301-
k8s.io/apimachinery v0.0.0-20250919032000-da5b06e2fb66/go.mod h1:1YSL0XujdSTcnuHOR73D16EdW+d49JOdd8TXjCo6Dhc=
302-
k8s.io/client-go v0.0.0-20250919032728-7b6fbf8e600b h1:0xDtcVt6WdAfDwq7WKqtrturodHMfXYGqy9Dmpfq21o=
303-
k8s.io/client-go v0.0.0-20250919032728-7b6fbf8e600b/go.mod h1:rLgoyEtcTRxWqVKIaf6XVmq0rmsJCAHJDZwqMGoaaK4=
300+
k8s.io/apimachinery v0.0.0-20250924032039-65724d7f3dbd h1:7GqbvrpiPpizvleF0yu9ZDT0n6tUKfNcRPjCYc3wydw=
301+
k8s.io/apimachinery v0.0.0-20250924032039-65724d7f3dbd/go.mod h1:1YSL0XujdSTcnuHOR73D16EdW+d49JOdd8TXjCo6Dhc=
302+
k8s.io/client-go v0.0.0-20250924032830-6cf045f1f42f h1:mwRRbgglGHu8C02K/nxPdoSMfaJkFPK2M8ny04AawV0=
303+
k8s.io/client-go v0.0.0-20250924032830-6cf045f1f42f/go.mod h1:n+gkrdm8vty6JF2DDKIt84Fas77bPOuYaaOq15Tn3bI=
304304
k8s.io/component-base v0.0.0-20250919034137-4453c3be72a8 h1:6ori9Hm1g6OqXGZa9ihRpScRfwRnVwf0PpqzTu2KCl0=
305305
k8s.io/component-base v0.0.0-20250919034137-4453c3be72a8/go.mod h1:k2hnvURL10Hd0tc/Tn7RZprCv4tlRGtDqYD3GSRo/NE=
306306
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=

pkg/endpoints/handlers/responsewriters/writers_test.go

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -709,51 +709,59 @@ func toJSON(b *testing.B, list *v1.PodList) []byte {
709709
return out
710710
}
711711

712-
func benchmarkSerializeObject(b *testing.B, payload []byte) {
713-
input, output := len(payload), len(gzipContent(payload, defaultGzipContentEncodingLevel))
714-
b.Logf("Payload size: %d, expected output size: %d, ratio: %.2f", input, output, float64(output)/float64(input))
715-
712+
func benchmarkSerializeObject(b *testing.B, payload []byte, gzip bool) {
716713
req := &http.Request{
717-
Header: http.Header{
718-
"Accept-Encoding": []string{"gzip"},
719-
},
720714
URL: &url.URL{Path: "/path"},
721715
}
716+
if gzip {
717+
req.Header = http.Header{
718+
"Accept-Encoding": []string{"gzip"},
719+
}
720+
}
721+
722722
featuregatetesting.SetFeatureGateDuringTest(b, utilfeature.DefaultFeatureGate, features.APIResponseCompression, true)
723723

724724
encoder := &fakeEncoder{
725725
buf: payload,
726726
}
727727

728728
b.ResetTimer()
729-
for i := 0; i < b.N; i++ {
729+
responseBytesTotal := 0
730+
for b.Loop() {
730731
recorder := httptest.NewRecorder()
731732
SerializeObject("application/json", encoder, recorder, req, http.StatusOK, nil /* object */)
732733
result := recorder.Result()
733734
if result.StatusCode != http.StatusOK {
734735
b.Fatalf("incorrect status code: got %v; want: %v", result.StatusCode, http.StatusOK)
735736
}
737+
responseBytesTotal += recorder.Body.Len()
736738
}
739+
b.ReportMetric(float64(responseBytesTotal/b.N), "writtenBytes/op")
737740
}
738741

739-
func BenchmarkSerializeObject1000PodsPB(b *testing.B) {
740-
benchmarkSerializeObject(b, toProtoBuf(b, benchmarkItems(b, "testdata/pod.json", 1000)))
741-
}
742-
func BenchmarkSerializeObject10000PodsPB(b *testing.B) {
743-
benchmarkSerializeObject(b, toProtoBuf(b, benchmarkItems(b, "testdata/pod.json", 10000)))
744-
}
745-
func BenchmarkSerializeObject100000PodsPB(b *testing.B) {
746-
benchmarkSerializeObject(b, toProtoBuf(b, benchmarkItems(b, "testdata/pod.json", 100000)))
747-
}
748-
749-
func BenchmarkSerializeObject1000PodsJSON(b *testing.B) {
750-
benchmarkSerializeObject(b, toJSON(b, benchmarkItems(b, "testdata/pod.json", 1000)))
751-
}
752-
func BenchmarkSerializeObject10000PodsJSON(b *testing.B) {
753-
benchmarkSerializeObject(b, toJSON(b, benchmarkItems(b, "testdata/pod.json", 10000)))
754-
}
755-
func BenchmarkSerializeObject100000PodsJSON(b *testing.B) {
756-
benchmarkSerializeObject(b, toJSON(b, benchmarkItems(b, "testdata/pod.json", 100000)))
742+
func BenchmarkSerializeObject(b *testing.B) {
743+
for _, count := range []int{1_000, 10_000, 100_000} {
744+
b.Run(fmt.Sprintf("Count=%d", count), func(b *testing.B) {
745+
medias := []struct {
746+
name string
747+
convert func(*testing.B, *v1.PodList) []byte
748+
}{
749+
{"Json", toJSON},
750+
{"Protobuf", toProtoBuf},
751+
}
752+
podList := benchmarkItems(b, "testdata/pod.json", count)
753+
for _, media := range medias {
754+
b.Run(fmt.Sprintf("MediaType=%s", media.name), func(b *testing.B) {
755+
payload := media.convert(b, podList)
756+
for _, gzip := range []bool{true, false} {
757+
b.Run(fmt.Sprintf("Compression=%v", gzip), func(b *testing.B) {
758+
benchmarkSerializeObject(b, payload, gzip)
759+
})
760+
}
761+
})
762+
}
763+
})
764+
}
757765
}
758766

759767
type fakeResponseRecorder struct {

0 commit comments

Comments
 (0)