-
Notifications
You must be signed in to change notification settings - Fork 0
/
gozlib_bench_test.go
59 lines (47 loc) · 1.43 KB
/
gozlib_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
package gozlib
import (
"bytes"
"compress/gzip"
"io"
"testing"
"github.com/stretchr/testify/assert"
)
const (
smallCompressedInputSizeBytes = 128
largeCompressedInputSizeBytes = 1024 * 1024
)
var (
smallTestData = makeTestData(uint32(smallCompressedInputSizeBytes))
largeTestData = makeTestData(uint32(largeCompressedInputSizeBytes))
)
func BenchmarkGoGZipCompressSmall(b *testing.B) {
benchmarkgoGZipCompress(b, smallTestData)
}
func BenchmarkStdLibGZipCompressSmall(b *testing.B) {
benchmarkStdLibGZipCompress(b, smallTestData)
}
func BenchmarkGoGZipCompressLarge(b *testing.B) {
benchmarkgoGZipCompress(b, largeTestData)
}
func BenchmarkStdLibGZipCompressLarge(b *testing.B) {
benchmarkStdLibGZipCompress(b, largeTestData)
}
func benchmarkStdLibGZipCompress(b *testing.B, input []byte) {
for i := 0; i < b.N; i++ {
output := bytes.NewBuffer([]byte{})
compressor, _ := gzip.NewWriterLevel(output, gzip.BestCompression)
written, _ := io.Copy(compressor, bytes.NewBuffer(input))
assert.Greater(b, written, int64(0))
compressor.Close()
}
}
func benchmarkgoGZipCompress(b *testing.B, input []byte) {
const defaultBufferSize = 1024 * 8
for i := 0; i < b.N; i++ {
output := bytes.NewBuffer([]byte{})
compressor, _ := NewGoGZipCompressor(output, CompressionLevelBestCompression, defaultBufferSize)
written, _ := io.Copy(compressor, bytes.NewBuffer(input))
assert.Greater(b, written, int64(0))
compressor.Close()
}
}