-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastzlib_test.go
244 lines (222 loc) · 5.15 KB
/
fastzlib_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
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
package czlib
import (
"bytes"
"compress/zlib"
"crypto/rand"
"fmt"
"io/ioutil"
"os"
"testing"
)
var gzipped, raw []byte
func zip(b []byte) []byte {
var out bytes.Buffer
w := zlib.NewWriter(&out)
w.Write(b)
w.Close()
return out.Bytes()
}
func init() {
var err error
payload := os.Getenv("PAYLOAD")
if len(payload) == 0 {
fmt.Println("You must provide PAYLOAD env var for path to test payload.")
return
}
raw, err = ioutil.ReadFile(payload)
if err != nil {
fmt.Printf("Error opening payload: %s\n", err)
}
gzipped = zip(raw)
// fmt.Printf("%d byte test payload (%d orig)\n", len(gzipped), len(raw))
}
// Generate an n-byte long []byte
func genData(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
return b, err
}
func TestAllZlib(t *testing.T) {
type compressFunc func([]byte) ([]byte, error)
funcs := []compressFunc{Compress, gzip, zzip}
names := []string{"Compress", "gzip", "zzip"}
for _, i := range []int{10, 128, 1000, 1024 * 10, 1024 * 100, 1024 * 1024, 1024 * 1024 * 7} {
data, err := genData(i)
if err != nil {
t.Error(err)
continue
}
for i, f := range funcs {
comp, err := f(data)
if err != nil {
t.Fatalf("Compression failed on %v: %s", names[i], err)
}
decomp, err := Decompress(comp)
if err != nil {
t.Fatalf("Decompression failed on %v: %s", names[i], err)
}
if bytes.Compare(decomp, data) != 0 {
t.Fatalf("deflate->inflate does not match original for %s", names[i])
}
}
}
}
func TestEmpty(t *testing.T) {
var empty []byte
_, err := Compress(empty)
if err != nil {
t.Fatalf("unexpected error compressing empty slice")
}
_, err = Decompress(empty)
if err == nil {
t.Fatalf("unexpected success decompressing empty slice")
}
}
func TestUnsafeZlib(t *testing.T) {
for _, i := range []int{10, 128, 1000, 1024 * 10, 1024 * 100, 1024 * 1024, 1024 * 1024 * 7} {
data, err := genData(i)
if err != nil {
t.Error(err)
continue
}
comp, err := UnsafeCompress(data)
if err != nil {
t.Fatal(err)
}
decomp, err := UnsafeDecompress(comp)
if err != nil {
t.Fatal(err)
}
if bytes.Compare(decomp, data) != 0 {
t.Fatal("Compress -> Decompress on byte array failed to match original data.")
}
comp.Free()
decomp.Free()
}
}
// Compression benchmarks
func BenchmarkCompressUnsafe(b *testing.B) {
if raw == nil {
b.Skip("You must provide PAYLOAD env var for benchmarking.")
}
b.SetBytes(int64(len(raw)))
for i := 0; i < b.N; i++ {
u, _ := UnsafeCompress(raw)
u.Free()
}
}
func BenchmarkCompress(b *testing.B) {
if raw == nil {
b.Skip("You must provide PAYLOAD env var for benchmarking.")
}
b.SetBytes(int64(len(raw)))
for i := 0; i < b.N; i++ {
Compress(raw)
}
}
func BenchmarkCompressStream(b *testing.B) {
if raw == nil {
b.Skip("You must provide PAYLOAD env var for benchmarking.")
}
b.SetBytes(int64(len(raw)))
for i := 0; i < b.N; i++ {
gzip(raw)
}
}
func BenchmarkCompressStdZlib(b *testing.B) {
if raw == nil {
b.Skip("You must provide PAYLOAD env var for benchmarking.")
}
b.SetBytes(int64(len(raw)))
for i := 0; i < b.N; i++ {
zzip(raw)
}
}
// Decomression benchmarks
func BenchmarkDecompressUnsafe(b *testing.B) {
if raw == nil {
b.Skip("You must provide PAYLOAD env var for benchmarking.")
}
b.SetBytes(int64(len(raw)))
for i := 0; i < b.N; i++ {
u, _ := UnsafeDecompress(gzipped)
u.Free()
}
}
func BenchmarkDecompress(b *testing.B) {
if raw == nil {
b.Skip("You must provide PAYLOAD env var for benchmarking.")
}
b.SetBytes(int64(len(raw)))
for i := 0; i < b.N; i++ {
Decompress(gzipped)
}
}
func BenchmarkDecompressStream(b *testing.B) {
if raw == nil {
b.Skip("You must provide PAYLOAD env var for benchmarking.")
}
b.SetBytes(int64(len(raw)))
for i := 0; i < b.N; i++ {
gunzip(gzipped)
}
}
func BenchmarkDecompressStdZlib(b *testing.B) {
if raw == nil {
b.Skip("You must provide PAYLOAD env var for benchmarking.")
}
b.SetBytes(int64(len(raw)))
for i := 0; i < b.N; i++ {
zunzip(gzipped)
}
}
// helpers
func gunzip(body []byte) ([]byte, error) {
reader, err := NewReader(bytes.NewBuffer(body))
if err != nil {
return []byte{}, err
}
return ioutil.ReadAll(reader)
}
// unzip a gzipped []byte payload, returning the unzipped []byte and error
func zunzip(body []byte) ([]byte, error) {
reader, err := zlib.NewReader(bytes.NewBuffer(body))
if err != nil {
return []byte{}, err
}
return ioutil.ReadAll(reader)
}
func gzip(body []byte) ([]byte, error) {
outb := make([]byte, 0, 16*1024)
out := bytes.NewBuffer(outb)
writer := NewWriter(out)
n, err := writer.Write(body)
if n != len(body) {
return []byte{}, fmt.Errorf("compressed %d, expected %d", n, len(body))
}
if err != nil {
return []byte{}, err
}
err = writer.Close()
if err != nil {
return []byte{}, err
}
return out.Bytes(), nil
}
func zzip(body []byte) ([]byte, error) {
outb := make([]byte, 0, len(body))
out := bytes.NewBuffer(outb)
writer := zlib.NewWriter(out)
n, err := writer.Write(body)
if n != len(body) {
return []byte{}, fmt.Errorf("compressed %d, expected %d", n, len(body))
}
if err != nil {
return []byte{}, err
}
err = writer.Close()
if err != nil {
return []byte{}, err
}
return out.Bytes(), nil
}