-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
96 lines (85 loc) · 2.11 KB
/
cache_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
package main
import (
"fmt"
"path"
"testing"
"github.com/boltdb/bolt"
"github.com/stretchr/testify/assert"
)
func TestUserCache(t *testing.T) {
setUserCache("zack", []string{"bob", "bill", "jane"})
users, _ := getUserCache("zack")
assert.Equal(t, users, []string{"bob", "bill", "jane"})
}
func TestResetCache(t *testing.T) {
setUserCache("zack", []string{"bob", "bill", "jane"})
resetCache("userCache")
_, ok := getUserCache("zack")
assert.Equal(t, ok, false)
}
func BenchmarkSetCache(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
setUserCache("zack", []string{"bob", "bill", "jane"})
}
}
func BenchmarkResetCache(b *testing.B) {
setUserCache("zack", []string{"bob", "bill", "jane"})
b.ResetTimer()
for i := 0; i < b.N; i++ {
resetCache("userCache)")
}
}
// BenchmarkCache needs to have precomputed parameters for testdb (run Optimize after loading testdb.sh)
func BenchmarkGetPSCache(b *testing.B) {
var ps FullParameters
db, err := bolt.Open(path.Join("data", "testdb.db"), 0600, nil)
if err != nil {
Error.Println(err)
}
err = db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("resources"))
if b == nil {
return fmt.Errorf("Resources dont exist")
}
v := b.Get([]byte("fullParameters"))
ps = loadParameters(v)
return nil
})
if err != nil {
Error.Println(err)
}
db.Close()
setPsCache("testdb", ps)
b.ResetTimer()
for i := 0; i < b.N; i++ {
getPsCache("testdb")
}
}
// BenchmarkCache needs to have precomputed parameters for testdb (run Optimize after loading testdb.sh)
func BenchmarkSetPSCache(b *testing.B) {
var ps FullParameters
db, err := bolt.Open(path.Join("data", "testdb.db"), 0600, nil)
if err != nil {
Error.Println(err)
}
err = db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("resources"))
if b == nil {
return fmt.Errorf("Resources dont exist")
}
v := b.Get([]byte("fullParameters"))
ps = loadParameters(v)
return nil
})
if err != nil {
Error.Println(err)
}
db.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
setPsCache("testdb", ps)
}
}