http2: add transport gzip response benchmark

For golang/go#61353
This commit is contained in:
Alexander Yastrebov
2025-10-10 14:24:28 +02:00
parent 9f2f0b95b6
commit 32b70f4aae

View File

@@ -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.