mirror of
https://github.com/kubernetes/kubernetes.git
synced 2026-06-09 08:55:55 -04:00
Merge pull request #134155 from serathius/gzip-benchmark
Improve BenchmarkSerializeObject benchmark
This commit is contained in:
commit
f24a967c42
1 changed files with 34 additions and 26 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue