2018-11-14 09:18:03 +00:00
|
|
|
package compress
|
2017-06-06 23:02:02 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-31 10:36:05 +00:00
|
|
|
"context"
|
2021-03-04 19:08:03 +00:00
|
|
|
"io"
|
2017-06-06 23:02:02 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2021-07-19 08:22:14 +00:00
|
|
|
"github.com/klauspost/compress/gzhttp"
|
2017-06-06 23:02:02 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-08-21 09:10:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/testhelpers"
|
2017-06-06 23:02:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-07-13 15:56:01 +00:00
|
|
|
acceptEncodingHeader = "Accept-Encoding"
|
|
|
|
contentEncodingHeader = "Content-Encoding"
|
2017-11-10 13:12:02 +00:00
|
|
|
contentTypeHeader = "Content-Type"
|
2017-07-13 15:56:01 +00:00
|
|
|
varyHeader = "Vary"
|
|
|
|
gzipValue = "gzip"
|
2017-06-06 23:02:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldCompressWhenNoContentEncodingHeader(t *testing.T) {
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
2017-07-13 15:56:01 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2021-07-19 08:22:14 +00:00
|
|
|
baseBody := generateBytes(gzhttp.DefaultMinSize)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2018-08-06 18:00:03 +00:00
|
|
|
_, err := rw.Write(baseBody)
|
|
|
|
assert.NoError(t, err)
|
2018-11-14 09:18:03 +00:00
|
|
|
})
|
2021-07-19 08:22:14 +00:00
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
rw := httptest.NewRecorder()
|
2018-11-14 09:18:03 +00:00
|
|
|
handler.ServeHTTP(rw, req)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
assert.Equal(t, gzipValue, rw.Header().Get(contentEncodingHeader))
|
2017-06-06 23:02:02 +00:00
|
|
|
assert.Equal(t, acceptEncodingHeader, rw.Header().Get(varyHeader))
|
|
|
|
|
|
|
|
if assert.ObjectsAreEqualValues(rw.Body.Bytes(), baseBody) {
|
|
|
|
assert.Fail(t, "expected a compressed body", "got %v", rw.Body.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotCompressWhenContentEncodingHeader(t *testing.T) {
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
2017-07-13 15:56:01 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2021-07-19 08:22:14 +00:00
|
|
|
fakeCompressedBody := generateBytes(gzhttp.DefaultMinSize)
|
2018-11-14 09:18:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2017-07-13 15:56:01 +00:00
|
|
|
rw.Header().Add(contentEncodingHeader, gzipValue)
|
|
|
|
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
2018-11-14 09:18:03 +00:00
|
|
|
_, err := rw.Write(fakeCompressedBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
2021-07-19 08:22:14 +00:00
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
2017-07-13 15:56:01 +00:00
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
2018-11-14 09:18:03 +00:00
|
|
|
handler.ServeHTTP(rw, req)
|
2017-07-13 15:56:01 +00:00
|
|
|
|
|
|
|
assert.Equal(t, gzipValue, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.Equal(t, acceptEncodingHeader, rw.Header().Get(varyHeader))
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), fakeCompressedBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotCompressWhenNoAcceptEncodingHeader(t *testing.T) {
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
|
2021-07-19 08:22:14 +00:00
|
|
|
fakeBody := generateBytes(gzhttp.DefaultMinSize)
|
2018-11-14 09:18:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := rw.Write(fakeBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
2021-07-19 08:22:14 +00:00
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
2018-11-14 09:18:03 +00:00
|
|
|
handler.ServeHTTP(rw, req)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:36:05 +00:00
|
|
|
func TestShouldNotCompressWhenSpecificContentType(t *testing.T) {
|
2021-07-19 08:22:14 +00:00
|
|
|
baseBody := generateBytes(gzhttp.DefaultMinSize)
|
2019-10-31 10:36:05 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
2021-02-12 11:12:03 +00:00
|
|
|
desc string
|
|
|
|
conf dynamic.Compress
|
|
|
|
reqContentType string
|
|
|
|
respContentType string
|
2019-10-31 10:36:05 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-02-12 11:12:03 +00:00
|
|
|
desc: "Exclude Request Content-Type",
|
2019-10-31 10:36:05 +00:00
|
|
|
conf: dynamic.Compress{
|
|
|
|
ExcludedContentTypes: []string{"text/event-stream"},
|
|
|
|
},
|
|
|
|
reqContentType: "text/event-stream",
|
|
|
|
},
|
2021-02-12 11:12:03 +00:00
|
|
|
{
|
|
|
|
desc: "Exclude Response Content-Type",
|
|
|
|
conf: dynamic.Compress{
|
|
|
|
ExcludedContentTypes: []string{"text/event-stream"},
|
|
|
|
},
|
|
|
|
respContentType: "text/event-stream",
|
|
|
|
},
|
2019-10-31 10:36:05 +00:00
|
|
|
{
|
|
|
|
desc: "application/grpc",
|
|
|
|
conf: dynamic.Compress{},
|
|
|
|
reqContentType: "application/grpc",
|
|
|
|
},
|
|
|
|
}
|
2017-11-10 13:12:02 +00:00
|
|
|
|
2019-10-31 10:36:05 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
|
|
|
if test.reqContentType != "" {
|
|
|
|
req.Header.Add(contentTypeHeader, test.reqContentType)
|
|
|
|
}
|
|
|
|
|
2021-02-12 11:12:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
if len(test.respContentType) > 0 {
|
|
|
|
rw.Header().Set(contentTypeHeader, test.respContentType)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := rw.Write(baseBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-10-31 10:36:05 +00:00
|
|
|
handler, err := New(context.Background(), next, test.conf, "test")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
|
|
|
|
|
|
assert.Empty(t, rw.Header().Get(acceptEncodingHeader))
|
|
|
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), baseBody)
|
|
|
|
})
|
|
|
|
}
|
2017-11-10 13:12:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
func TestIntegrationShouldNotCompress(t *testing.T) {
|
2017-07-13 15:56:01 +00:00
|
|
|
fakeCompressedBody := generateBytes(100000)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2018-11-14 09:18:03 +00:00
|
|
|
handler http.Handler
|
2017-08-21 09:10:03 +00:00
|
|
|
expectedStatusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "when content already compressed",
|
2018-11-14 09:18:03 +00:00
|
|
|
handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2017-08-21 09:10:03 +00:00
|
|
|
rw.Header().Add(contentEncodingHeader, gzipValue)
|
|
|
|
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
2018-11-14 09:18:03 +00:00
|
|
|
_, err := rw.Write(fakeCompressedBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}),
|
2017-08-21 09:10:03 +00:00
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "when content already compressed and status code Created",
|
2018-11-14 09:18:03 +00:00
|
|
|
handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2017-08-21 09:10:03 +00:00
|
|
|
rw.Header().Add(contentEncodingHeader, gzipValue)
|
|
|
|
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
|
|
|
rw.WriteHeader(http.StatusCreated)
|
2018-11-14 09:18:03 +00:00
|
|
|
_, err := rw.Write(fakeCompressedBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}),
|
2017-08-21 09:10:03 +00:00
|
|
|
expectedStatusCode: http.StatusCreated,
|
|
|
|
},
|
2017-07-13 15:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2021-07-19 08:22:14 +00:00
|
|
|
compress, err := New(context.Background(), test.handler, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
ts := httptest.NewServer(compress)
|
2017-08-21 09:10:03 +00:00
|
|
|
defer ts.Close()
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
assert.Equal(t, test.expectedStatusCode, resp.StatusCode)
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
assert.Equal(t, gzipValue, resp.Header.Get(contentEncodingHeader))
|
|
|
|
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2021-03-04 19:08:03 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2017-08-21 09:10:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, fakeCompressedBody, body)
|
|
|
|
})
|
|
|
|
}
|
2017-07-13 15:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 23:48:03 +00:00
|
|
|
func TestShouldWriteHeaderWhenFlush(t *testing.T) {
|
2018-11-14 09:18:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2017-11-08 23:48:03 +00:00
|
|
|
rw.Header().Add(contentEncodingHeader, gzipValue)
|
|
|
|
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
|
|
|
rw.WriteHeader(http.StatusUnauthorized)
|
|
|
|
rw.(http.Flusher).Flush()
|
2018-11-14 09:18:03 +00:00
|
|
|
_, err := rw.Write([]byte("short"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2017-11-08 23:48:03 +00:00
|
|
|
})
|
2021-07-19 08:22:14 +00:00
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
ts := httptest.NewServer(handler)
|
2017-11-08 23:48:03 +00:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
|
|
|
|
|
|
|
assert.Equal(t, gzipValue, resp.Header.Get(contentEncodingHeader))
|
|
|
|
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
|
|
|
}
|
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
func TestIntegrationShouldCompress(t *testing.T) {
|
2017-07-13 15:56:01 +00:00
|
|
|
fakeBody := generateBytes(100000)
|
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2018-11-14 09:18:03 +00:00
|
|
|
handler http.Handler
|
2017-08-21 09:10:03 +00:00
|
|
|
expectedStatusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "when AcceptEncoding header is present",
|
2018-11-14 09:18:03 +00:00
|
|
|
handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := rw.Write(fakeBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}),
|
2017-08-21 09:10:03 +00:00
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "when AcceptEncoding header is present and status code Created",
|
2018-11-14 09:18:03 +00:00
|
|
|
handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2017-08-21 09:10:03 +00:00
|
|
|
rw.WriteHeader(http.StatusCreated)
|
2018-11-14 09:18:03 +00:00
|
|
|
_, err := rw.Write(fakeBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}),
|
2017-08-21 09:10:03 +00:00
|
|
|
expectedStatusCode: http.StatusCreated,
|
|
|
|
},
|
2017-07-13 15:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2021-07-19 08:22:14 +00:00
|
|
|
compress, err := New(context.Background(), test.handler, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
ts := httptest.NewServer(compress)
|
2017-08-21 09:10:03 +00:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
assert.Equal(t, test.expectedStatusCode, resp.StatusCode)
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
assert.Equal(t, gzipValue, resp.Header.Get(contentEncodingHeader))
|
|
|
|
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2021-03-04 19:08:03 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2017-08-21 09:10:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
if assert.ObjectsAreEqualValues(body, fakeBody) {
|
|
|
|
assert.Fail(t, "expected a compressed body", "got %v", body)
|
|
|
|
}
|
|
|
|
})
|
2017-07-13 15:56:01 +00:00
|
|
|
}
|
2017-06-06 23:02:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 16:00:08 +00:00
|
|
|
func TestMinResponseBodyBytes(t *testing.T) {
|
|
|
|
fakeBody := generateBytes(100000)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
minResponseBodyBytes int
|
|
|
|
expectedCompression bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "should compress",
|
|
|
|
expectedCompression: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "should not compress",
|
|
|
|
minResponseBodyBytes: 100001,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
|
|
|
|
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
if _, err := rw.Write(fakeBody); err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{MinResponseBodyBytes: test.minResponseBodyBytes}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
|
|
|
|
|
|
if test.expectedCompression {
|
|
|
|
assert.Equal(t, gzipValue, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.NotEqualValues(t, rw.Body.Bytes(), fakeBody)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 08:22:14 +00:00
|
|
|
func BenchmarkCompress(b *testing.B) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
parallel bool
|
|
|
|
size int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "2k",
|
|
|
|
size: 2048,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "20k",
|
|
|
|
size: 20480,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "100k",
|
|
|
|
size: 102400,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2k parallel",
|
|
|
|
parallel: true,
|
|
|
|
size: 2048,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "20k parallel",
|
|
|
|
parallel: true,
|
|
|
|
size: 20480,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "100k parallel",
|
|
|
|
parallel: true,
|
|
|
|
size: 102400,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
b.Run(test.name, func(b *testing.B) {
|
|
|
|
baseBody := generateBytes(test.size)
|
|
|
|
|
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := rw.Write(baseBody)
|
|
|
|
assert.NoError(b, err)
|
|
|
|
})
|
|
|
|
handler, _ := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
|
2022-08-09 15:36:08 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/whatever", nil)
|
2021-07-19 08:22:14 +00:00
|
|
|
req.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(int64(test.size))
|
|
|
|
if test.parallel {
|
|
|
|
b.ResetTimer()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
runBenchmark(b, req, handler)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
runBenchmark(b, req, handler)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runBenchmark(b *testing.B, req *http.Request, handler http.Handler) {
|
|
|
|
b.Helper()
|
|
|
|
|
|
|
|
res := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(res, req)
|
|
|
|
if code := res.Code; code != 200 {
|
|
|
|
b.Fatalf("Expected 200 but got %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(b, gzipValue, res.Header().Get(contentEncodingHeader))
|
|
|
|
}
|
|
|
|
|
2021-01-28 08:00:03 +00:00
|
|
|
func generateBytes(length int) []byte {
|
2017-06-06 23:02:02 +00:00
|
|
|
var value []byte
|
2021-01-28 08:00:03 +00:00
|
|
|
for i := 0; i < length; i++ {
|
2017-07-13 15:56:01 +00:00
|
|
|
value = append(value, 0x61+byte(i))
|
2017-06-06 23:02:02 +00:00
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|