-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompress_test.go
240 lines (205 loc) · 7.97 KB
/
compress_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
package utils
import (
"bytes"
"compress/gzip"
"compress/zlib"
"testing"
"github.com/fufuok/utils/assert"
)
func TestCompressZip(t *testing.T) {
data := bytes.Repeat(testBytes, 100)
dst, err := Zip(data)
assert.Equal(t, true, err == nil, "failed to zip")
t.Logf("origin len: %d, zipped(level: %d) len: %d", len(data), zlib.BestSpeed, len(dst))
src, err := Unzip(dst)
assert.Equal(t, true, err == nil, "failed to unzip")
assert.Equal(t, true, bytes.Equal(data, src), "data != src")
dst, err = ZipLevel(data, zlib.BestCompression)
assert.Equal(t, true, err == nil, "failed to zip")
t.Logf("origin len: %d, zipped(level: %d) len: %d", len(data), zlib.BestCompression, len(dst))
src, err = Unzip(dst)
assert.Equal(t, true, err == nil, "failed to unzip")
assert.Equal(t, true, bytes.Equal(data, src), "data != src")
dst, err = ZipLevel(data, 6)
assert.Equal(t, true, err == nil, "failed to zip")
t.Logf("origin len: %d, zipped(level: %d) len: %d", len(data), zlib.DefaultCompression, len(dst))
src, err = Unzip(dst)
assert.Equal(t, true, err == nil, "failed to unzip")
assert.Equal(t, true, bytes.Equal(data, src), "data != src")
}
func TestCompressGzip(t *testing.T) {
data := bytes.Repeat(testBytes, 100)
dst, err := Gzip(data)
assert.Equal(t, true, err == nil, "failed to zip")
t.Logf("origin len: %d, zipped(level: %d) len: %d", len(data), gzip.BestSpeed, len(dst))
src, err := Ungzip(dst)
assert.Equal(t, true, err == nil, "failed to unzip")
assert.Equal(t, true, bytes.Equal(data, src), "data != src")
dst, err = GzipLevel(data, gzip.BestCompression)
assert.Equal(t, true, err == nil, "failed to zip")
t.Logf("origin len: %d, zipped(level: %d) len: %d", len(data), gzip.BestCompression, len(dst))
src, err = Ungzip(dst)
assert.Equal(t, true, err == nil, "failed to unzip")
assert.Equal(t, true, bytes.Equal(data, src), "data != src")
dst, err = GzipLevel(data, 6)
assert.Equal(t, true, err == nil, "failed to zip")
t.Logf("origin len: %d, zipped(level: %d) len: %d", len(data), gzip.DefaultCompression, len(dst))
src, err = Ungzip(dst)
assert.Equal(t, true, err == nil, "failed to unzip")
assert.Equal(t, true, bytes.Equal(data, src), "data != src")
}
func BenchmarkCompress_Zip(b *testing.B) {
data := bytes.Repeat(testBytes, 100)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Zip(data)
}
}
func BenchmarkCompress_Zip_BestCompression(b *testing.B) {
data := bytes.Repeat(testBytes, 100)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = ZipLevel(data, gzip.BestCompression)
}
}
func BenchmarkCompress_Unzip(b *testing.B) {
data := bytes.Repeat(testBytes, 100)
dec, _ := Zip(data)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Unzip(dec)
}
}
func BenchmarkCompress_Zip_Parallel(b *testing.B) {
data := bytes.Repeat(testBytes, 100)
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = Zip(data)
}
})
}
func BenchmarkCompress_Unzip_Parallel(b *testing.B) {
data := bytes.Repeat(testBytes, 100)
dec, _ := Zip(data)
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = Unzip(dec)
}
})
}
func BenchmarkCompress_ZipUnzip_Parallel(b *testing.B) {
bs := bytes.Repeat(testBytes, 100)
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
dec, _ := Zip(bs)
src, err := Unzip(dec)
if err != nil {
b.Fatal(err)
}
if !EqualFoldBytes(src, bs) {
b.Fatal("src != bs")
}
}
})
}
func BenchmarkCompress_Gzip(b *testing.B) {
data := bytes.Repeat(testBytes, 100)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Gzip(data)
}
}
func BenchmarkCompress_Gzip_BestCompression(b *testing.B) {
data := bytes.Repeat(testBytes, 100)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = GzipLevel(data, gzip.BestCompression)
}
}
func BenchmarkCompress_Ungzip(b *testing.B) {
data := bytes.Repeat(testBytes, 100)
dec, _ := Gzip(data)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Ungzip(dec)
}
}
func BenchmarkCompress_Gzip_Parallel(b *testing.B) {
data := bytes.Repeat(testBytes, 100)
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = Gzip(data)
}
})
}
func BenchmarkCompress_Ungzip_Parallel(b *testing.B) {
data := bytes.Repeat(testBytes, 100)
dec, _ := Gzip(data)
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = Ungzip(dec)
}
})
}
func BenchmarkCompress_GzipUngzip(b *testing.B) {
bs := bytes.Repeat(testBytes, 100)
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
dec, _ := Gzip(bs)
src, err := Ungzip(dec)
if err != nil {
b.Fatal(err)
}
if !EqualFoldBytes(src, bs) {
b.Fatal("src != bs")
}
}
})
}
// go test -run=^$ -benchtime=1s -benchmem -count=2 -bench=BenchmarkCompress
// goos: linux
// goarch: amd64
// pkg: github.com/fufuok/utils
// cpu: Intel(R) Xeon(R) Gold 6151 CPU @ 3.00GHz
// BenchmarkCompress_Zip-4 137169 8728 ns/op 96 B/op 1 allocs/op
// BenchmarkCompress_Zip-4 136028 8758 ns/op 104 B/op 1 allocs/op
// BenchmarkCompress_Zip_BestCompression-4 31932 35699 ns/op 105 B/op 1 allocs/op
// BenchmarkCompress_Zip_BestCompression-4 31845 35171 ns/op 80 B/op 1 allocs/op
// BenchmarkCompress_Unzip-4 84660 14576 ns/op 52604 B/op 12 allocs/op
// BenchmarkCompress_Unzip-4 80158 15248 ns/op 52604 B/op 12 allocs/op
// BenchmarkCompress_Zip_Parallel-4 504540 2354 ns/op 100 B/op 1 allocs/op
// BenchmarkCompress_Zip_Parallel-4 490543 2429 ns/op 96 B/op 1 allocs/op
// BenchmarkCompress_Unzip_Parallel-4 102718 11924 ns/op 52606 B/op 12 allocs/op
// BenchmarkCompress_Unzip_Parallel-4 82278 14111 ns/op 52604 B/op 12 allocs/op
// BenchmarkCompress_ZipUnzip_Parallel-4 85200 15105 ns/op 61635 B/op 13 allocs/op
// BenchmarkCompress_ZipUnzip_Parallel-4 85476 14631 ns/op 61918 B/op 13 allocs/op
// BenchmarkCompress_Gzip-4 155563 7617 ns/op 119 B/op 1 allocs/op
// BenchmarkCompress_Gzip-4 155354 7601 ns/op 112 B/op 1 allocs/op
// BenchmarkCompress_Gzip_BestCompression-4 32972 34728 ns/op 120 B/op 1 allocs/op
// BenchmarkCompress_Gzip_BestCompression-4 33518 33674 ns/op 96 B/op 1 allocs/op
// BenchmarkCompress_Ungzip-4 173500 7082 ns/op 12037 B/op 6 allocs/op
// BenchmarkCompress_Ungzip-4 162517 7543 ns/op 12038 B/op 6 allocs/op
// BenchmarkCompress_Gzip_Parallel-4 584652 2074 ns/op 114 B/op 1 allocs/op
// BenchmarkCompress_Gzip_Parallel-4 507086 2096 ns/op 114 B/op 1 allocs/op
// BenchmarkCompress_Ungzip_Parallel-4 318189 3876 ns/op 12042 B/op 6 allocs/op
// BenchmarkCompress_Ungzip_Parallel-4 313982 4064 ns/op 12043 B/op 6 allocs/op
// BenchmarkCompress_GzipUngzip-4 177799 6656 ns/op 14329 B/op 7 allocs/op
// BenchmarkCompress_GzipUngzip-4 192142 6538 ns/op 14509 B/op 7 allocs/op