import (
"bytes"
+ "fmt"
"io"
"io/ioutil"
+ "math"
"runtime"
"strconv"
"strings"
}
}
-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")
}
- compressed := new(bytes.Buffer)
- w := NewWriter(compressed, LSB, 8)
- for i := 0; i < n; i += len(buf0) {
- if len(buf0) > n-i {
- buf0 = buf0[:n-i]
- }
- w.Write(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.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)
+ 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) {
+ if len(buf0) > n-i {
+ buf0 = buf0[:n-i]
+ }
+ w.Write(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.NewReader(buf1), LSB, 8))
+ }
+ })
+ }
}
package lzw
import (
+ "fmt"
"internal/testenv"
"io"
"io/ioutil"
+ "math"
"os"
"runtime"
"testing"
}
}
-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")
}
- buf1 := make([]byte, n)
- for i := 0; i < n; i += len(buf0) {
- if len(buf0) > n-i {
- buf0 = buf0[:n-i]
+
+ 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 {
+ buf0 = buf0[:n-i]
+ }
+ copy(buf1[i:], buf0)
}
- copy(buf1[i:], buf0)
- }
- buf0 = nil
- runtime.GC()
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- w := NewWriter(ioutil.Discard, LSB, 8)
- w.Write(buf1)
- w.Close()
+ buf0 = nil
+ runtime.GC()
+ 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)
-}