-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache_test.go
More file actions
65 lines (61 loc) · 1.09 KB
/
cache_test.go
File metadata and controls
65 lines (61 loc) · 1.09 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
package cache
import (
"fmt"
"testing"
"time"
)
func BenchmarkCacheGet(b *testing.B) {
ce := NewCache()
keys := make([]string, b.N)
for i := range keys {
keys[i] = fmt.Sprintf("key %d", i)
}
for i := 0; i < b.N; i++ {
ce.Set(keys[i], i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if ce.Get(keys[i]) == nil {
b.FailNow()
}
}
}
func BenchmarkCacheSet(b *testing.B) {
ce := NewCache()
keys := make([]string, b.N)
for i := range keys {
keys[i] = fmt.Sprintf("key %d", i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ce.Set(keys[i], i)
}
}
func TestCacheTime(t *testing.T) {
ce := NewCache()
n := 2097152
keys := make([]string, n)
for i := range keys {
keys[i] = fmt.Sprintf("key %d", i)
}
var buf [4096]byte
for i := range buf {
buf[i] = 0xff
}
tm := time.Now()
for i := 0; i < n; i++ {
b := make([]byte, len(buf))
copy(b, buf[:])
ce.Set(keys[i], b)
}
t.Log("set", time.Now().Sub(tm))
tm = time.Now()
for i := 0; i < n; i++ {
b := ce.Get(keys[i])
copy(buf[:], b.([]byte))
if i == 1000000 {
fmt.Println(buf)
}
}
t.Log("get", time.Now().Sub(tm))
}