-
Notifications
You must be signed in to change notification settings - Fork 142
/
bench_test.go
199 lines (164 loc) · 4.42 KB
/
bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package lz4_test
import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/pierrec/lz4/v4"
"github.com/pierrec/lz4/v4/internal/lz4block"
)
func BenchmarkCompress(b *testing.B) {
buf := make([]byte, len(pg1661))
var c lz4.Compressor
n, _ := c.CompressBlock(pg1661, buf)
b.ReportAllocs()
b.ResetTimer()
b.ReportMetric(float64(n), "outbytes")
for i := 0; i < b.N; i++ {
_, _ = c.CompressBlock(pg1661, buf)
}
}
func BenchmarkCompressRandom(b *testing.B) {
buf := make([]byte, lz4.CompressBlockBound(len(random)))
var c lz4.Compressor
n, _ := c.CompressBlock(random, buf)
b.ReportAllocs()
b.SetBytes(int64(len(random)))
b.ResetTimer()
b.ReportMetric(float64(n), "outbytes")
for i := 0; i < b.N; i++ {
_, _ = c.CompressBlock(random, buf)
}
}
func BenchmarkCompressHC(b *testing.B) {
buf := make([]byte, len(pg1661))
c := lz4.CompressorHC{Level: 16}
n, _ := c.CompressBlock(pg1661, buf)
b.ReportAllocs()
b.ResetTimer()
b.ReportMetric(float64(n), "outbytes")
for i := 0; i < b.N; i++ {
_, _ = c.CompressBlock(pg1661, buf)
}
}
func BenchmarkUncompress(b *testing.B) {
buf := make([]byte, len(pg1661))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = lz4block.UncompressBlock(pg1661LZ4, buf, nil)
}
}
func mustLoadFile(f string) []byte {
var b []byte
var err error
if strings.HasSuffix(f, ".gz") {
b, err = loadGoldenGz(f)
} else {
b, err = ioutil.ReadFile(f)
}
if err != nil {
panic(err)
}
return b
}
var (
pg1661 = mustLoadFile("testdata/pg1661.txt.gz")
digits = mustLoadFile("testdata/e.txt.gz")
twain = mustLoadFile("testdata/Mark.Twain-Tom.Sawyer.txt.gz")
random = mustLoadFile("testdata/random.data.gz")
pg1661LZ4 = mustLoadFile("testdata/pg1661.txt.lz4")
digitsLZ4 = mustLoadFile("testdata/e.txt.lz4")
twainLZ4 = mustLoadFile("testdata/Mark.Twain-Tom.Sawyer.txt.lz4")
randomLZ4 = mustLoadFile("testdata/random.data.lz4")
)
func benchmarkUncompress(b *testing.B, compressed []byte) {
r := bytes.NewReader(compressed)
zr := lz4.NewReader(r)
// Decompress once to determine the uncompressed size of testfile.
_, err := io.Copy(ioutil.Discard, zr)
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(len(compressed)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Reset(compressed)
zr.Reset(r)
_, _ = io.Copy(ioutil.Discard, zr)
}
}
func BenchmarkUncompressPg1661(b *testing.B) { benchmarkUncompress(b, pg1661LZ4) }
func BenchmarkUncompressDigits(b *testing.B) { benchmarkUncompress(b, digitsLZ4) }
func BenchmarkUncompressTwain(b *testing.B) { benchmarkUncompress(b, twainLZ4) }
func BenchmarkUncompressRand(b *testing.B) { benchmarkUncompress(b, randomLZ4) }
func benchmarkCompress(b *testing.B, uncompressed []byte) {
w := bytes.NewBuffer(nil)
zw := lz4.NewWriter(w)
r := bytes.NewReader(uncompressed)
// Compress once to determine the compressed size of testfile.
_, err := io.Copy(zw, r)
if err != nil {
b.Fatal(err)
}
if err := zw.Close(); err != nil {
b.Fatal(err)
}
b.SetBytes(int64(len(uncompressed)))
b.ReportAllocs()
b.ResetTimer()
b.ReportMetric(float64(w.Len()), "outbytes")
for i := 0; i < b.N; i++ {
r.Reset(uncompressed)
zw.Reset(w)
_, _ = io.Copy(zw, r)
}
}
func BenchmarkCompressPg1661(b *testing.B) { benchmarkCompress(b, pg1661) }
func BenchmarkCompressDigits(b *testing.B) { benchmarkCompress(b, digits) }
func BenchmarkCompressTwain(b *testing.B) { benchmarkCompress(b, twain) }
func BenchmarkCompressRand(b *testing.B) { benchmarkCompress(b, random) }
// Benchmark to check reallocations upon Reset().
// See issue https://github.com/pierrec/lz4/issues/52.
func BenchmarkWriterReset(b *testing.B) {
b.ReportAllocs()
zw := lz4.NewWriter(nil)
src := mustLoadFile("testdata/gettysburg.txt")
var buf bytes.Buffer
for n := 0; n < b.N; n++ {
buf.Reset()
zw.Reset(&buf)
_, _ = zw.Write(src)
_ = zw.Close()
}
}
// Golden files are compressed with gzip.
func loadGolden(t *testing.T, fname string) []byte {
fname = strings.Replace(fname, ".lz4", ".gz", 1)
t.Helper()
b, err := loadGoldenGz(fname)
if err != nil {
t.Fatal(err)
}
return b
}
func loadGoldenGz(fname string) ([]byte, error) {
file, err := os.Open(fname)
if err != nil {
return nil, err
}
defer file.Close()
gzr, err := gzip.NewReader(file)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, gzr); err != nil {
return nil, err
}
return buf.Bytes(), nil
}