-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdycache_test.go
More file actions
97 lines (86 loc) · 1.51 KB
/
dycache_test.go
File metadata and controls
97 lines (86 loc) · 1.51 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
93
94
95
96
97
package gcache
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
"github.com/golang/groupcache/lru"
)
var lock sync.Mutex
var r *rand.Rand
func init() {
r = rand.New(rand.NewSource(time.Now().Unix()))
}
func setIfNotEx(cache *lru.Cache, key lru.Key, value interface{}) bool {
lock.Lock()
defer lock.Unlock()
_, ex := cache.Get(key)
if ex {
return true
}
cache.Add(key, value)
return false
}
func TestOnEvicted(t *testing.T) {
cache := lru.New(90)
WarpCache(cache, nil, time.Millisecond*20000)
// sample := 6000
sample := 6000000
data := simulateColdData()
// data := simulateRandData()
hit := 0
m := make(map[int]bool, 100)
for i := 0; i < sample; i++ {
a := r.Intn(100)
// if a > 80 {
a += <-data
// }
m[a] = true
if setIfNotEx(cache, a, i) {
hit++
}
}
t.Logf("hit:%d, rate:%.2f%%", hit, float64(hit)*100/float64(sample))
t.Logf("data-range:%d\n%+v", len(m), m)
}
// cold-data: 100ms
func simulateColdData() chan int {
// ticker := time.NewTicker()
timer := time.NewTimer(time.Millisecond * 200)
now := time.Now().UnixNano()
ch := make(chan int, 2)
go func(now *int64) {
for {
<-timer.C
*now += 50
}
}(&now)
go func() {
for {
// fmt.Println(now)
if now%500 > 100 {
ch <- 100
fmt.Print(".")
} else {
ch <- 0
}
}
}()
return ch
}
// rand-data: plus 100
func simulateRandData() chan int {
ch := make(chan int, 2)
go func() {
for {
i := time.Now().UnixNano()
if i%2 == 1 {
ch <- 100
} else {
ch <- 0
}
}
}()
return ch
}