mirror of
https://github.com/golang/go
synced 2024-11-20 04:14:49 -07:00
compress/flate: add Encoder/Decoder benchmarks
In CL 6127051, nigeltao suggested that further gains were possible by improving the performance of flate. This CL adds a set of benchmarks (based on compress/lzw) that can be used to judge any future improvements. R=nigeltao CC=golang-dev https://golang.org/cl/6128049
This commit is contained in:
parent
24cce5c60c
commit
d5b299eda2
75
src/pkg/compress/flate/reader_test.go
Normal file
75
src/pkg/compress/flate/reader_test.go
Normal file
@ -0,0 +1,75 @@
|
||||
// Copyright 2012 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package flate
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func benchmarkDecoder(b *testing.B, level, n int) {
|
||||
b.StopTimer()
|
||||
b.SetBytes(int64(n))
|
||||
buf0, err := ioutil.ReadFile("../testdata/e.txt")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
buf0 = buf0[:10000]
|
||||
compressed := new(bytes.Buffer)
|
||||
w, err := NewWriter(compressed, level)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
for i := 0; i < n; i += len(buf0) {
|
||||
io.Copy(w, bytes.NewBuffer(buf0))
|
||||
}
|
||||
w.Close()
|
||||
buf1 := compressed.Bytes()
|
||||
buf0, compressed, w = nil, nil, nil
|
||||
runtime.GC()
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
io.Copy(ioutil.Discard, NewReader(bytes.NewBuffer(buf1)))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDecoderBestSpeed1K(b *testing.B) {
|
||||
benchmarkDecoder(b, BestSpeed, 1e4)
|
||||
}
|
||||
|
||||
func BenchmarkDecoderBestSpeed10K(b *testing.B) {
|
||||
benchmarkDecoder(b, BestSpeed, 1e5)
|
||||
}
|
||||
|
||||
func BenchmarkDecoderBestSpeed100K(b *testing.B) {
|
||||
benchmarkDecoder(b, BestSpeed, 1e6)
|
||||
}
|
||||
|
||||
func BenchmarkDecoderDefaultCompression1K(b *testing.B) {
|
||||
benchmarkDecoder(b, DefaultCompression, 1e4)
|
||||
}
|
||||
|
||||
func BenchmarkDecoderDefaultCompression10K(b *testing.B) {
|
||||
benchmarkDecoder(b, DefaultCompression, 1e5)
|
||||
}
|
||||
|
||||
func BenchmarkDecoderDefaultCompression100K(b *testing.B) {
|
||||
benchmarkDecoder(b, DefaultCompression, 1e6)
|
||||
}
|
||||
|
||||
func BenchmarkDecoderBestCompression1K(b *testing.B) {
|
||||
benchmarkDecoder(b, BestCompression, 1e4)
|
||||
}
|
||||
|
||||
func BenchmarkDecoderBestCompression10K(b *testing.B) {
|
||||
benchmarkDecoder(b, BestCompression, 1e5)
|
||||
}
|
||||
|
||||
func BenchmarkDecoderBestCompression100K(b *testing.B) {
|
||||
benchmarkDecoder(b, BestCompression, 1e6)
|
||||
}
|
72
src/pkg/compress/flate/writer_test.go
Normal file
72
src/pkg/compress/flate/writer_test.go
Normal file
@ -0,0 +1,72 @@
|
||||
// Copyright 2012 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package flate
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func benchmarkEncoder(b *testing.B, level, n int) {
|
||||
b.StopTimer()
|
||||
b.SetBytes(int64(n))
|
||||
buf0, err := ioutil.ReadFile("../testdata/e.txt")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
buf0 = buf0[:10000]
|
||||
buf1 := make([]byte, n)
|
||||
for i := 0; i < n; i += len(buf0) {
|
||||
copy(buf1[i:], buf0)
|
||||
}
|
||||
buf0 = nil
|
||||
runtime.GC()
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
w, err := NewWriter(ioutil.Discard, level)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
w.Write(buf1)
|
||||
w.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkEncoderBestSpeed1K(b *testing.B) {
|
||||
benchmarkEncoder(b, BestSpeed, 1e4)
|
||||
}
|
||||
|
||||
func BenchmarkEncoderBestSpeed10K(b *testing.B) {
|
||||
benchmarkEncoder(b, BestSpeed, 1e5)
|
||||
}
|
||||
|
||||
func BenchmarkEncoderBestSpeed100K(b *testing.B) {
|
||||
benchmarkEncoder(b, BestSpeed, 1e6)
|
||||
}
|
||||
|
||||
func BenchmarkEncoderDefaultCompression1K(b *testing.B) {
|
||||
benchmarkEncoder(b, DefaultCompression, 1e4)
|
||||
}
|
||||
|
||||
func BenchmarkEncoderDefaultCompression10K(b *testing.B) {
|
||||
benchmarkEncoder(b, DefaultCompression, 1e5)
|
||||
}
|
||||
|
||||
func BenchmarkEncoderDefaultCompression100K(b *testing.B) {
|
||||
benchmarkEncoder(b, DefaultCompression, 1e6)
|
||||
}
|
||||
|
||||
func BenchmarkEncoderBestCompression1K(b *testing.B) {
|
||||
benchmarkEncoder(b, BestCompression, 1e4)
|
||||
}
|
||||
|
||||
func BenchmarkEncoderBestCompression10K(b *testing.B) {
|
||||
benchmarkEncoder(b, BestCompression, 1e5)
|
||||
}
|
||||
|
||||
func BenchmarkEncoderBestCompression100K(b *testing.B) {
|
||||
benchmarkEncoder(b, BestCompression, 1e6)
|
||||
}
|
Loading…
Reference in New Issue
Block a user