forked from kopia/kopia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_benchmark_compression.go
More file actions
362 lines (276 loc) · 9.2 KB
/
Copy pathcommand_benchmark_compression.go
File metadata and controls
362 lines (276 loc) · 9.2 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package cli
import (
"bytes"
"context"
"hash/fnv"
"io"
"os"
"runtime"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/gather"
"github.com/kopia/kopia/internal/timetrack"
"github.com/kopia/kopia/internal/units"
"github.com/kopia/kopia/repo/compression"
)
const defaultCompressedDataByMethod = 128 << 20 // 128 MB
type commandBenchmarkCompression struct {
repeat int
dataFile string
bySize bool
byAllocated bool
verifyStable bool
optionPrint bool
parallel int
deprecated bool
operations string
algorithms string
out textOutput
}
func (c *commandBenchmarkCompression) setup(svc appServices, parent commandParent) {
cmd := parent.Command("compression", "Run compression benchmarks")
cmd.Flag("repeat", "Number of repetitions").Default("0").IntVar(&c.repeat)
cmd.Flag("data-file", "Use data from the given file").Required().ExistingFileVar(&c.dataFile)
cmd.Flag("by-size", "Sort results by size").BoolVar(&c.bySize)
cmd.Flag("by-alloc", "Sort results by allocated bytes").BoolVar(&c.byAllocated)
cmd.Flag("parallel", "Number of parallel goroutines").Default("1").IntVar(&c.parallel)
cmd.Flag("operations", "Operations").Default("both").EnumVar(&c.operations, "compress", "decompress", "both")
cmd.Flag("verify-stable", "Verify that compression is stable").BoolVar(&c.verifyStable)
cmd.Flag("print-options", "Print out options usable for repository creation").BoolVar(&c.optionPrint)
cmd.Flag("deprecated", "Included deprecated compression algorithms").BoolVar(&c.deprecated)
cmd.Flag("algorithms", "Comma-separated list of algorithms to benchmark").StringVar(&c.algorithms)
cmd.Action(svc.noRepositoryAction(c.run))
c.out.setup(svc)
}
func (c *commandBenchmarkCompression) readInputFile(ctx context.Context) ([]byte, error) {
f, err := os.Open(c.dataFile)
if err != nil {
return nil, errors.Wrap(err, "error opening input file")
}
defer f.Close() //nolint:errcheck
st, err := f.Stat()
if err != nil {
return nil, errors.Wrap(err, "stat error")
}
dataLength := st.Size()
if dataLength > defaultCompressedDataByMethod {
dataLength = defaultCompressedDataByMethod
log(ctx).Infof("NOTICE: The provided input file is too big, using first %v.", units.BytesStringBase2(dataLength))
}
data := make([]byte, dataLength)
if _, err := io.ReadFull(f, data); err != nil {
return nil, errors.Wrap(err, "error reading file")
}
return data, nil
}
type compressionBenchmarkResult struct {
compression compression.Name
throughput float64
compressedSize uint64
allocations uint64
allocBytes uint64
}
func (c *commandBenchmarkCompression) shouldIncludeAlgorithm(name compression.Name) bool {
if c.algorithms == "" {
if compression.IsDeprecated[name] && !c.deprecated {
return false
}
return true
}
for a := range strings.SplitSeq(c.algorithms, ",") {
if strings.HasPrefix(string(name), a) {
return true
}
}
return false
}
func (c *commandBenchmarkCompression) run(ctx context.Context) error {
var benchmarkCompression, benchmarkDecompression bool
data, err := c.readInputFile(ctx)
if err != nil {
return err
}
if len(data) == 0 {
return errors.New("empty data file")
}
repeatCount := c.repeat
if repeatCount == 0 {
repeatCount = defaultCompressedDataByMethod / len(data)
if repeatCount == 0 {
repeatCount = 1
}
}
algorithms := map[compression.Name]compression.Compressor{}
for name, comp := range compression.ByName {
if c.shouldIncludeAlgorithm(name) {
algorithms[name] = comp
}
}
log(ctx).Infof("Will repeat each benchmark %v times per compression method (total %v). Override with --repeat=N.", repeatCount, units.BytesString(repeatCount*len(data)))
switch c.operations {
case "compress":
benchmarkCompression = true
benchmarkDecompression = false
case "decompress":
benchmarkCompression = false
benchmarkDecompression = true
default:
benchmarkCompression = true
benchmarkDecompression = true
}
if benchmarkCompression {
if err := c.runCompression(ctx, data, repeatCount, algorithms); err != nil {
return err
}
}
if benchmarkDecompression {
if err := c.runDecompression(ctx, data, repeatCount, algorithms); err != nil {
return err
}
}
return nil
}
func (c *commandBenchmarkCompression) runCompression(ctx context.Context, data []byte, repeatCount int, algorithms map[compression.Name]compression.Compressor) error {
var results []compressionBenchmarkResult
log(ctx).Infof("Compressing input file %q (%v) using %v compression methods.", c.dataFile, units.BytesString(len(data)), len(algorithms))
for name, comp := range algorithms {
log(ctx).Infof("Benchmarking compressor '%v'...", name)
cnt := repeatCount
runtime.GC()
var startMS, endMS runtime.MemStats
run := func(compressed *bytes.Buffer) uint64 {
var (
compressedSize uint64
lastHash uint64
input = bytes.NewReader(nil)
)
for i := range cnt {
compressed.Reset()
input.Reset(data)
if err := comp.Compress(compressed, input); err != nil {
log(ctx).Errorf("compression %q failed: %v", name, err)
continue
}
compressedSize = uint64(compressed.Len()) //nolint:gosec
if c.verifyStable {
h := hashOf(compressed.Bytes())
if i == 0 {
lastHash = h
} else if h != lastHash {
log(ctx).Errorf("compression %q is not stable", name)
continue
}
}
}
return compressedSize
}
outputBuffers := makeOutputBuffers(c.parallel, defaultCompressedDataByMethod)
tt := timetrack.Start()
runtime.ReadMemStats(&startMS)
compressedSize := runInParallel(outputBuffers, run)
runtime.ReadMemStats(&endMS)
_, perSecond := tt.Completed(float64(c.parallel) * float64(len(data)) * float64(cnt))
results = append(results,
compressionBenchmarkResult{
compression: name,
throughput: perSecond,
compressedSize: compressedSize,
allocations: endMS.Mallocs - startMS.Mallocs,
allocBytes: endMS.TotalAlloc - startMS.TotalAlloc,
})
}
c.sortResults(results)
c.printResults(results)
return nil
}
func (c *commandBenchmarkCompression) runDecompression(ctx context.Context, data []byte, repeatCount int, algorithms map[compression.Name]compression.Compressor) error {
var results []compressionBenchmarkResult
log(ctx).Infof("Decompressing input file %q (%v) using %v compression methods.", c.dataFile, units.BytesString(len(data)), len(algorithms))
var compressedInput gather.WriteBuffer
defer compressedInput.Close()
for name, comp := range algorithms {
compressedInput.Reset()
if err := comp.Compress(&compressedInput, bytes.NewReader(data)); err != nil {
return errors.Wrapf(err, "unable to compress data using %v", name)
}
compressedInputBytes := compressedInput.ToByteSlice()
log(ctx).Infof("Benchmarking decompressor '%v'...", name)
cnt := repeatCount
runtime.GC()
var startMS, endMS runtime.MemStats
run := func(decompressed *bytes.Buffer) uint64 {
input := bytes.NewReader(nil)
for range cnt {
decompressed.Reset()
input.Reset(compressedInputBytes)
if err := comp.Decompress(decompressed, input, true); err != nil {
log(ctx).Errorf("decompression %q failed: %v", name, err)
}
}
//nolint:gosec
return uint64(compressedInput.Length())
}
outputBuffers := makeOutputBuffers(c.parallel, defaultCompressedDataByMethod)
tt := timetrack.Start()
runtime.ReadMemStats(&startMS)
compressedSize := runInParallel(outputBuffers, run)
runtime.ReadMemStats(&endMS)
_, perSecond := tt.Completed(float64(c.parallel) * float64(len(data)) * float64(cnt))
results = append(results,
compressionBenchmarkResult{
compression: name,
throughput: perSecond,
compressedSize: compressedSize,
allocations: endMS.Mallocs - startMS.Mallocs,
allocBytes: endMS.TotalAlloc - startMS.TotalAlloc,
})
}
c.sortResults(results)
c.printResults(results)
return nil
}
func (c *commandBenchmarkCompression) sortResults(results []compressionBenchmarkResult) {
switch {
case c.bySize:
sort.Slice(results, func(i, j int) bool {
return results[i].compressedSize < results[j].compressedSize
})
case c.byAllocated:
sort.Slice(results, func(i, j int) bool {
return results[i].allocBytes < results[j].allocBytes
})
default:
sort.Slice(results, func(i, j int) bool {
return results[i].throughput > results[j].throughput
})
}
}
func (c *commandBenchmarkCompression) printResults(results []compressionBenchmarkResult) {
c.out.printStdout(" %-26v %-12v %-12v %v\n", "Compression", "Compressed", "Throughput", "Allocs Memory Usage")
c.out.printStdout("------------------------------------------------------------------------------------------------\n")
for ndx, r := range results {
maybeDeprecated := ""
if compression.IsDeprecated[r.compression] {
maybeDeprecated = " (deprecated)"
}
c.out.printStdout("%3d. %-26v %-12v %8v/s %-8v %v%v",
ndx,
r.compression,
units.BytesString(r.compressedSize),
units.BytesString(r.throughput),
r.allocations,
units.BytesString(r.allocBytes),
maybeDeprecated,
)
if c.optionPrint {
c.out.printStdout(", --compression=%s", r.compression)
}
c.out.printStdout("\n")
}
}
func hashOf(b []byte) uint64 {
h := fnv.New64a()
h.Write(b)
return h.Sum64()
}