@@ -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+
777783func BenchmarkCompressGzip0 (b * testing.B ) { benchmarkCompress (b , 0 , []string {"gzip,deflate" }) }
778784func BenchmarkCompressGzip2 (b * testing.B ) { benchmarkCompress (b , 100 , []string {"gzip,deflate" }) }
779785func BenchmarkCompressGzip4 (b * testing.B ) { benchmarkCompress (b , 10000 , []string {"gzip,deflate" }) }
@@ -786,6 +792,70 @@ func BenchmarkCompressBrotli4(b *testing.B) { benchmarkCompress(b, 10000, []stri
786792func BenchmarkCompressBrotli6 (b * testing.B ) { benchmarkCompress (b , 1000000 , []string {"br" }) }
787793func 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+
789859func BenchmarkCanEncodeEntity (b * testing.B ) {
790860 testCases := []struct {
791861 name string
0 commit comments