-
Notifications
You must be signed in to change notification settings - Fork 9
/
compressor_benchmark_test.go
184 lines (139 loc) · 4.28 KB
/
compressor_benchmark_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
package libdeflate
import (
"bytes"
"compress/zlib"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"testing"
)
/*---------------------
BENCHMARKS
-----------------------*/
// real world data benchmarks
const decompressedMcPacketsLoc = "https://raw.githubusercontent.com/4kills/zlib_benchmark/master/decompressed_mc_packets.json"
var decompressedMcPackets [][]byte
func BenchmarkCompressZlibAllMcPacketsLibdeflate(b *testing.B) {
compressZlibAllMcPacketsLibdeflateLevel(DefaultCompressionLevel, b)
}
func BenchmarkCompressZlibAllMcPacketsStdLib(b *testing.B) {
compressZlibAllMcPacketsStdLibLevel(DefaultCompressionLevel, b)
}
func BenchmarkCompressZlibAllMcPacketsFastestLibdeflate(b *testing.B) {
compressZlibAllMcPacketsLibdeflateLevel(MinCompressionLevel, b)
}
func BenchmarkCompressZlibAllMcPacketsFastestStdLib(b *testing.B) {
compressZlibAllMcPacketsStdLibLevel(MinCompressionLevel, b)
}
func BenchmarkCompressZlib1McPacketLibdeflate(b *testing.B) {
compressZlib1McPacketLibdeflateLevel(DefaultCompressionLevel, b)
}
func BenchmarkCompressZlib1McPacketStdLib(b *testing.B) {
compressZlib1McPacketStdLibLevel(DefaultCompressionLevel, b)
}
//-----------------------------------------------------------------
func compressZlibAllMcPacketsLibdeflateLevel(level int, b *testing.B) {
loadPacketsIfNil(&decompressedMcPackets, decompressedMcPacketsLoc)
c, _ := NewCompressorLevel(level)
defer c.Close()
b.ResetTimer()
reportBytesPerIteration(decompressedMcPackets, b)
for i := 0; i < b.N; i++ {
for _, v := range decompressedMcPackets {
b.StopTimer()
out := make([]byte, len(v))
b.StartTimer()
n, _, _ := c.CompressZlib(v, out)
b.ReportMetric(float64(len(v))/float64(n), "ratio")
}
}
}
func compressZlibAllMcPacketsStdLibLevel(level int, b *testing.B) {
loadPacketsIfNil(&decompressedMcPackets, decompressedMcPacketsLoc)
w, _ := zlib.NewWriterLevel(&bytes.Buffer{}, level)
defer w.Close()
b.ResetTimer()
reportBytesPerIteration(decompressedMcPackets, b)
for i := 0; i < b.N; i++ {
for _, v := range decompressedMcPackets {
b.StopTimer()
buf := bytes.NewBuffer(make([]byte, 0, len(v)))
w.Reset(buf)
b.StartTimer()
w.Write(v)
w.Flush() // has to be called too, because this lib's compressor always flushes
b.ReportMetric(float64(len(v))/float64(buf.Len()), "ratio")
}
}
}
func compressZlib1McPacketLibdeflateLevel(level int, b *testing.B) {
loadPacketsIfNil(&decompressedMcPackets, decompressedMcPacketsLoc)
c, _ := NewCompressorLevel(level)
defer c.Close()
out := make([]byte, len(decompressedMcPackets[1]))
b.ResetTimer()
reportBytesPerIteration(decompressedMcPackets[1:2], b)
for i := 0; i < b.N; i++ {
n, _, _ := c.CompressZlib(decompressedMcPackets[1], out)
b.ReportMetric(float64(len(decompressedMcPackets[1]))/float64(n), "ratio")
}
}
func compressZlib1McPacketStdLibLevel(level int, b *testing.B) {
loadPacketsIfNil(&decompressedMcPackets, decompressedMcPacketsLoc)
w, _ := zlib.NewWriterLevel(&bytes.Buffer{}, level)
defer w.Close()
out := make([]byte, 0, len(decompressedMcPackets[1]))
b.ResetTimer()
reportBytesPerIteration(decompressedMcPackets[1:2], b)
for i := 0; i < b.N; i++ {
b.StopTimer()
buf := bytes.NewBuffer(out)
w.Reset(buf)
b.StartTimer()
w.Write(decompressedMcPackets[1])
w.Flush() // has to be called too, because this lib's compressor always flushes
b.ReportMetric(float64(len(decompressedMcPackets[1]))/float64(buf.Len()), "ratio")
}
}
/*---------------------
HELPER
-----------------------*/
func reportBytesPerIteration(input [][]byte, b *testing.B) {
b.StopTimer()
numOfBytes := 0
for _, v := range input {
numOfBytes += len(v)
}
b.ReportMetric(float64(numOfBytes), "bytes_processed/op")
b.StartTimer()
}
func loadPacketsIfNil(packets *[][]byte, loc string) {
if *packets != nil {
return
}
*packets = loadPackets(loc)
}
func loadPackets(loc string) [][]byte {
b, err := downloadFile(loc)
if err != nil {
panic(err)
}
return unmarshal(b)
}
func unmarshal(b *bytes.Buffer) [][]byte {
var out [][]byte
byteValue, _ := ioutil.ReadAll(b)
json.Unmarshal(byteValue, &out)
return out
}
func downloadFile(url string) (*bytes.Buffer, error) {
r, err := http.Get(url)
if err != nil {
return nil, err
}
defer r.Body.Close()
b := &bytes.Buffer{}
_, err = io.Copy(b, r.Body)
return b, err
}