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