Created
May 8, 2020 12:45
-
-
Save erikdubbelboer/e6873706bfa67ae0c1ec3f2f35f0e5b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"compress/gzip" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"testing" | |
"github.com/andybalholm/brotli" | |
) | |
func main() { | |
res, err := http.Get("https://www.google.com/") | |
if err != nil { | |
panic(err) | |
} | |
data, err := ioutil.ReadAll(res.Body) | |
if err != nil { | |
panic(err) | |
} | |
buf := bytes.Buffer{} | |
buf.Grow(len(data)) | |
b := brotli.NewWriter(&buf) | |
n := 0 | |
a := testing.AllocsPerRun(1000, func() { | |
if _, err := b.Write(data); err != nil { | |
panic(err) | |
} | |
if err := b.Flush(); err != nil { | |
panic(err) | |
} | |
n += buf.Len() | |
buf.Reset() | |
b.Reset(&buf) | |
}) | |
fmt.Println("allocs per run:", a) | |
g := gzip.NewWriter(&buf) | |
m := 0 | |
a = testing.AllocsPerRun(1000, func() { | |
if _, err := g.Write(data); err != nil { | |
panic(err) | |
} | |
if err := g.Flush(); err != nil { | |
panic(err) | |
} | |
m += buf.Len() | |
buf.Reset() | |
g.Reset(&buf) | |
}) | |
fmt.Println("allocs per run:", a) | |
fmt.Println("brotli:", n, "gzip:", m, "ratio:", 1-float64(n)/float64(m)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"compress/gzip" | |
"io/ioutil" | |
"net/http" | |
"testing" | |
"github.com/andybalholm/brotli" | |
) | |
var data []byte | |
func init() { | |
res, err := http.Get("https://www.google.com/") | |
if err != nil { | |
panic(err) | |
} | |
data, err = ioutil.ReadAll(res.Body) | |
if err != nil { | |
panic(err) | |
} | |
} | |
func BenchmarkBrotli(b *testing.B) { | |
buf := bytes.Buffer{} | |
buf.Grow(len(data)) | |
o := brotli.NewWriter(&buf) | |
for i := 0; i < b.N; i++ { | |
if _, err := o.Write(data); err != nil { | |
panic(err) | |
} | |
if err := o.Flush(); err != nil { | |
panic(err) | |
} | |
buf.Reset() | |
o.Reset(&buf) | |
} | |
} | |
func BenchmarkGzip(b *testing.B) { | |
buf := bytes.Buffer{} | |
buf.Grow(len(data)) | |
g := gzip.NewWriter(&buf) | |
for i := 0; i < b.N; i++ { | |
if _, err := g.Write(data); err != nil { | |
panic(err) | |
} | |
if err := g.Flush(); err != nil { | |
panic(err) | |
} | |
buf.Reset() | |
g.Reset(&buf) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment