一. 测试代码
package gzip
import (
"compress/gzip"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func RequestGzip(enable bool) (int, error) {
client := http.Client{}
req, err := http.NewRequest("GET", "http://www.baidu.com", nil)
if enable { // 请求 header 添加 gzip
req.Header.Add("Content-Encoding", "gzip")
req.Header.Add("Accept-Encoding", "gzip")
}
resp, err := client.Do(req)
if err != nil {
fmt.Println("client.Do Error:", err)
return 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("ioutil.ReadAll Error", err)
return 0, err
}
return len(body), nil
}
// client 解析 gzip 返回
func ClientUncompress() {
client := http.Client{}
req, err := http.NewRequest("GET", "http://www.baidu.com", nil)
req.Header.Add("Content-Encoding", "gzip")
req.Header.Add("Accept-Encoding", "gzip")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var buf [1024 * 1024]byte
reader, err := gzip.NewReader(resp.Body)
if err != nil {
log.Fatal(err)
}
_, err = reader.Read(buf[:])
if err != nil {
log.Fatal(err)
}
reader.Close()
}
// client 正常解析返回
func ClientNormal() {
client := http.Client{}
req, err := http.NewRequest("GET", "http://www.baidu.com", nil)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var buf [1024 * 1024]byte
_, err = resp.Body.Read(buf[:])
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
}
1.1. http 开启 gzip 数据大小对比
package gzip
import (
"testing"
)
func TestRequestGzip(t *testing.T) {
length, err := RequestGzip(true) // 开启 gzip
if err != nil {
t.Error(err)
}
t.Logf("Request with gzip length: %d", length)
length, err = RequestGzip(false) // 未开启 gzip
if err != nil {
t.Error(err)
}
t.Logf("Request without gzip length: %d", length)
}
go test -v .
=== RUN TestRequestGzip
--- PASS: TestRequestGzip (0.05s)
gzip_test.go:12: Request with gzip length: 32168
gzip_test.go:18: Request without gzip length: 118315
PASS
可以看到返回的数据压缩前大小 118315,压缩后 32168。
1.2. http 开启 gzip benchmark 对比
压缩后数据量变小了,但解压缩会稍稍降低性能。
package gzip
import "testing"
func BenchmarkClientUncompress(b *testing.B) {
for i := 0; i < b.N; i++ {
ClientUncompress()
}
}
func BenchmarkClientNormal(b *testing.B) {
for i := 0; i < b.N; i++ {
ClientNormal()
}
}
go test -bench=. -run=none
goos: darwin
goarch: amd64
BenchmarkClientUncompress-8 100 19972186 ns/op
BenchmarkClientNormal-8 100 17922070 ns/op
PASS