-
Notifications
You must be signed in to change notification settings - Fork 3
/
batchget_test.go
213 lines (184 loc) · 6.25 KB
/
batchget_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package craw
import (
"fmt"
"log"
"testing"
"time"
"github.com/wonderivan/logger"
)
func init() {
logger.SetLogger(`{"Console": {"level": "WARN"}}`)
}
type benchCraw struct {
}
func (this *benchCraw) Init() error {
return nil
}
func (this *benchCraw) CustomGet(key string) (data interface{}, expired time.Duration, err error) {
return key, -1, nil
}
func (this *benchCraw) CustomSet(key string, data interface{}) error {
return nil
}
func (this *benchCraw) Destroy() {
}
func TestGetByOrder(t *testing.T) {
handler := NewCraw("mytest", new(benchCraw))
defer handler.Destroy()
total := 2000000
keys := make([]string, total)
fmt.Printf("fill cache use %d test data\n", total)
for i := 0; i < total; i++ {
keys[i] = fmt.Sprintf("key%d", i)
handler.SetCraw(keys[i], keys[i], -1)
}
start := time.Now()
for i := 0; i < total; i++ {
_, err := handler.GetData(keys[i])
if err != nil {
log.Fatalf("GetData err:%s", err)
}
}
interval := time.Since(start)
fmt.Printf("test %d use %v, average:%.2fns/op,%.2fop/s \n",
total, interval, float64(interval)/float64(total), float64(total)/float64(interval)*1000000000)
}
var outputType int
var outputGraph map[string][]string
func TestStatisticsTable(t *testing.T) {
outputType = 1
hitRate := []int{0, 1, 3, 5, 7, 10}
fmt.Println("测试量|读取命中率|总用时|每条读取耗时|每秒读取条数")
fmt.Println("|-|-|-|-|-|")
ExecStatisticsGet(10000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsGet(50000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsGet(100000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsGet(500000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsGet(1000000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsGet(2000000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
fmt.Println()
fmt.Println("测试量|写入命中率|总用时|每条写入耗时|每秒写入条数")
fmt.Println("|-|-|-|-|-|")
ExecStatisticsSet(10000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsSet(50000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsSet(100000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsSet(500000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsSet(1000000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsSet(2000000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
fmt.Println()
}
func TestStatisticsGraph(t *testing.T) {
outputType = 2
outputGraph = make(map[string][]string)
hitRate := []int{0, 1, 3, 5, 7, 10}
ExecStatisticsGet(10000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsGet(50000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsGet(100000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsGet(500000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsGet(1000000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsGet(2000000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
fmt.Println("读取命中率\t1W\t5W\t10W\t50W\t100W\t200W")
for _, hit := range hitRate {
k := fmt.Sprintf("%.2f%%", float32(hit)/10*100)
v := outputGraph[k]
fmt.Printf("%s", k)
for _, v1 := range v {
fmt.Printf("\t%s", v1)
}
fmt.Println()
}
fmt.Println()
outputGraph = make(map[string][]string)
ExecStatisticsSet(10000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsSet(50000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsSet(100000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsSet(500000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsSet(1000000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
ExecStatisticsSet(2000000, hitRate) // 0%,10%,30%,50%,70%,100%命中率
fmt.Println("写入命中率\t1W\t5W\t10W\t50W\t100W\t200W")
for _, hit := range hitRate {
k := fmt.Sprintf("%.2f%%", float32(hit)/10*100)
v := outputGraph[k]
fmt.Printf("%s", k)
for _, v1 := range v {
fmt.Printf("\t%s", v1)
}
fmt.Println()
}
fmt.Println()
}
func ExecStatisticsGet(total int, hits []int) {
for _, hit := range hits {
ExecStatisticsTestGet(total, hit)
}
}
func ExecStatisticsSet(total int, hits []int) {
for _, hit := range hits {
ExecStatisticsTestSet(total, hit)
}
}
// hit : 0-10 => 0%-100%
func ExecStatisticsTestGet(total, hit int) {
handler := NewCraw("mytest1", new(benchCraw))
defer handler.Destroy()
keys := make([]string, total)
for i := 0; i < total; i++ {
keys[i] = fmt.Sprintf("key%d", i)
if i%10 < hit {
handler.SetCraw(keys[i], keys[i], -1)
}
}
start := time.Now()
for i := 0; i < total; i++ {
_, err := handler.GetData(keys[i])
if err != nil {
log.Fatalf("GetData err:%s", err)
}
}
interval := time.Since(start)
hitRate := float32(hit) / 10 * 100
if int(hitRate) != int(handler.HitRate()) {
log.Fatalf("Unexpect hitRate:%v,%v", hitRate, handler.HitRate())
}
switch outputType {
case 1:
fmt.Printf("|%d|%.2f%%|%v|%.2fns/op|%.2fop|\n",
total, hitRate,
interval, float64(interval)/float64(total), float64(total)/float64(interval)*1000000000)
case 2:
outputGraph[fmt.Sprintf("%.2f%%", hitRate)] = append(outputGraph[fmt.Sprintf("%.2f%%", hitRate)], fmt.Sprintf("%.2f", float64(total)/float64(interval)*100000))
fmt.Printf("|%d|%.2f%%|%.2fW|\n",
total, hitRate, float64(total)/float64(interval)*100000)
}
}
// hit : 0-10 => 0%-100%
func ExecStatisticsTestSet(total, hit int) {
handler := NewCraw("mytest1", new(benchCraw))
defer handler.Destroy()
keys := make([]string, total)
for i := 0; i < total; i++ {
keys[i] = fmt.Sprintf("key%d", i)
if i%10 < hit {
handler.SetCraw(keys[i], keys[i], -1)
}
}
start := time.Now()
for i := 0; i < total; i++ {
err := handler.SetCraw(keys[i], keys[i], -1)
if err != nil {
log.Fatalf("SetData err:%s", err)
}
}
interval := time.Since(start)
hitRate := float32(hit) / 10 * 100
switch outputType {
case 1:
fmt.Printf("|%d|%.2f%%|%v|%.2fns/op|%.2fop|\n",
total, hitRate,
interval, float64(interval)/float64(total), float64(total)/float64(interval)*1000000000)
case 2:
outputGraph[fmt.Sprintf("%.2f%%", hitRate)] = append(outputGraph[fmt.Sprintf("%.2f%%", hitRate)], fmt.Sprintf("%.2f", float64(total)/float64(interval)*100000))
fmt.Printf("|%d|%.2f%%|%.2fW|\n",
total, hitRate, float64(total)/float64(interval)*100000)
}
}