forked from go-ego/riot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.go
executable file
·311 lines (272 loc) · 7.33 KB
/
benchmark.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// riot 性能测试
//
package main
import (
"bufio"
"flag"
"log"
"math/rand"
"os"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"sync"
"time"
"github.com/go-ego/riot"
"github.com/go-ego/riot/types"
)
const (
numRepeatQuery = 1000
)
var (
weiboData = flag.String(
"weibo_data",
"../../testdata/weibo_data.txt",
"微博数据")
queries = flag.String(
"queries",
"女人, 你好世界, 网络草根, 热门微博, 红十字会,"+
"鳄鱼表演, 星座歧视, chinajoy, 高帅富, 假期计划",
"待搜索的关键词")
dictionaries = flag.String(
"dictionaries",
"../../data/dict/dictionary.txt",
"分词字典文件")
stopTokenFile = flag.String(
"stop_token_file",
"../../data/dict/stop_tokens.txt",
"停用词文件")
idOnly = flag.Bool("idonly", true, "search return id only")
cpuprofile = flag.String("cpuprofile", "", "处理器profile文件")
memprofile = flag.String("memprofile", "", "内存profile文件")
numRepeatText = flag.Int("numRepeatText", 10, "文本重复加入多少次")
numDeleteDocs = flag.Int("numDeleteDocs", 1000, "测试删除文档的个数")
indexType = flag.Int("indexType", types.DocIdsIndex, "索引类型")
usePersistent = flag.Bool("useStore", false, "是否使用持久存储")
storeFolder = flag.String("storeFolder",
"benchmark.persistent", "持久存储数据库保存的目录")
storageEngine = flag.String("storeEngine", "lbd", "use store engine")
storeShards = flag.Int("storeShards", 0, "持久数据库存储裂分数目")
searcher = riot.Engine{}
options = types.RankOpts{
OutputOffset: 0,
MaxOutputs: 100,
}
searchQueries = []string{}
// numShards shards number
numShards = 2
numQueryThreads = runtime.NumCPU() / numShards
t0 time.Time
)
func initEngine() {
searcher.Init(types.EngineOpts{
IDOnly: *idOnly,
GseDict: *dictionaries,
StopTokenFile: *stopTokenFile,
IndexerOpts: &types.IndexerOpts{
IndexType: *indexType,
},
NumShards: numShards,
DefRankOpts: &options,
UseStore: *usePersistent,
StoreFolder: *storeFolder,
StoreShards: *storeShards,
})
}
func openFile() {
// 打开将要搜索的文件
file, err := os.Open(*weiboData)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// 逐行读入
log.Printf("读入文本 %s", *weiboData)
scanner := bufio.NewScanner(file)
lines := []string{}
size := 0
for scanner.Scan() {
var text string
data := strings.Split(scanner.Text(), "||||")
if len(data) != 10 {
continue
}
text = data[9]
if text != "" {
size += len(text) * (*numRepeatText)
lines = append(lines, text)
}
}
log.Println("size ...", size)
log.Print("文件行数: ", len(lines))
// 记录时间
t0 = time.Now()
// 打开处理器 profile 文件
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// 建索引
log.Print("建索引 ... ")
// 打乱 docId 顺序进行测试,若 docId 最大值超 Int 则不能用 rand.Perm 方法
docIds := rand.Perm(*numRepeatText * len(lines))
docIdx := 0
for i := 0; i < *numRepeatText; i++ {
for _, line := range lines {
searcher.Index(
strconv.Itoa(docIds[docIdx]+1),
types.DocData{Content: line})
docIdx++
if docIdx-docIdx/1000000*1000000 == 0 {
log.Printf("已索引%d百万文档", docIdx/1000000)
runtime.GC()
}
}
}
}
func deleteDoc() {
// 记录时间
t1 := time.Now()
log.Printf("建立索引花费时间 %v", t1.Sub(t0))
log.Printf("建立索引速度每秒添加 %f 百万个索引",
float64(searcher.NumTokenIndexAdded())/t1.Sub(t0).Seconds()/(1000000))
// 记录时间并计算删除索引时间
t2 := time.Now()
for i := 1; i <= *numDeleteDocs; i++ {
searcher.RemoveDoc(strconv.Itoa(i))
}
searcher.Flush()
t3 := time.Now()
log.Printf("删除 %d 条索引花费时间 %v", *numDeleteDocs, t3.Sub(t2))
}
func searchQ() {
t4 := time.Now()
done := make(chan bool)
recordResponse := recordResponseLock{}
recordResponse.count = make(map[string]int)
for iThread := 0; iThread < numQueryThreads; iThread++ {
go search(done, &recordResponse)
}
for iThread := 0; iThread < numQueryThreads; iThread++ {
<-done
}
// 记录时间并计算分词速度
t5 := time.Now()
log.Printf("搜索平均响应时间 %v 毫秒",
t5.Sub(t4).Seconds()*1000/float64(numRepeatQuery*len(searchQueries)))
log.Printf("搜索吞吐量每秒 %v 次查询",
float64(numRepeatQuery*numQueryThreads*len(searchQueries))/
t5.Sub(t4).Seconds())
// 测试搜索结果输出,因为不同 case 的 docId 对应不上,所以只测试总数
recordResponse.RLock()
for keyword, count := range recordResponse.count {
log.Printf("关键词 [%s] 共搜索到 %d 个相关文档", keyword, count)
}
recordResponse.RUnlock()
}
func useStore(tBeginInit, tEndInit time.Time) {
searcher.Close()
t6 := time.Now()
searcher1 := riot.Engine{}
searcher1.Init(types.EngineOpts{
IDOnly: true,
GseDict: *dictionaries,
StopTokenFile: *stopTokenFile,
IndexerOpts: &types.IndexerOpts{
IndexType: *indexType,
},
NumShards: numShards,
DefRankOpts: &options,
UseStore: *usePersistent,
StoreFolder: *storeFolder,
StoreEngine: *storageEngine,
StoreShards: *storeShards,
})
defer searcher1.Close()
t7 := time.Now()
t := t7.Sub(t6).Seconds() - tEndInit.Sub(tBeginInit).Seconds()
log.Print("从持久存储加入的索引总数", searcher1.NumTokenIndexAdded())
log.Printf("从持久存储建立索引花费时间 %v 秒", t)
log.Printf("从持久存储建立索引速度每秒添加 %f 百万个索引",
float64(searcher1.NumTokenIndexAdded())/t/(1000000))
}
// TestBM BM test
func TestBM() {
// 解析命令行参数
flag.Parse()
searchQueries = strings.Split(*queries, ",")
log.Printf("待搜索的关键词为\"%s\"", searchQueries)
// 初始化
tBeginInit := time.Now()
initEngine()
tEndInit := time.Now()
defer searcher.Close()
openFile()
searcher.Flush()
log.Print("加入的索引总数: ", searcher.NumTokenAdded())
deleteDoc()
// 手动做 GC 防止影响性能测试
time.Sleep(time.Second)
runtime.GC()
// 写入内存 profile 文件
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
defer f.Close()
}
searchQ()
if *usePersistent {
useStore(tBeginInit, tEndInit)
//os.RemoveAll(*storeFolder)
}
log.Println("end...")
}
func main() {
TestBM()
}
type recordResponseLock struct {
sync.RWMutex
count map[string]int
}
func search(ch chan bool, record *recordResponseLock) {
for i := 0; i < numRepeatQuery; i++ {
for _, query := range searchQueries {
output := searcher.SearchID(types.SearchReq{Text: query})
record.RLock()
if _, found := record.count[query]; !found {
record.RUnlock()
record.Lock()
record.count[query] = len(output.Docs)
record.Unlock()
} else {
record.RUnlock()
}
}
}
ch <- true
}
func searchDoc(ch chan bool, record *recordResponseLock) {
for i := 0; i < numRepeatQuery; i++ {
for _, query := range searchQueries {
output := searcher.SearchDoc(types.SearchReq{Text: query})
record.RLock()
if _, found := record.count[query]; !found {
record.RUnlock()
record.Lock()
record.count[query] = len(output.Docs)
record.Unlock()
} else {
record.RUnlock()
}
}
}
ch <- true
}