-
Notifications
You must be signed in to change notification settings - Fork 10
/
writer_benchmark_test.go
251 lines (190 loc) · 5.63 KB
/
writer_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
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
245
246
247
248
249
250
251
package zlib
import (
"bytes"
"compress/zlib"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"testing"
)
// real world data benchmarks
const decompressedMcPacketsLoc = "https://raw.githubusercontent.com/4kills/zlib_benchmark/master/decompressed_mc_packets.json"
var decompressedMcPackets [][]byte
func BenchmarkWriteBytesAllMcPacketsDefault(b *testing.B) {
loadPacketsIfNil(&decompressedMcPackets, decompressedMcPacketsLoc)
benchmarkWriteBytesMcPacketsGeneric(decompressedMcPackets, b)
}
func benchmarkWriteBytesMcPacketsGeneric(input [][]byte, b *testing.B) {
w := NewWriter(nil)
defer w.Close()
b.ResetTimer()
reportBytesPerChunk(input, b)
for i := 0; i < b.N; i++ {
for _, v := range input {
w.WriteBuffer(v, make([]byte, len(v)))
}
}
}
func BenchmarkWriteAllMcPacketsDefault(b *testing.B) {
loadPacketsIfNil(&decompressedMcPackets, decompressedMcPacketsLoc)
w := NewWriter(&bytes.Buffer{})
benchmarkWriteMcPacketsGeneric(w, decompressedMcPackets, b)
}
func BenchmarkWriteAllMcPacketsDefaultStd(b *testing.B) {
loadPacketsIfNil(&decompressedMcPackets, decompressedMcPacketsLoc)
w := zlib.NewWriter(&bytes.Buffer{})
benchmarkWriteMcPacketsGeneric(w, decompressedMcPackets, b)
}
func benchmarkWriteMcPacketsGeneric(w TestWriter, input [][]byte, b *testing.B) {
defer w.Close()
buf := bytes.NewBuffer(make([]byte, 0, 1e+5))
b.ResetTimer()
reportBytesPerChunk(input, b)
for i := 0; i < b.N; i++ {
for _, v := range input {
w.Write(v) // writes data
// flushes data to buf and sets to buf again (reset compression for indiviual packets)
w.Flush()
b.StopTimer()
w.Reset(buf)
buf.Reset()
b.StartTimer()
}
}
}
// laboratory condition benchmarks
func BenchmarkWriteBytes64BBestCompression(b *testing.B) {
benchmarkWriteBytesLevel(xByte(64), BestCompression, b)
}
func BenchmarkWriteBytes8192BBestCompression(b *testing.B) {
benchmarkWriteBytesLevel(xByte(8192), BestCompression, b)
}
func BenchmarkWriteBytes65536BBestCompression(b *testing.B) {
benchmarkWriteBytesLevel(xByte(65536), BestCompression, b)
}
func BenchmarkWriteBytes64BBestSpeed(b *testing.B) {
benchmarkWriteBytesLevel(xByte(64), BestSpeed, b)
}
func BenchmarkWriteBytes8192BBestSpeed(b *testing.B) {
benchmarkWriteBytesLevel(xByte(8192), BestSpeed, b)
}
func BenchmarkWriteBytes65536BBestSpeed(b *testing.B) {
benchmarkWriteBytesLevel(xByte(65536), BestSpeed, b)
}
func BenchmarkWriteBytes64BDefault(b *testing.B) {
benchmarkWriteBytesLevel(xByte(64), DefaultCompression, b)
}
func BenchmarkWriteBytes8192BDefault(b *testing.B) {
benchmarkWriteBytesLevel(xByte(8192), DefaultCompression, b)
}
func BenchmarkWriteBytes65536BDefault(b *testing.B) {
benchmarkWriteBytesLevel(xByte(65536), DefaultCompression, b)
}
func benchmarkWriteBytesLevel(input []byte, level int, b *testing.B) {
w, _ := NewWriterLevel(nil, level)
defer w.Close()
for i := 0; i < b.N; i++ {
w.WriteBuffer(input, make([]byte, len(input))) // write bytes resets the compressor
}
}
func BenchmarkWrite64BBestCompression(b *testing.B) {
benchmarkWriteLevel(xByte(64), BestCompression, b)
}
func BenchmarkWrite8192BBestCompression(b *testing.B) {
benchmarkWriteLevel(xByte(8192), BestCompression, b)
}
func BenchmarkWrite65536BBestCompression(b *testing.B) {
benchmarkWriteLevel(xByte(65536), BestCompression, b)
}
func BenchmarkWrite64BBestSpeed(b *testing.B) {
benchmarkWriteLevel(xByte(64), BestSpeed, b)
}
func BenchmarkWrite8192BBestSpeed(b *testing.B) {
benchmarkWriteLevel(xByte(8192), BestSpeed, b)
}
func BenchmarkWrite65536BBestSpeed(b *testing.B) {
benchmarkWriteLevel(xByte(65536), BestSpeed, b)
}
func BenchmarkWrite64BDefault(b *testing.B) {
benchmarkWriteLevel(xByte(64), DefaultCompression, b)
}
func BenchmarkWrite8192BDefault(b *testing.B) {
benchmarkWriteLevel(xByte(8192), DefaultCompression, b)
}
func BenchmarkWrite65536BDefault(b *testing.B) {
benchmarkWriteLevel(xByte(65536), DefaultCompression, b)
}
func benchmarkWriteLevel(input []byte, level int, b *testing.B) {
buf := bytes.NewBuffer(make([]byte, 0, len(input)))
w, _ := NewWriterLevel(buf, level)
benchmarkWriteLevelGeneric(w, buf, input, b)
}
func benchmarkWriteLevelGeneric(w TestWriter, buf *bytes.Buffer, input []byte, b *testing.B) {
defer w.Close()
for i := 0; i < b.N; i++ {
w.Write(input)
w.Flush()
b.StopTimer()
w.Reset(buf) // to ensure packets are compressed individually
buf.Reset()
b.StartTimer()
}
}
// HELPER FUNCTIONS
type TestWriter interface {
Write(p []byte) (int, error)
Close() error
Flush() error
Reset(w io.Writer)
}
func reportBytesPerChunk(input [][]byte, b *testing.B) {
b.StopTimer()
numOfBytes := 0
for _, v := range input {
numOfBytes += len(v)
}
b.ReportMetric(float64(numOfBytes), "bytes/chunk")
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
}
func xByte(multOf16 int) []byte {
_16byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}
if multOf16 == 0 || multOf16%16 != 0 {
panic(errors.New("multOf16 is not a valid multiple of 16"))
}
xByte := make([]byte, multOf16)
for i := 0; i < multOf16; i += 16 {
copy(xByte[i:i+16], _16byte)
}
return xByte
}