-
Notifications
You must be signed in to change notification settings - Fork 0
/
perf_test.go
377 lines (333 loc) · 8.43 KB
/
perf_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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package fcache
import (
"bytes"
"errors"
"fmt"
"io"
"math/rand"
"os"
"runtime"
"sync"
"testing"
)
func BenchmarkCache_Put_SameKey(b *testing.B) {
dir := b.TempDir()
cache, err := Builder(dir, 1*GiB).Build()
if err != nil {
b.Fatal(err)
}
size := 7 * KiB
key := uint64(1)
data := make([]byte, size)
rand.Read(data)
b.Run(fmt.Sprintf("%d_bytes", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := cache.Put(key, data, 0)
if err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkCache_Get_SameKey(b *testing.B) {
dir := b.TempDir()
cache, err := Builder(dir, 1*GiB).Build()
if err != nil {
b.Fatal(err)
}
size := 7 * KiB
key := uint64(1)
data := make([]byte, size)
rand.Read(data)
_, err = cache.Put(key, data, 0)
if err != nil {
b.Fatal(err)
}
b.Run(fmt.Sprintf("%d_bytes", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, err := cache.Get(key)
if err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkCache_GetOrPut_FreshKey(b *testing.B) {
dir := b.TempDir()
cache, err := Builder(dir, 1*GiB).Build()
if err != nil {
b.Fatal(err)
}
size := 7 * KiB
key := uint64(1)
data := make([]byte, size)
rand.Read(data)
_, err = cache.Put(key, data, 0)
if err != nil {
b.Fatal(err)
}
b.Run(fmt.Sprintf("%d_bytes", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, _, err := cache.GetOrPut(uint64(i), 0, FillerFunc(func(key uint64, sink io.Writer) (written int64, err error) {
written, err = io.Copy(sink, bytes.NewReader(data))
return
}))
if err != nil {
b.Fatal(err)
}
}
})
}
func runWorkers(b *testing.B, cache Cache, items int64, size Size, workers int, work func(cache Cache, items int64, size Size, workers int, workerId int) error) {
wgDone := sync.WaitGroup{}
for i := 0; i < workers; i++ {
wgDone.Add(1)
go func(i int) {
defer wgDone.Done()
err := work(cache, items, size, workers, i)
if err != nil {
b.Error("Worker", i, "failed:", err)
return
}
}(i)
}
wgDone.Wait()
}
func putFreshKeyWorker(cache Cache, items int64, size Size, workers int, workerId int) error {
dataRand := rand.New(rand.NewSource(int64(workerId)))
for i := int64(workerId); i < items; i += int64(workers) {
_, err := cache.PutReader(uint64(i), io.LimitReader(dataRand, int64(size)), 0)
if err != nil {
return err
}
}
return nil
}
func BenchmarkCache_Parallel_Put_FreshKey(b *testing.B) {
items := int64(10000)
size := 5 * KiB
targetSize := size * Size(items)
workers := 5
b.Run(fmt.Sprintf("%d_items_%d_bytes_%d_workers", items, size, workers), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
dir := b.TempDir()
cache, err := Builder(dir, targetSize).Build()
if err != nil {
b.Fatal(err)
}
b.StartTimer()
runWorkers(b, cache, items, size, workers, putFreshKeyWorker)
}
})
}
func getFreshKeyWorker(cache Cache, items int64, _ Size, workers int, workerId int) error {
for i := int64(workerId); i < items; i += int64(workers) {
_, _, err := cache.Get(uint64(i))
if err != nil {
return err
}
}
return nil
}
func BenchmarkCache_Parallel_Get_FreshKey(b *testing.B) {
items := int64(10000)
size := 5 * KiB
targetSize := size * Size(items)
workers := 5
b.Run(fmt.Sprintf("%d_items_%d_bytes_%d_workers", items, size, workers), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
dir := b.TempDir()
cache, err := Builder(dir, targetSize).Build()
if err != nil {
b.Fatal(err)
}
runWorkers(b, cache, items, size, 2, putFreshKeyWorker)
b.StartTimer()
runWorkers(b, cache, items, size, workers, getFreshKeyWorker)
}
})
}
func putRandomKeyWorker(cache Cache, items int64, size Size, _ int, workerId int) error {
keyRand := rand.New(rand.NewSource(int64(workerId)))
dataRand := rand.New(rand.NewSource(int64(workerId)))
for i := int64(0); i < items; i++ {
key := keyRand.Int63n(items)
_, err := cache.PutReader(uint64(key), io.LimitReader(dataRand, int64(size)), 0)
if err != nil {
return err
}
}
return nil
}
func BenchmarkCache_Parallel_Put_RandomKey(b *testing.B) {
items := int64(10000)
size := 5 * KiB
targetSize := size * Size(items)
workers := 5
b.Run(fmt.Sprintf("%d_items_%d_bytes_%d_workers", items, size, workers), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
dir := b.TempDir()
cache, err := Builder(dir, targetSize).Build()
if err != nil {
b.Fatal(err)
}
b.StartTimer()
runWorkers(b, cache, items, size, workers, putRandomKeyWorker)
}
})
}
func getRandomKeyWorker(cache Cache, items int64, _ Size, _ int, workerId int) error {
keyRand := rand.New(rand.NewSource(int64(workerId)))
for i := int64(0); i < items; i++ {
key := keyRand.Int63n(items)
_, _, err := cache.Get(uint64(key))
if err != nil {
return err
}
}
return nil
}
func BenchmarkCache_Parallel_Get_RandomKey(b *testing.B) {
items := int64(10000)
size := 5 * KiB
targetSize := size * Size(items)
workers := 5
b.Run(fmt.Sprintf("%d_items_%d_bytes_%d_workers", items, size, workers), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
dir := b.TempDir()
cache, err := Builder(dir, targetSize).Build()
if err != nil {
b.Fatal(err)
}
runWorkers(b, cache, items, size, 2, putFreshKeyWorker)
b.StartTimer()
runWorkers(b, cache, items, size, workers, getRandomKeyWorker)
}
})
}
func randomLoadWorker(cache Cache, items int64, size Size, _ int, workerId int) error {
keyRand := rand.New(rand.NewSource(int64(workerId)))
dataRand := rand.New(rand.NewSource(int64(workerId)))
actionRand := rand.New(rand.NewSource(int64(workerId)))
for i := int64(0); i < items*3; i++ {
key := uint64(keyRand.Int63n(items))
var err error
switch actionRand.Intn(2) {
case 0:
_, err = cache.Delete(key)
case 1:
_, _, _, err = cache.GetOrPut(key, 0, FillerFunc(func(key uint64, sink io.Writer) (written int64, err error) {
return io.Copy(sink, io.LimitReader(dataRand, int64(size)))
}))
default:
panic("Unexpected action")
}
if err != nil {
return err
}
}
return nil
}
func BenchmarkCache_Parallel_Random_Load(b *testing.B) {
items := int64(10)
size := 5 * KiB
targetSize := size * Size(items)
workers := 50
b.Run(fmt.Sprintf("%d_items_%d_bytes_%d_workers", items, size, workers), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
dir := b.TempDir()
cache, err := Builder(dir, targetSize).Build()
if err != nil {
b.Fatal(err)
}
b.StartTimer()
runWorkers(b, cache, items, size, workers, randomLoadWorker)
}
})
}
func writeMemoryUsage(w io.Writer, header bool) {
if header {
_, err := w.Write([]byte("TotalAlloc,Sys,HeapAlloc,HeapInuse,HeapSys,StackInuse,StackSys,NumGC\n"))
if err != nil {
panic("Failed to write memory header: " + err.Error())
}
return
}
var m runtime.MemStats
runtime.ReadMemStats(&m)
toMiB := func(b uint64) float32 { return float32(b) / 1024.0 / 1024.0 }
line := fmt.Sprintf("%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%d\n",
toMiB(m.TotalAlloc),
toMiB(m.Sys),
toMiB(m.HeapAlloc),
toMiB(m.HeapInuse),
toMiB(m.HeapSys),
toMiB(m.StackInuse),
toMiB(m.StackSys),
m.NumGC,
)
_, err := w.Write([]byte(line))
if err != nil {
panic("Failed to write memory line: " + err.Error())
}
}
func BenchmarkCache_MemoryUsage(b *testing.B) {
items := int64(100000)
size := 5 * KiB
targetSize := size * Size(items)
err := os.Mkdir("benchmarks", 0770)
if err != nil && !errors.Is(err, os.ErrExist) {
b.Fatal(err)
}
f, err := os.Create("benchmarks/memory.csv")
if err != nil {
b.Fatal(err)
}
defer func() {
err := f.Close()
if err != nil {
b.Fatal(err)
}
}()
writeMemoryUsage(f, true)
b.Run(fmt.Sprintf("%d_items_%d_bytes", items, size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
dir := b.TempDir()
cache, err := Builder(dir, targetSize).Build()
if err != nil {
b.Fatal(err)
}
b.StartTimer()
runWorkers(b, cache, items, size, 2, putFreshKeyWorker)
b.StopTimer()
runtime.GC()
writeMemoryUsage(f, false)
b.StartTimer()
}
})
}
func BenchmarkCache_loadEntries(b *testing.B) {
items := int64(10000)
size := 1 * Byte
targetSize := size * Size(items)
workers := 5
dir := b.TempDir()
cache, err := Builder(dir, targetSize).Build()
if err != nil {
b.Fatal(err)
}
runWorkers(b, cache, items, size, workers, putFreshKeyWorker)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Builder(dir, targetSize).Build()
if err != nil {
b.Fatal(err)
}
}
}