Skip to content

Instantly share code, notes, and snippets.

@klauspost
Created October 27, 2016 09:13
Show Gist options
  • Save klauspost/809f12607d48fa16eaee34ca76c90c78 to your computer and use it in GitHub Desktop.
Save klauspost/809f12607d48fa16eaee34ca76c90c78 to your computer and use it in GitHub Desktop.
package bench
import (
"bytes"
"compress/gzip"
ogzip "github.com/klauspost/compress/gzip"
"testing"
)
var bidReq = []byte(`{"id":"50215d10a41d474f77591bff601f6ade","imp":[{"id":"86df3bc6-7bd4-44d9-64e2-584a69790229","native":{"request":"{\"ver\":\"1.0\",\"plcmtcnt\":1,\"assets\":[{\"id\":1,\"data\":{\"type\":12}},{\"id\":2,\"required\":1,\"title\":{\"len\":50}},{\"id\":3,\"required\":1,\"img\":{\"type\":1,\"w\":80,\"h\":80}},{\"id\":4,\"required\":1,\"img\":{\"type\":3,\"w\":1200,\"h\":627}},{\"id\":5,\"data\":{\"type\":3}},{\"id\":6,\"required\":1,\"data\":{\"type\":2,\"len\":100}}]}","ver":"1.0"},"tagid":"1","bidfloor":0.6,"bidfloorcur":"USD"}],"site":{"id":"1012864","domain":"www.abc.com","cat":["IAB3"],"mobile":1,"keywords":"apps,games,discovery,recommendation"},"device":{"dnt":1,"ua":"Mozilla/5.0 (Linux; U; Android 4.2.2; km-kh; SHV-E120S Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30","ip":"175.100.59.170","geo":{"lat":11.5625,"lon":104.916,"country":"KHM","region":"12","city":"Phnom Penh","type":2},"carrier":"Viettel (cambodia) Pte., Ltd.","language":"km","model":"android","os":"Android","osv":"4.2.2","connectiontype":2,"devicetype":1},"user":{"id":"325a32d3-1dba-5ffc-82f2-1df428520728"},"at":2,"tmax":100,"wseat":["74","17","30","142","167","177","153","7","90","140","148","164","104","71","19","187","139","63","88","160","222","205","46"],"cur":["USD"]}`)
func BenchmarkNativeGzip(b *testing.B) {
b.SetBytes(int64(len(bidReq)))
for i := 0; i < b.N; i++ {
b := bytes.NewBuffer(nil)
w := gzip.NewWriter(b)
w.Write(bidReq)
w.Close()
}
}
func BenchmarkKlauspostGzip(b *testing.B) {
b.SetBytes(int64(len(bidReq)))
for i := 0; i < b.N; i++ {
b := bytes.NewBuffer(nil)
w := ogzip.NewWriter(b)
w.Write(bidReq)
w.Close()
}
}
func BenchmarkNativeGzipReset(b *testing.B) {
b.SetBytes(int64(len(bidReq)))
w := gzip.NewWriter(nil)
bb := bytes.NewBuffer(nil)
for i := 0; i < b.N; i++ {
bb.Reset()
w.Reset(bb)
w.Write(bidReq)
w.Close()
}
}
func BenchmarkKlauspostGzipReset(b *testing.B) {
b.SetBytes(int64(len(bidReq)))
w := ogzip.NewWriter(nil)
bb := bytes.NewBuffer(nil)
for i := 0; i < b.N; i++ {
bb.Reset()
w.Reset(bb)
w.Write(bidReq)
w.Close()
}
//b.Log("Size", bb.Len())
}
func BenchmarkNativeGzip1(b *testing.B) {
b.SetBytes(int64(len(bidReq)))
bb := bytes.NewBuffer(nil)
for i := 0; i < b.N; i++ {
bb.Reset()
w, _ := gzip.NewWriterLevel(bb, 1)
w.Write(bidReq)
w.Close()
}
}
func BenchmarkKlauspostGzip1(b *testing.B) {
b.SetBytes(int64(len(bidReq)))
bb := bytes.NewBuffer(nil)
for i := 0; i < b.N; i++ {
bb.Reset()
w, _ := ogzip.NewWriterLevel(bb, 1)
w.Write(bidReq)
w.Close()
}
}
func BenchmarkNativeGzip1Reset(b *testing.B) {
b.SetBytes(int64(len(bidReq)))
w, _ := gzip.NewWriterLevel(nil, 1)
bb := bytes.NewBuffer(nil)
for i := 0; i < b.N; i++ {
bb.Reset()
w.Reset(bb)
w.Write(bidReq)
w.Close()
}
}
func BenchmarkKlauspostGzip1Reset(b *testing.B) {
b.SetBytes(int64(len(bidReq)))
w, _ := ogzip.NewWriterLevel(nil, 1)
bb := bytes.NewBuffer(nil)
for i := 0; i < b.N; i++ {
bb.Reset()
w.Reset(bb)
w.Write(bidReq)
w.Close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment