OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // This benchmark tests gzip and gunzip performance. | 5 // This benchmark tests gzip and gunzip performance. |
6 | 6 |
7 package go1 | 7 package go1 |
8 | 8 |
9 import ( | 9 import ( |
10 "bytes" | 10 "bytes" |
11 gz "compress/gzip" | 11 gz "compress/gzip" |
12 "io" | 12 "io" |
13 "io/ioutil" | 13 "io/ioutil" |
14 "testing" | 14 "testing" |
15 ) | 15 ) |
16 | 16 |
17 var ( | 17 var ( |
18 jsongunz = bytes.Repeat(jsonbytes, 10) | 18 jsongunz = bytes.Repeat(jsonbytes, 10) |
19 jsongz []byte | 19 jsongz []byte |
20 ) | 20 ) |
21 | 21 |
22 func init() { | 22 func init() { |
23 var buf bytes.Buffer | 23 var buf bytes.Buffer |
24 » c, err := gz.NewWriter(&buf) | 24 » c := gz.NewWriter(&buf) |
25 » if err != nil { | |
26 » » panic(err) | |
27 » } | |
28 c.Write(jsongunz) | 25 c.Write(jsongunz) |
29 c.Close() | 26 c.Close() |
30 jsongz = buf.Bytes() | 27 jsongz = buf.Bytes() |
31 } | 28 } |
32 | 29 |
33 func gzip() { | 30 func gzip() { |
34 » c, err := gz.NewWriter(ioutil.Discard) | 31 » c := gz.NewWriter(ioutil.Discard) |
35 » if err != nil { | |
36 » » panic(err) | |
37 » } | |
38 if _, err := c.Write(jsongunz); err != nil { | 32 if _, err := c.Write(jsongunz); err != nil { |
39 panic(err) | 33 panic(err) |
40 } | 34 } |
41 if err := c.Close(); err != nil { | 35 if err := c.Close(); err != nil { |
42 panic(err) | 36 panic(err) |
43 } | 37 } |
44 } | 38 } |
45 | 39 |
46 func gunzip() { | 40 func gunzip() { |
47 r, err := gz.NewReader(bytes.NewBuffer(jsongz)) | 41 r, err := gz.NewReader(bytes.NewBuffer(jsongz)) |
(...skipping 12 matching lines...) Expand all Loading... |
60 gzip() | 54 gzip() |
61 } | 55 } |
62 } | 56 } |
63 | 57 |
64 func BenchmarkGunzip(b *testing.B) { | 58 func BenchmarkGunzip(b *testing.B) { |
65 b.SetBytes(int64(len(jsongunz))) | 59 b.SetBytes(int64(len(jsongunz))) |
66 for i := 0; i < b.N; i++ { | 60 for i := 0; i < b.N; i++ { |
67 gunzip() | 61 gunzip() |
68 } | 62 } |
69 } | 63 } |
OLD | NEW |