Skip to content

Commit 2e0f777

Browse files
serathiusk8s-publishing-bot
authored andcommitted
Improve BenchmarkSerializeObject benchmark
Use sub-benchmarks with "Key=Value" naming convention to used by https://pkg.go.dev/golang.org/x/perf/cmd/benchstat. Use ReportMetric to report response size instead of writing it to logs. Kubernetes-commit: 273912fb7f64f2a8f917fe1f73e72103856a0aa4
1 parent c9eb9e8 commit 2e0f777

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

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)