-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress_test.go
146 lines (118 loc) · 2.76 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
package sod
import (
"bytes"
"compress/gzip"
"compress/lzw"
"compress/zlib"
"encoding/json"
"io"
"io/ioutil"
"testing"
)
var (
data [][]byte
)
func init() {
for o := range genTestStructs(10000) {
if b, err := json.Marshal(o); err != nil {
panic(err)
} else {
data = append(data, b)
}
}
}
func compress(b *testing.B, w io.Writer) (written int) {
// compress
for _, buf := range data {
if n, err := w.Write(buf); err != nil {
panic(err)
} else {
written += n
}
}
return
}
func decompress(b *testing.B, r io.Reader) (read int) {
// decompress
if all, err := ioutil.ReadAll(r); err != nil {
b.Error(err)
} else {
read += len(all)
}
return
}
// c -> compressed size u -> uncompressed size
func percCompressRatio(c, u int) float64 {
return (1 - float64(c)/float64(u)) * 100
}
func BenchmarkMemcpy(b *testing.B) {
buf := new(bytes.Buffer)
written := compress(b, buf)
b.Logf("Compression ratio=%.1f%%", percCompressRatio(buf.Len(), written))
read := decompress(b, buf)
if written != read {
b.Error("data read different from written")
}
}
func BenchmarkGzip(b *testing.B) {
buf := new(bytes.Buffer)
w := gzip.NewWriter(buf)
written := compress(b, w)
w.Close()
b.Logf("Compression ratio=%.1f%%", percCompressRatio(buf.Len(), written))
r, _ := gzip.NewReader(buf)
read := decompress(b, r)
if written != read {
b.Error("data read different from written")
}
}
func BenchmarkGzipBestSpeed(b *testing.B) {
buf := new(bytes.Buffer)
w, _ := gzip.NewWriterLevel(buf, gzip.BestSpeed)
written := compress(b, w)
w.Close()
b.Logf("Compression ratio=%.1f%%", percCompressRatio(buf.Len(), written))
r, _ := gzip.NewReader(buf)
read := decompress(b, r)
if written != read {
b.Error("data read different from written")
}
}
func BenchmarkLzw(b *testing.B) {
buf := new(bytes.Buffer)
w := lzw.NewWriter(buf, lzw.MSB, 8)
written := compress(b, w)
w.Close()
b.Logf("Compression ratio=%.1f%%", percCompressRatio(buf.Len(), written))
r := lzw.NewReader(buf, lzw.MSB, 8)
read := decompress(b, r)
if written != read {
b.Error("data read different from written")
}
r.Close()
}
func BenchmarkZlib(b *testing.B) {
buf := new(bytes.Buffer)
w := zlib.NewWriter(buf)
written := compress(b, w)
w.Close()
b.Logf("Compression ratio=%.1f%%", percCompressRatio(buf.Len(), written))
r, _ := zlib.NewReader(buf)
read := decompress(b, r)
if written != read {
b.Error("data read different from written")
}
r.Close()
}
/*func BenchmarkLz4(b *testing.B) {
buf := new(bytes.Buffer)
w := lz4.NewWriter(buf)
written := compress(b, w)
w.Close()
b.Logf("Compression ratio=%.1f%%", percCompressRatio(buf.Len(), written))
r := lz4.NewReader(buf)
read := decompress(b, r)
if written != read {
b.Error("data read different from written")
}
}*/