From 32b70f4aae09563ab91dd956406cd074d6987183 Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Fri, 10 Oct 2025 14:24:28 +0200 Subject: [PATCH] http2: add transport gzip response benchmark For golang/go#61353 --- http2/transport_test.go | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/http2/transport_test.go b/http2/transport_test.go index 8ccae257..dcd7ae16 100644 --- a/http2/transport_test.go +++ b/http2/transport_test.go @@ -11,6 +11,7 @@ import ( "bytes" "compress/gzip" "context" + crand "crypto/rand" "crypto/tls" "encoding/hex" "errors" @@ -3866,6 +3867,57 @@ func benchLargeDownloadRoundTrip(b *testing.B, frameSize uint32) { } } +func BenchmarkClientGzip(b *testing.B) { + disableGoroutineTracking(b) + b.ReportAllocs() + + const responseSize = 1024 * 1024 + + var buf bytes.Buffer + gz := gzip.NewWriter(&buf) + if _, err := io.CopyN(gz, crand.Reader, responseSize); err != nil { + b.Fatal(err) + } + gz.Close() + + data := buf.Bytes() + ts := newTestServer(b, + func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Encoding", "gzip") + w.Write(data) + }, + optQuiet, + ) + + tr := &Transport{TLSClientConfig: tlsConfigInsecure} + defer tr.CloseIdleConnections() + + req, err := http.NewRequest("GET", ts.URL, nil) + if err != nil { + b.Fatal(err) + } + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + res, err := tr.RoundTrip(req) + if err != nil { + b.Fatalf("RoundTrip err = %v; want nil", err) + } + if res.StatusCode != http.StatusOK { + b.Fatalf("Response code = %v; want %v", res.StatusCode, http.StatusOK) + } + n, err := io.Copy(io.Discard, res.Body) + res.Body.Close() + if err != nil { + b.Fatalf("RoundTrip err = %v; want nil", err) + } + if n != responseSize { + b.Fatalf("RoundTrip expected %d bytes, got %d", responseSize, n) + } + } +} + // The client closes the connection just after the server got the client's HEADERS // frame, but before the server sends its HEADERS response back. The expected // result is an error on RoundTrip explaining the client closed the connection.