Last active
May 7, 2019 00:57
-
-
Save shibukawa/799f34831fce1c1e6a5077ca2bf7701a to your computer and use it in GitHub Desktop.
pure go compression benchmark
This file contains 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/flate" | |
"github.com/golang/snappy" | |
"github.com/pierrec/lz4" | |
"io" | |
"io/ioutil" | |
"testing" | |
) | |
const sourceFile = "linux-2.2.0.tar" | |
var source []byte | |
func init() { | |
var err error | |
source, err = ioutil.ReadFile(sourceFile) | |
if err != nil { | |
panic(err) | |
} | |
} | |
func BenchmarkLZ4(b *testing.B) { | |
w := lz4.NewWriter(ioutil.Discard) | |
w.Write(source) | |
w.Close() | |
} | |
func BenchmarkSnappy(b *testing.B) { | |
w := snappy.NewWriter(ioutil.Discard) | |
w.Write(source) | |
w.Close() | |
} | |
func BenchmarkSnappy2(b *testing.B) { | |
w := snappy.NewBufferedWriter(ioutil.Discard) | |
w.Write(source) | |
w.Close() | |
} | |
func BenchmarkLZ4_Reader(b *testing.B) { | |
var buffer bytes.Buffer | |
w := lz4.NewWriter(&buffer) | |
w.Write(source) | |
w.Close() | |
b.ResetTimer() | |
r := lz4.NewReader(&buffer) | |
io.Copy(ioutil.Discard, r) | |
} | |
func BenchmarkSnappy_Reader(b *testing.B) { | |
var buffer bytes.Buffer | |
w := snappy.NewWriter(&buffer) | |
w.Write(source) | |
w.Close() | |
b.ResetTimer() | |
r := snappy.NewReader(&buffer) | |
io.Copy(ioutil.Discard, r) | |
} | |
func BenchmarkFlate(b *testing.B) { | |
w, _ := flate.NewWriter(ioutil.Discard, flate.DefaultCompression) | |
w.Write(source) | |
w.Close() | |
} | |
func BenchmarkFlate_Reader(b *testing.B) { | |
var buffer bytes.Buffer | |
w, _ := flate.NewWriter(&buffer, flate.DefaultCompression) | |
w.Write(source) | |
w.Close() | |
b.ResetTimer() | |
r := flate.NewReader(&buffer) | |
io.Copy(ioutil.Discard, r) | |
} |
This file contains 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
% go test -bench . -benchmem | |
goos: darwin | |
goarch: amd64 | |
pkg: bench | |
BenchmarkLZ4-8 2000000000 0.14 ns/op 0 B/op 0 allocs/op | |
BenchmarkSnappy-8 2000000000 0.07 ns/op 0 B/op 0 allocs/op | |
BenchmarkSnappy2-8 2000000000 0.07 ns/op 0 B/op 0 allocs/op | |
BenchmarkLZ4_Reader-8 2000000000 0.07 ns/op 0 B/op 0 allocs/op | |
BenchmarkSnappy_Reader-8 2000000000 0.03 ns/op 0 B/op 0 allocs/op | |
BenchmarkFlate-8 1 2156897975 ns/op 813696 B/op 16 allocs/op | |
BenchmarkFlate_Reader-8 2000000000 0.17 ns/op 0 B/op 0 allocs/op | |
PASS | |
ok bench 54.229s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment