github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/compress/flate/writer_test.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package flate
     6  
     7  import (
     8  	"io/ioutil"
     9  	"runtime"
    10  	"testing"
    11  )
    12  
    13  func benchmarkEncoder(b *testing.B, testfile, level, n int) {
    14  	b.StopTimer()
    15  	b.SetBytes(int64(n))
    16  	buf0, err := ioutil.ReadFile(testfiles[testfile])
    17  	if err != nil {
    18  		b.Fatal(err)
    19  	}
    20  	if len(buf0) == 0 {
    21  		b.Fatalf("test file %q has no data", testfiles[testfile])
    22  	}
    23  	buf1 := make([]byte, n)
    24  	for i := 0; i < n; i += len(buf0) {
    25  		if len(buf0) > n-i {
    26  			buf0 = buf0[:n-i]
    27  		}
    28  		copy(buf1[i:], buf0)
    29  	}
    30  	buf0 = nil
    31  	runtime.GC()
    32  	b.StartTimer()
    33  	for i := 0; i < b.N; i++ {
    34  		w, err := NewWriter(ioutil.Discard, level)
    35  		if err != nil {
    36  			b.Fatal(err)
    37  		}
    38  		w.Write(buf1)
    39  		w.Close()
    40  	}
    41  }
    42  
    43  func BenchmarkEncodeDigitsSpeed1e4(b *testing.B)    { benchmarkEncoder(b, digits, speed, 1e4) }
    44  func BenchmarkEncodeDigitsSpeed1e5(b *testing.B)    { benchmarkEncoder(b, digits, speed, 1e5) }
    45  func BenchmarkEncodeDigitsSpeed1e6(b *testing.B)    { benchmarkEncoder(b, digits, speed, 1e6) }
    46  func BenchmarkEncodeDigitsDefault1e4(b *testing.B)  { benchmarkEncoder(b, digits, default_, 1e4) }
    47  func BenchmarkEncodeDigitsDefault1e5(b *testing.B)  { benchmarkEncoder(b, digits, default_, 1e5) }
    48  func BenchmarkEncodeDigitsDefault1e6(b *testing.B)  { benchmarkEncoder(b, digits, default_, 1e6) }
    49  func BenchmarkEncodeDigitsCompress1e4(b *testing.B) { benchmarkEncoder(b, digits, compress, 1e4) }
    50  func BenchmarkEncodeDigitsCompress1e5(b *testing.B) { benchmarkEncoder(b, digits, compress, 1e5) }
    51  func BenchmarkEncodeDigitsCompress1e6(b *testing.B) { benchmarkEncoder(b, digits, compress, 1e6) }
    52  func BenchmarkEncodeTwainSpeed1e4(b *testing.B)     { benchmarkEncoder(b, twain, speed, 1e4) }
    53  func BenchmarkEncodeTwainSpeed1e5(b *testing.B)     { benchmarkEncoder(b, twain, speed, 1e5) }
    54  func BenchmarkEncodeTwainSpeed1e6(b *testing.B)     { benchmarkEncoder(b, twain, speed, 1e6) }
    55  func BenchmarkEncodeTwainDefault1e4(b *testing.B)   { benchmarkEncoder(b, twain, default_, 1e4) }
    56  func BenchmarkEncodeTwainDefault1e5(b *testing.B)   { benchmarkEncoder(b, twain, default_, 1e5) }
    57  func BenchmarkEncodeTwainDefault1e6(b *testing.B)   { benchmarkEncoder(b, twain, default_, 1e6) }
    58  func BenchmarkEncodeTwainCompress1e4(b *testing.B)  { benchmarkEncoder(b, twain, compress, 1e4) }
    59  func BenchmarkEncodeTwainCompress1e5(b *testing.B)  { benchmarkEncoder(b, twain, compress, 1e5) }
    60  func BenchmarkEncodeTwainCompress1e6(b *testing.B)  { benchmarkEncoder(b, twain, compress, 1e6) }