Source file test/bench/go1/gzip_test.go

     1  // Copyright 2011 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  // This benchmark tests gzip and gunzip performance.
     6  
     7  package go1
     8  
     9  import (
    10  	"bytes"
    11  	gz "compress/gzip"
    12  	"io"
    13  	"testing"
    14  )
    15  
    16  var (
    17  	jsongunz = bytes.Repeat(jsonbytes, 10)
    18  	jsongz   []byte
    19  )
    20  
    21  func init() {
    22  	var buf bytes.Buffer
    23  	c := gz.NewWriter(&buf)
    24  	c.Write(jsongunz)
    25  	c.Close()
    26  	jsongz = buf.Bytes()
    27  }
    28  
    29  func gzip() {
    30  	c := gz.NewWriter(io.Discard)
    31  	if _, err := c.Write(jsongunz); err != nil {
    32  		panic(err)
    33  	}
    34  	if err := c.Close(); err != nil {
    35  		panic(err)
    36  	}
    37  }
    38  
    39  func gunzip() {
    40  	r, err := gz.NewReader(bytes.NewBuffer(jsongz))
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	if _, err := io.Copy(io.Discard, r); err != nil {
    45  		panic(err)
    46  	}
    47  	r.Close()
    48  }
    49  
    50  func BenchmarkGzip(b *testing.B) {
    51  	b.SetBytes(int64(len(jsongunz)))
    52  	for i := 0; i < b.N; i++ {
    53  		gzip()
    54  	}
    55  }
    56  
    57  func BenchmarkGunzip(b *testing.B) {
    58  	b.SetBytes(int64(len(jsongunz)))
    59  	for i := 0; i < b.N; i++ {
    60  		gunzip()
    61  	}
    62  }
    63  

View as plain text