Skip to content

Commit 6ac9da5

Browse files
authored
Optimize/compression (#3937)
see commit message ref: #3933 fix: #3932 --------- Signed-off-by: Sandor Szücs <sandor.szuecs@zalando.de>
1 parent aa183dd commit 6ac9da5

4 files changed

Lines changed: 77 additions & 5 deletions

File tree

filters/builtin/compress.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"sync"
1414

1515
"github.com/andybalholm/brotli"
16+
kpflate "github.com/klauspost/compress/flate"
17+
kpgzip "github.com/klauspost/compress/gzip"
1618

1719
log "github.com/sirupsen/logrus"
1820
"github.com/zalando/skipper/filters"
@@ -342,12 +344,12 @@ func newEncoder(enc string, level int) (encoder, error) {
342344
if level > gzip.BestCompression {
343345
level = gzip.BestCompression
344346
}
345-
return gzip.NewWriterLevel(nil, level)
347+
return kpgzip.NewWriterLevel(nil, level)
346348
case "deflate":
347349
if level > flate.BestCompression {
348350
level = flate.BestCompression
349351
}
350-
return flate.NewWriter(nil, level)
352+
return kpflate.NewWriter(nil, level)
351353
default:
352354
unsupported()
353355
return nil, nil

filters/builtin/compress_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,12 @@ func TestPoolRelease(t *testing.T) {
774774
wg.Wait()
775775
}
776776

777+
func BenchmarkCompressDeflate0(b *testing.B) { benchmarkCompress(b, 0, []string{"deflate"}) }
778+
func BenchmarkCompressDeflate2(b *testing.B) { benchmarkCompress(b, 100, []string{"deflate"}) }
779+
func BenchmarkCompressDeflate4(b *testing.B) { benchmarkCompress(b, 10000, []string{"deflate"}) }
780+
func BenchmarkCompressDeflate6(b *testing.B) { benchmarkCompress(b, 1000000, []string{"deflate"}) }
781+
func BenchmarkCompressDeflate8(b *testing.B) { benchmarkCompress(b, 100000000, []string{"deflate"}) }
782+
777783
func BenchmarkCompressGzip0(b *testing.B) { benchmarkCompress(b, 0, []string{"gzip,deflate"}) }
778784
func BenchmarkCompressGzip2(b *testing.B) { benchmarkCompress(b, 100, []string{"gzip,deflate"}) }
779785
func BenchmarkCompressGzip4(b *testing.B) { benchmarkCompress(b, 10000, []string{"gzip,deflate"}) }
@@ -786,6 +792,70 @@ func BenchmarkCompressBrotli4(b *testing.B) { benchmarkCompress(b, 10000, []stri
786792
func BenchmarkCompressBrotli6(b *testing.B) { benchmarkCompress(b, 1000000, []string{"br"}) }
787793
func BenchmarkCompressBrotli8(b *testing.B) { benchmarkCompress(b, 100000000, []string{"br"}) }
788794

795+
func benchmarkCompressJSON(b *testing.B, n int64, encoding []string) {
796+
// Repeating JSON pattern — highly compressible, realistic payload
797+
pattern := []byte(`{"id":12345,"name":"test-user","email":"user@example.com","active":true,"tags":["a","b"]},`)
798+
buf := make([]byte, n)
799+
for i := 0; i < len(buf); i += len(pattern) {
800+
copy(buf[i:], pattern)
801+
}
802+
803+
s := NewCompress()
804+
f, _ := s.CreateFilter(nil)
805+
b.RunParallel(func(pb *testing.PB) {
806+
for pb.Next() {
807+
body := io.NopCloser(bytes.NewReader(buf))
808+
req := &http.Request{Header: http.Header{"Accept-Encoding": encoding}}
809+
rsp := &http.Response{
810+
Header: http.Header{"Content-Type": []string{"application/json"}},
811+
Body: body}
812+
ctx := &filtertest.Context{
813+
FRequest: req,
814+
FResponse: rsp}
815+
f.Response(ctx)
816+
_, err := io.ReadAll(rsp.Body)
817+
if err != nil {
818+
b.Fatal(err)
819+
}
820+
}
821+
})
822+
}
823+
824+
func benchmarkCompressLevel(b *testing.B, n int64, level int, encoding []string) {
825+
s := NewCompress()
826+
f, _ := s.CreateFilter([]any{float64(level)})
827+
b.RunParallel(func(pb *testing.PB) {
828+
for pb.Next() {
829+
body := io.NopCloser(&io.LimitedReader{R: rand.New(rand.NewSource(0)), N: n})
830+
req := &http.Request{Header: http.Header{"Accept-Encoding": encoding}}
831+
rsp := &http.Response{
832+
Header: http.Header{"Content-Type": []string{"application/octet-stream"}},
833+
Body: body}
834+
ctx := &filtertest.Context{
835+
FRequest: req,
836+
FResponse: rsp}
837+
f.Response(ctx)
838+
_, err := io.ReadAll(rsp.Body)
839+
if err != nil {
840+
b.Fatal(err)
841+
}
842+
}
843+
})
844+
}
845+
846+
// JSON payload benchmarks (compressible data)
847+
func BenchmarkCompressGzipJSON4(b *testing.B) { benchmarkCompressJSON(b, 10000, []string{"gzip"}) }
848+
func BenchmarkCompressGzipJSON6(b *testing.B) { benchmarkCompressJSON(b, 1000000, []string{"gzip"}) }
849+
func BenchmarkCompressGzipJSON8(b *testing.B) { benchmarkCompressJSON(b, 100000000, []string{"gzip"}) }
850+
851+
// Higher compression level (level 6 = DefaultCompression)
852+
func BenchmarkCompressGzipLevel6_4(b *testing.B) {
853+
benchmarkCompressLevel(b, 10000, 6, []string{"gzip"})
854+
}
855+
func BenchmarkCompressGzipLevel6_6(b *testing.B) {
856+
benchmarkCompressLevel(b, 1000000, 6, []string{"gzip"})
857+
}
858+
789859
func BenchmarkCanEncodeEntity(b *testing.B) {
790860
testCases := []struct {
791861
name string

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/google/uuid v1.6.0
2626
github.com/hashicorp/memberlist v0.5.4
2727
github.com/instana/go-sensor v1.73.1
28+
github.com/klauspost/compress v1.18.5
2829
github.com/lightstep/lightstep-tracer-go v0.26.0
2930
github.com/miekg/dns v1.1.72
3031
github.com/oklog/ulid v1.3.1
@@ -195,7 +196,6 @@ require (
195196
github.com/itchyny/timefmt-go v0.1.7 // indirect
196197
github.com/jmespath/go-jmespath v0.4.0 // indirect
197198
github.com/josharian/intern v1.0.0 // indirect
198-
github.com/klauspost/compress v1.18.2 // indirect
199199
github.com/klauspost/pgzip v1.2.6 // indirect
200200
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
201201
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E
418418
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
419419
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
420420
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
421-
github.com/klauspost/compress v1.18.2 h1:iiPHWW0YrcFgpBYhsA6D1+fqHssJscY/Tm/y2Uqnapk=
422-
github.com/klauspost/compress v1.18.2/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
421+
github.com/klauspost/compress v1.18.5 h1:/h1gH5Ce+VWNLSWqPzOVn6XBO+vJbCNGvjoaGBFW2IE=
422+
github.com/klauspost/compress v1.18.5/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
423423
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
424424
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
425425
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=

0 commit comments

Comments
 (0)