@@ -709,51 +709,59 @@ func toJSON(b *testing.B, list *v1.PodList) []byte {
709709return 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 ) {
716713req := & http.Request {
717- Header : http.Header {
718- "Accept-Encoding" : []string {"gzip" },
719- },
720714URL : & url.URL {Path : "/path" },
721715}
716+ if gzip {
717+ req .Header = http.Header {
718+ "Accept-Encoding" : []string {"gzip" },
719+ }
720+ }
721+
722722featuregatetesting .SetFeatureGateDuringTest (b , utilfeature .DefaultFeatureGate , features .APIResponseCompression , true )
723723
724724encoder := & fakeEncoder {
725725buf : payload ,
726726}
727727
728728b .ResetTimer ()
729- for i := 0 ; i < b .N ; i ++ {
729+ responseBytesTotal := 0
730+ for b .Loop () {
730731recorder := httptest .NewRecorder ()
731732SerializeObject ("application/json" , encoder , recorder , req , http .StatusOK , nil /* object */ )
732733result := recorder .Result ()
733734if result .StatusCode != http .StatusOK {
734735b .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
759767type fakeResponseRecorder struct {
0 commit comments