...

Source file src/github.com/pierrec/lz4/v4/bench_test.go

Documentation: github.com/pierrec/lz4/v4

     1  package lz4_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"io/ioutil"
     7  	"testing"
     8  
     9  	"github.com/pierrec/lz4/v4"
    10  	"github.com/pierrec/lz4/v4/internal/lz4block"
    11  )
    12  
    13  func BenchmarkCompress(b *testing.B) {
    14  	buf := make([]byte, len(pg1661))
    15  	var c lz4.Compressor
    16  
    17  	n, _ := c.CompressBlock(pg1661, buf)
    18  
    19  	b.ReportAllocs()
    20  	b.ResetTimer()
    21  	b.ReportMetric(float64(n), "outbytes")
    22  
    23  	for i := 0; i < b.N; i++ {
    24  		_, _ = c.CompressBlock(pg1661, buf)
    25  	}
    26  }
    27  
    28  func BenchmarkCompressRandom(b *testing.B) {
    29  	buf := make([]byte, lz4.CompressBlockBound(len(random)))
    30  	var c lz4.Compressor
    31  
    32  	n, _ := c.CompressBlock(random, buf)
    33  
    34  	b.ReportAllocs()
    35  	b.SetBytes(int64(len(random)))
    36  	b.ResetTimer()
    37  	b.ReportMetric(float64(n), "outbytes")
    38  
    39  	for i := 0; i < b.N; i++ {
    40  		_, _ = c.CompressBlock(random, buf)
    41  	}
    42  }
    43  
    44  func BenchmarkCompressHC(b *testing.B) {
    45  	buf := make([]byte, len(pg1661))
    46  	c := lz4.CompressorHC{Level: 16}
    47  
    48  	n, _ := c.CompressBlock(pg1661, buf)
    49  
    50  	b.ReportAllocs()
    51  	b.ResetTimer()
    52  	b.ReportMetric(float64(n), "outbytes")
    53  
    54  	for i := 0; i < b.N; i++ {
    55  		_, _ = c.CompressBlock(pg1661, buf)
    56  	}
    57  }
    58  
    59  func BenchmarkUncompress(b *testing.B) {
    60  	buf := make([]byte, len(pg1661))
    61  
    62  	b.ReportAllocs()
    63  	b.ResetTimer()
    64  
    65  	for i := 0; i < b.N; i++ {
    66  		_, _ = lz4block.UncompressBlock(pg1661LZ4, buf, nil)
    67  	}
    68  }
    69  
    70  func mustLoadFile(f string) []byte {
    71  	b, err := ioutil.ReadFile(f)
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  	return b
    76  }
    77  
    78  var (
    79  	pg1661    = mustLoadFile("testdata/pg1661.txt")
    80  	digits    = mustLoadFile("testdata/e.txt")
    81  	twain     = mustLoadFile("testdata/Mark.Twain-Tom.Sawyer.txt")
    82  	random    = mustLoadFile("testdata/random.data")
    83  	pg1661LZ4 = mustLoadFile("testdata/pg1661.txt.lz4")
    84  	digitsLZ4 = mustLoadFile("testdata/e.txt.lz4")
    85  	twainLZ4  = mustLoadFile("testdata/Mark.Twain-Tom.Sawyer.txt.lz4")
    86  	randomLZ4 = mustLoadFile("testdata/random.data.lz4")
    87  )
    88  
    89  func benchmarkUncompress(b *testing.B, compressed []byte) {
    90  	r := bytes.NewReader(compressed)
    91  	zr := lz4.NewReader(r)
    92  
    93  	// Decompress once to determine the uncompressed size of testfile.
    94  	_, err := io.Copy(ioutil.Discard, zr)
    95  	if err != nil {
    96  		b.Fatal(err)
    97  	}
    98  
    99  	b.SetBytes(int64(len(compressed)))
   100  	b.ReportAllocs()
   101  	b.ResetTimer()
   102  
   103  	for i := 0; i < b.N; i++ {
   104  		r.Reset(compressed)
   105  		zr.Reset(r)
   106  		_, _ = io.Copy(ioutil.Discard, zr)
   107  	}
   108  }
   109  
   110  func BenchmarkUncompressPg1661(b *testing.B) { benchmarkUncompress(b, pg1661LZ4) }
   111  func BenchmarkUncompressDigits(b *testing.B) { benchmarkUncompress(b, digitsLZ4) }
   112  func BenchmarkUncompressTwain(b *testing.B)  { benchmarkUncompress(b, twainLZ4) }
   113  func BenchmarkUncompressRand(b *testing.B)   { benchmarkUncompress(b, randomLZ4) }
   114  
   115  func benchmarkCompress(b *testing.B, uncompressed []byte) {
   116  	w := bytes.NewBuffer(nil)
   117  	zw := lz4.NewWriter(w)
   118  	r := bytes.NewReader(uncompressed)
   119  
   120  	// Compress once to determine the compressed size of testfile.
   121  	_, err := io.Copy(zw, r)
   122  	if err != nil {
   123  		b.Fatal(err)
   124  	}
   125  	if err := zw.Close(); err != nil {
   126  		b.Fatal(err)
   127  	}
   128  
   129  	b.SetBytes(int64(len(uncompressed)))
   130  	b.ReportAllocs()
   131  	b.ResetTimer()
   132  	b.ReportMetric(float64(w.Len()), "outbytes")
   133  
   134  	for i := 0; i < b.N; i++ {
   135  		r.Reset(uncompressed)
   136  		zw.Reset(w)
   137  		_, _ = io.Copy(zw, r)
   138  	}
   139  }
   140  
   141  func BenchmarkCompressPg1661(b *testing.B) { benchmarkCompress(b, pg1661) }
   142  func BenchmarkCompressDigits(b *testing.B) { benchmarkCompress(b, digits) }
   143  func BenchmarkCompressTwain(b *testing.B)  { benchmarkCompress(b, twain) }
   144  func BenchmarkCompressRand(b *testing.B)   { benchmarkCompress(b, random) }
   145  
   146  // Benchmark to check reallocations upon Reset().
   147  // See issue https://github.com/pierrec/lz4/issues/52.
   148  func BenchmarkWriterReset(b *testing.B) {
   149  	b.ReportAllocs()
   150  
   151  	zw := lz4.NewWriter(nil)
   152  	src := mustLoadFile("testdata/gettysburg.txt")
   153  	var buf bytes.Buffer
   154  
   155  	for n := 0; n < b.N; n++ {
   156  		buf.Reset()
   157  		zw.Reset(&buf)
   158  
   159  		_, _ = zw.Write(src)
   160  		_ = zw.Close()
   161  	}
   162  }
   163  

View as plain text