nytimes / gziphandler

Go middleware to gzip HTTP responses
https://godoc.org/github.com/NYTimes/gziphandler
Apache License 2.0
868 stars 129 forks source link

Reuse writers #2

Closed dim closed 9 years ago

dim commented 9 years ago

BEFORE:

$ go test ./... -test.bench=. -test.benchmem
PASS
BenchmarkGzipHandler_Serial     3000      513371 ns/op   1457632 B/op       53 allocs/op
BenchmarkGzipHandler_Parallel     3000      523055 ns/op   1457632 B/op       53 allocs/op
ok    github.com/nytimes/gziphandler  3.233s

AFTER:

$ go test ./... -test.bench=. -test.benchmem
PASS
BenchmarkGzipHandler_Serial     5000      256076 ns/op     57567 B/op       32 allocs/op
BenchmarkGzipHandler_Parallel     5000      251386 ns/op     47680 B/op       32 allocs/op
ok    github.com/nytimes/gziphandler  2.599s
adammck commented 9 years ago

I'm in favor of improving performance, but I suspect that in practice, the majority of the time spent would be in compressing the payloads rather than instantiating the writers. The test payloads are only 33 bytes, after all. Would you mind updating the benchmarks to use a (significantly) larger payload and see how that affects things?

dim commented 9 years ago

Hey, updated the benchmarks. Obviously the importance of this optimisation diminishes with the increasing compression effort, but to me it's still well worth it.

In our fork we also allow to configure a lower compression level, which significantly reduces CPU/compression time and increases the performance delta.

BenchmarkGzipHandler_S2k        5000      330696 ns/op   1462784 B/op      106 allocs/op
BenchmarkGzipHandler_S20k       2000      971565 ns/op   1495872 B/op      121 allocs/op
BenchmarkGzipHandler_S100k       300     4091846 ns/op   1635680 B/op      195 allocs/op
BenchmarkGzipHandler_P2k        5000      337403 ns/op   1462784 B/op      106 allocs/op
BenchmarkGzipHandler_P20k       2000      976987 ns/op   1495872 B/op      121 allocs/op
BenchmarkGzipHandler_P100k       300     4103702 ns/op   1635681 B/op      195 allocs/op

BenchmarkGzipHandler_S2k        5000      228732 ns/op     17893 B/op       84 allocs/op
BenchmarkGzipHandler_S20k       2000      872040 ns/op    136766 B/op      100 allocs/op
BenchmarkGzipHandler_S100k       300     3955646 ns/op    191786 B/op      173 allocs/op
BenchmarkGzipHandler_P2k       10000      220198 ns/op     17602 B/op       84 allocs/op
BenchmarkGzipHandler_P20k       2000      856099 ns/op    136766 B/op      100 allocs/op
BenchmarkGzipHandler_P100k       300     4006694 ns/op    191788 B/op      173 allocs/op
adammck commented 9 years ago

Thanks for the patch!