-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfilecache_benchmark_test.go
More file actions
92 lines (80 loc) · 1.68 KB
/
filecache_benchmark_test.go
File metadata and controls
92 lines (80 loc) · 1.68 KB
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
package filecache
import (
"os"
"sync"
"testing"
"time"
)
func setupCache(t *testing.T) *FileCache {
cache := NewDefaultCache()
cache.MaxItems = 1000
cache.MaxSize = 1024 * 1024 // 1 MB
cache.ExpireItem = 5
cache.Every = 1
if err := cache.Start(); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { cache.Stop() })
return cache
}
func BenchmarkCacheNow(b *testing.B) {
cache := setupCache(&testing.T{})
file := createTempFile(&testing.T{}, 4096)
defer os.Remove(file)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := cache.CacheNow(file); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkGetItem(b *testing.B) {
cache := setupCache(&testing.T{})
file := createTempFile(&testing.T{}, 4096)
defer os.Remove(file)
if err := cache.CacheNow(file); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, ok := cache.GetItem(file); !ok {
b.Fatal("item missing from cache")
}
}
}
func BenchmarkConcurrentReadWrite(b *testing.B) {
cache := setupCache(&testing.T{})
file := createTempFile(&testing.T{}, 4096)
defer os.Remove(file)
b.ResetTimer()
var wg sync.WaitGroup
for i := 0; i < b.N; i++ {
wg.Add(2)
go func() {
defer wg.Done()
_ = cache.CacheNow(file)
}()
go func() {
defer wg.Done()
cache.GetItem(file)
}()
}
wg.Wait()
}
func BenchmarkVacuum(b *testing.B) {
cache := setupCache(&testing.T{})
file := createTempFile(&testing.T{}, 4096)
defer os.Remove(file)
for i := 0; i < 200; i++ {
_ = cache.CacheNow(file)
}
cache.mutex.Lock()
for _, itm := range cache.items {
itm.Lastaccess = time.Now().Add(-10 * time.Second)
}
cache.mutex.Unlock()
b.ResetTimer()
for i := 0; i < b.N; i++ {
cache.vacuumOnce()
}
}