-
Notifications
You must be signed in to change notification settings - Fork 3
/
craw_test.go
executable file
·113 lines (95 loc) · 2.34 KB
/
craw_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
package craw
import (
"fmt"
"log"
"testing"
"time"
)
type cacheGet struct {
testV int
}
func (this *cacheGet) Init() error {
this.testV = 0
return nil
}
func (this *cacheGet) CustomGet(key string) (data interface{}, expired time.Duration, err error) {
this.testV++
return this.testV, 0, nil
}
func (this *cacheGet) CustomSet(key string, data interface{}) error {
return nil
}
func (this *cacheGet) Destroy() {
}
func TestCrawGetPut(t *testing.T) {
handler := NewCraw("mytest", new(cacheGet))
defer handler.Destroy()
count := 0
for count < 10 {
v, err := handler.GetData("k1")
if err != nil {
log.Fatalf("GetData err:%s", err)
}
log.Printf("GetData type(%T), value:%v", v, v)
count = v.(int)
err = handler.SetCraw("k1", count+1, 1)
if err != nil {
log.Fatalf("GetData err:%s", err)
}
}
}
func TestCrawHitRate(t *testing.T) {
handler := NewCraw("mytest", new(cacheGet))
defer handler.Destroy()
count := 0
for count < 100000 {
key := fmt.Sprintf("key%d", count)
_, err := handler.GetData(key)
if err != nil {
log.Fatalf("GetData err:%s", err)
}
//log.Printf("GetData %s type(%T), value:%v", key, v, v)
if count%3 == 0 {
key = fmt.Sprintf("key%d", count+1)
err = handler.SetCraw(key, count+1, 10)
if err != nil {
log.Fatalf("GetData err:%s", err)
}
//log.Printf("SetData %s value:%v", key, v)
}
count++
}
log.Printf("hit rate:%v", handler.HitRate()) // output: 1/3 = 33.3333%
}
// go test -benchmem -run=^$ -bench ^BenchmarkPut$
func BenchmarkPut(b *testing.B) {
handler := NewCraw("mytest2", new(cacheGet))
defer handler.Destroy()
b.ResetTimer()
b.N = 10000000
keys := make([]string, b.N)
for i := 0; i < b.N; i++ {
keys[i] = fmt.Sprintf("key%d", i)
}
for i := 0; i < b.N; i++ {
handler.SetCraw(keys[i], keys[i], -1)
}
}
// go test -benchmem -run=^$ -bench ^BenchmarkGet$
func BenchmarkGet(b *testing.B) {
handler := NewCraw("mytest", new(cacheGet))
defer handler.Destroy()
b.N = 10000000
keys := make([]string, b.N)
for i := 0; i < b.N; i++ {
keys[i] = fmt.Sprintf("key%d", i)
handler.SetCraw(keys[i], keys[i], -1)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := handler.GetData(keys[i])
if err != nil {
log.Fatalf("GetData err:%s", err)
}
}
}