Commit 5dd922c9 authored by Marcel van Lohuizen's avatar Marcel van Lohuizen

compress/lzw: use Run for benchmarks

load file only once per group.

Change-Id: I965661507055e6e100506bf14d37133ecdd2cc5e
Reviewed-on: https://go-review.googlesource.com/23423Reviewed-by: default avatarRuss Cox <rsc@golang.org>
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 095fbdcc
......@@ -6,8 +6,10 @@ package lzw
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"math"
"runtime"
"strconv"
"strings"
......@@ -118,16 +120,21 @@ func TestReader(t *testing.T) {
}
}
func benchmarkDecoder(b *testing.B, n int) {
b.StopTimer()
b.SetBytes(int64(n))
buf0, err := ioutil.ReadFile("../testdata/e.txt")
func BenchmarkDecoder(b *testing.B) {
buf, err := ioutil.ReadFile("../testdata/e.txt")
if err != nil {
b.Fatal(err)
}
if len(buf0) == 0 {
if len(buf) == 0 {
b.Fatalf("test file has no data")
}
for e := 4; e <= 6; e++ {
n := int(math.Pow10(e))
b.Run(fmt.Sprint("1e", e), func(b *testing.B) {
b.StopTimer()
b.SetBytes(int64(n))
buf0 := buf
compressed := new(bytes.Buffer)
w := NewWriter(compressed, LSB, 8)
for i := 0; i < n; i += len(buf0) {
......@@ -144,16 +151,6 @@ func benchmarkDecoder(b *testing.B, n int) {
for i := 0; i < b.N; i++ {
io.Copy(ioutil.Discard, NewReader(bytes.NewReader(buf1), LSB, 8))
}
}
func BenchmarkDecoder1e4(b *testing.B) {
benchmarkDecoder(b, 1e4)
}
func BenchmarkDecoder1e5(b *testing.B) {
benchmarkDecoder(b, 1e5)
}
func BenchmarkDecoder1e6(b *testing.B) {
benchmarkDecoder(b, 1e6)
})
}
}
......@@ -5,9 +5,11 @@
package lzw
import (
"fmt"
"internal/testenv"
"io"
"io/ioutil"
"math"
"os"
"runtime"
"testing"
......@@ -122,16 +124,18 @@ func TestSmallLitWidth(t *testing.T) {
}
}
func benchmarkEncoder(b *testing.B, n int) {
b.StopTimer()
b.SetBytes(int64(n))
buf0, err := ioutil.ReadFile("../testdata/e.txt")
func BenchmarkEncoder(b *testing.B) {
buf, err := ioutil.ReadFile("../testdata/e.txt")
if err != nil {
b.Fatal(err)
}
if len(buf0) == 0 {
if len(buf) == 0 {
b.Fatalf("test file has no data")
}
for e := 4; e <= 6; e++ {
n := int(math.Pow10(e))
buf0 := buf
buf1 := make([]byte, n)
for i := 0; i < n; i += len(buf0) {
if len(buf0) > n-i {
......@@ -141,22 +145,13 @@ func benchmarkEncoder(b *testing.B, n int) {
}
buf0 = nil
runtime.GC()
b.StartTimer()
b.Run(fmt.Sprint("1e", e), func(b *testing.B) {
b.SetBytes(int64(n))
for i := 0; i < b.N; i++ {
w := NewWriter(ioutil.Discard, LSB, 8)
w.Write(buf1)
w.Close()
}
}
func BenchmarkEncoder1e4(b *testing.B) {
benchmarkEncoder(b, 1e4)
}
func BenchmarkEncoder1e5(b *testing.B) {
benchmarkEncoder(b, 1e5)
}
func BenchmarkEncoder1e6(b *testing.B) {
benchmarkEncoder(b, 1e6)
})
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment