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 ...@@ -6,8 +6,10 @@ package lzw
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"math"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
...@@ -118,16 +120,21 @@ func TestReader(t *testing.T) { ...@@ -118,16 +120,21 @@ func TestReader(t *testing.T) {
} }
} }
func benchmarkDecoder(b *testing.B, n int) { func BenchmarkDecoder(b *testing.B) {
b.StopTimer() buf, err := ioutil.ReadFile("../testdata/e.txt")
b.SetBytes(int64(n))
buf0, err := ioutil.ReadFile("../testdata/e.txt")
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
if len(buf0) == 0 { if len(buf) == 0 {
b.Fatalf("test file has no data") 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) compressed := new(bytes.Buffer)
w := NewWriter(compressed, LSB, 8) w := NewWriter(compressed, LSB, 8)
for i := 0; i < n; i += len(buf0) { for i := 0; i < n; i += len(buf0) {
...@@ -144,16 +151,6 @@ func benchmarkDecoder(b *testing.B, n int) { ...@@ -144,16 +151,6 @@ func benchmarkDecoder(b *testing.B, n int) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
io.Copy(ioutil.Discard, NewReader(bytes.NewReader(buf1), LSB, 8)) 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 @@ ...@@ -5,9 +5,11 @@
package lzw package lzw
import ( import (
"fmt"
"internal/testenv" "internal/testenv"
"io" "io"
"io/ioutil" "io/ioutil"
"math"
"os" "os"
"runtime" "runtime"
"testing" "testing"
...@@ -122,16 +124,18 @@ func TestSmallLitWidth(t *testing.T) { ...@@ -122,16 +124,18 @@ func TestSmallLitWidth(t *testing.T) {
} }
} }
func benchmarkEncoder(b *testing.B, n int) { func BenchmarkEncoder(b *testing.B) {
b.StopTimer() buf, err := ioutil.ReadFile("../testdata/e.txt")
b.SetBytes(int64(n))
buf0, err := ioutil.ReadFile("../testdata/e.txt")
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
if len(buf0) == 0 { if len(buf) == 0 {
b.Fatalf("test file has no data") b.Fatalf("test file has no data")
} }
for e := 4; e <= 6; e++ {
n := int(math.Pow10(e))
buf0 := buf
buf1 := make([]byte, n) buf1 := make([]byte, n)
for i := 0; i < n; i += len(buf0) { for i := 0; i < n; i += len(buf0) {
if len(buf0) > n-i { if len(buf0) > n-i {
...@@ -141,22 +145,13 @@ func benchmarkEncoder(b *testing.B, n int) { ...@@ -141,22 +145,13 @@ func benchmarkEncoder(b *testing.B, n int) {
} }
buf0 = nil buf0 = nil
runtime.GC() 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++ { for i := 0; i < b.N; i++ {
w := NewWriter(ioutil.Discard, LSB, 8) w := NewWriter(ioutil.Discard, LSB, 8)
w.Write(buf1) w.Write(buf1)
w.Close() 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