forked from d4l3k/go-pbzip2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbzip2_test.go
103 lines (91 loc) · 2.04 KB
/
pbzip2_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
package pbzip2
import (
"bytes"
"compress/bzip2"
"crypto/rand"
"io"
"io/ioutil"
"strconv"
"testing"
)
var targetLen int64 = 10000000
func TestHasPBZip2(t *testing.T) {
if !hasPBZip2() {
t.Errorf("should have pbzip2")
}
}
func testString(t testing.TB) string {
return testStringN(t, targetLen)
}
func testStringN(t testing.TB, targetLen int64) string {
var buf bytes.Buffer
if _, err := io.CopyN(&buf, rand.Reader, targetLen); err != nil {
t.Fatal(err)
}
out := buf.String()
if len(out) != int(targetLen) {
t.Fatal("incorrect testString length")
}
return out
}
var benchSizes = []int64{1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000}
func BenchmarkPBZip2Read(b *testing.B) {
for _, size := range benchSizes {
size := size
b.Run(strconv.Itoa(int(size))+"B", func(b *testing.B) {
var buf bytes.Buffer
writer, err := NewWriter(&buf)
if err != nil {
b.Fatal(err)
}
in := testStringN(b, size)
if _, err := writer.Write([]byte(in)); err != nil {
b.Fatal(err)
}
if err := writer.Close(); err != nil {
b.Fatal(err)
}
compressed := buf.Bytes()
b.ResetTimer()
for i := 0; i < b.N; i++ {
reader, err := NewReader(bytes.NewReader(compressed))
if err != nil {
b.Fatal(err)
}
if _, err := ioutil.ReadAll(reader); err != nil {
b.Fatal(err)
}
if err := reader.Close(); err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkBZip2Read(b *testing.B) {
for _, size := range benchSizes {
size := size
b.Run(strconv.Itoa(int(size))+"B", func(b *testing.B) {
var buf bytes.Buffer
writer, err := NewWriter(&buf)
if err != nil {
b.Fatal(err)
}
in := testStringN(b, size)
if _, err := writer.Write([]byte(in)); err != nil {
b.Fatal(err)
}
if err := writer.Close(); err != nil {
b.Fatal(err)
}
compressed := buf.Bytes()
b.ResetTimer()
for i := 0; i < b.N; i++ {
reader := bzip2.NewReader(bytes.NewReader(compressed))
if _, err := ioutil.ReadAll(reader); err != nil {
b.Fatal(err)
}
}
})
}
}