-
Notifications
You must be signed in to change notification settings - Fork 63
/
stream_timing_test.go
65 lines (60 loc) · 1.67 KB
/
stream_timing_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
package gozstd
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
)
func BenchmarkStreamCompress(b *testing.B) {
for _, blockSize := range benchBlockSizes {
b.Run(fmt.Sprintf("blockSize_%d", blockSize), func(b *testing.B) {
for _, level := range benchCompressionLevels {
b.Run(fmt.Sprintf("level_%d", level), func(b *testing.B) {
benchmarkStreamCompress(b, blockSize, level)
})
}
})
}
}
func benchmarkStreamCompress(b *testing.B, blockSize, level int) {
block := newBenchString(blockSize * benchBlocksPerStream)
b.ReportAllocs()
b.SetBytes(int64(len(block)))
b.RunParallel(func(pb *testing.PB) {
r := bytes.NewReader(block)
for pb.Next() {
if err := StreamCompressLevel(ioutil.Discard, r, level); err != nil {
panic(fmt.Errorf("unexpected error: %s", err))
}
r.Reset(block)
}
})
}
func BenchmarkStreamDecompress(b *testing.B) {
for _, blockSize := range benchBlockSizes {
b.Run(fmt.Sprintf("blockSize_%d", blockSize), func(b *testing.B) {
for _, level := range benchCompressionLevels {
b.Run(fmt.Sprintf("level_%d", level), func(b *testing.B) {
benchmarkStreamDecompress(b, blockSize, level)
})
}
})
}
}
func benchmarkStreamDecompress(b *testing.B, blockSize, level int) {
block := newBenchString(blockSize * benchBlocksPerStream)
cd := CompressLevel(nil, block, level)
b.Logf("compressionRatio: %f", float64(len(block))/float64(len(cd)))
b.ReportAllocs()
b.SetBytes(int64(len(block)))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
r := bytes.NewReader(cd)
for pb.Next() {
if err := StreamDecompress(ioutil.Discard, r); err != nil {
panic(fmt.Errorf("unexpected error: %s", err))
}
r.Reset(cd)
}
})
}