I have a small change to reuse gzip Writer http://pastebin.com/Hz7nYQMq , performance still the same.
func testCompression(){
w := gzip.NewWriter(ioutil.Discard)
for i:=0;i<10000;i++{
w.Reset(ioutil.Discard)
w.Write(data)
w.Close()
}
}
func main(){
runtime.GOMAXPROCS(1)
testCompression()
}
time ./testgzip real 0m6.612s user 0m6.608s sys 0m0.000s
package main
import (
"compress/gzip"
kgzip "github.com/klauspost/compress/gzip"
"io/ioutil"
"testing"
)
var StdGzipWriter = gzip.NewWriter(ioutil.Discard)
var KlausGzipWriter = kgzip.NewWriter(ioutil.Discard)
func BenchmarkStdGzip(b *testing.B) {
for i := 0; i < b.N; i++ {
StdGzipWriter.Reset(ioutil.Discard)
StdGzipWriter.Write(data)
StdGzipWriter.Close()
}
}
func BenchmarkKlausGzip(b *testing.B) {
for i := 0; i < b.N; i++ {
KlausGzipWriter.Reset(ioutil.Discard)
KlausGzipWriter.Write(data)
KlausGzipWriter.Close()
}
}
var data = ...
$ GOMAXPROCS=1 go test -bench=.
testing: warning: no tests to run
PASS
BenchmarkStdGzip 2000 594023 ns/op
BenchmarkKlausGzip 5000 359110 ns/op
GOMAXPROCS=1 go test -bench=.
2015/08/25 16:54:50 second init
testing: warning: no tests to run
PASS
BenchmarkStdGzip 2000 680051 ns/op
BenchmarkKlausGzip 2000 828096 ns/op
ok github.com/secmask/gget 3.181s
public static void main(String[] args) throws IOException {
byte[] data = Files.readAllBytes(Paths.get("data.txt"));
ByteArrayOutputStream bout = new ByteArrayOutputStream(20*1024);
final int N = 20000;
long start = System.nanoTime();
for(int i=0;i<N;i++){
GZIPOutputStream gzout = new GZIPOutputStream(bout);
gzout.write(data);
gzout.close();
bout.reset();
}
System.out.format("%d ns/op",(System.nanoTime() - start)/N);
}Java: 211536 ns/op
Go (std): 568859 ns/op
Go (klaus): 351132 ns/op
$ GOMAXPROCS=1 go test -bench=.
2015/08/25 17:39:37 second init
testing: warning: no tests to run
PASS
BenchmarkStdGzip 2000 581464 ns/op
BenchmarkKlausGzip 5000 358567 ns/op
ok github.com/secmask/gget 3.064s
$ GOMAXPROCS=1 ./t.test -test.bench .
testing: warning: no tests to run
PASS
BenchmarkStdGzip 11243 -> 3389 byte
11243 -> 3389 byte
11243 -> 3389 byte
3000 563957 ns/op
BenchmarkKlausGzip 11243 -> 3436 byte
11243 -> 3436 byte
11243 -> 3436 byte
5000 353031 ns/op
BenchmarkCGzip 11243 -> 3382 byte
11243 -> 3382 byte
11243 -> 3382 byte
10000 215544 ns/op
@James: all of the test here are based on a static content, in production, we receive the dynamic content from an other services, most of them are really dynamic ( I have buit a cache for it, but cache hit ratio is very low so I remove them then)