说明
基于brotli算法实现了gin的中间件,为使用gin框架的Go项目提供brotli压缩功能,中间件使用方便依赖少,使用module管理依赖包。
引入"github.com/CodeLineage/brotli"即可使用,
brotli
Brotli是一个通用目的的无损压缩算法,它通过用变种的LZ77 算法,Huffman 编码和二阶文本建模进行数据压缩,是一种压缩比很高的压缩方法。在压缩速度上跟 Deflate 差不多,但是提供了更密集的压缩。
文档
源码
使用示例
默认方式
handler.Use(brotli.DefaultHandler().Gin)
自定义
handler.Use(brotli.NewHandler(brotli.Config{
CompressionLevel: brotli.DefaultCompression,
MinContentLength: brotli.DefalutContentLen,
RequestFilter: []brotli.RequestFilter{
brotli.NewCommonRequestFilter(),
brotli.NewRequestApiFilter([]string{
"/blog/cache/http",
"/blog/detail/index",
}),
},
ResponseHeaderFilter: []brotli.ResponseHeaderFilter{
brotli.DefaultContentTypeFilter(),
},
}).Gin)
测试
压测压缩速率
bash-4.3# go test -v -run="GinWithLevelsHandler"
=== RUN TestGinWithLevelsHandler
=== RUN TestGinWithLevelsHandler/level_0
=== RUN TestGinWithLevelsHandler/level_1
=== RUN TestGinWithLevelsHandler/level_2
=== RUN TestGinWithLevelsHandler/level_3
=== RUN TestGinWithLevelsHandler/level_4
=== RUN TestGinWithLevelsHandler/level_5
=== RUN TestGinWithLevelsHandler/level_6
=== RUN TestGinWithLevelsHandler/level_7
=== RUN TestGinWithLevelsHandler/level_8
=== RUN TestGinWithLevelsHandler/level_9
=== RUN TestGinWithLevelsHandler/level_10
=== RUN TestGinWithLevelsHandler/level_11
--- PASS: TestGinWithLevelsHandler (0.06s)
--- PASS: TestGinWithLevelsHandler/level_0 (0.00s)
handler_test.go:215: level_0: compressed 4267 => 1456 ratio=>0.66
--- PASS: TestGinWithLevelsHandler/level_1 (0.00s)
handler_test.go:215: level_1: compressed 4267 => 1419 ratio=>0.67
--- PASS: TestGinWithLevelsHandler/level_2 (0.00s)
handler_test.go:215: level_2: compressed 4267 => 1410 ratio=>0.67
--- PASS: TestGinWithLevelsHandler/level_3 (0.00s)
handler_test.go:215: level_3: compressed 4267 => 1340 ratio=>0.69
--- PASS: TestGinWithLevelsHandler/level_4 (0.00s)
handler_test.go:215: level_4: compressed 4267 => 1257 ratio=>0.71
--- PASS: TestGinWithLevelsHandler/level_5 (0.00s)
handler_test.go:215: level_5: compressed 4267 => 1187 ratio=>0.72
--- PASS: TestGinWithLevelsHandler/level_6 (0.01s)
handler_test.go:215: level_6: compressed 4267 => 1187 ratio=>0.72
--- PASS: TestGinWithLevelsHandler/level_7 (0.00s)
handler_test.go:215: level_7: compressed 4267 => 1186 ratio=>0.72
--- PASS: TestGinWithLevelsHandler/level_8 (0.01s)
handler_test.go:215: level_8: compressed 4267 => 1186 ratio=>0.72
--- PASS: TestGinWithLevelsHandler/level_9 (0.01s)
handler_test.go:215: level_9: compressed 4267 => 1185 ratio=>0.72
--- PASS: TestGinWithLevelsHandler/level_10 (0.01s)
handler_test.go:215: level_10: compressed 4268 => 1049 ratio=>0.75
--- PASS: TestGinWithLevelsHandler/level_11 (0.02s)
handler_test.go:215: level_11: compressed 4268 => 1042 ratio=>0.76
PASS
ok github.com/CodeLineage/brotli 0.076s
基准测试
bash-4.3# go test -benchmem -bench .
goos: linux
goarch: amd64
pkg: github.com/CodeLineage/brotli
BenchmarkGin_SmallPayload-2 2917842 435 ns/op 96 B/op 3 allocs/op
BenchmarkGinWithDefaultHandler_SmallPayload-2 1271218 997 ns/op 128 B/op 4 allocs/op
BenchmarkGin_BigPayload-2 2912677 424 ns/op 96 B/op 3 allocs/op
BenchmarkGinWithDefaultHandler_BigPayload-2 3063 405511 ns/op 882 B/op 6 allocs/op
PASS
ok github.com/CodeLineage/brotli 7.122s