benchmark.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // 悟空性能测试
  2. package main
  3. import (
  4. "bufio"
  5. "flag"
  6. "github.com/huichen/wukong/engine"
  7. "github.com/huichen/wukong/types"
  8. "log"
  9. "os"
  10. "runtime"
  11. "runtime/pprof"
  12. "strings"
  13. "time"
  14. )
  15. const (
  16. numRepeatQuery = 1000
  17. )
  18. var (
  19. weibo_data = flag.String(
  20. "weibo_data",
  21. "../testdata/weibo_data.txt",
  22. "微博数据,每行的格式是\"<id>||||<timestamp>||||<uid>||||"+
  23. "<reposts count>||||<text>\",否则每行只是文本")
  24. queries = flag.String(
  25. "queries",
  26. "女人母亲,你好中国,网络草根,热门微博,红十字会,"+
  27. "鳄鱼表演,星座歧视,chinajoy,高帅富,假期计划",
  28. "待搜索的关键词")
  29. dictionaries = flag.String(
  30. "dictionaries",
  31. "../data/dictionary.txt",
  32. "分词字典文件")
  33. stop_token_file = flag.String(
  34. "stop_token_file",
  35. "../data/stop_tokens.txt",
  36. "停用词文件")
  37. cpuprofile = flag.String("cpuprofile", "", "处理器profile文件")
  38. memprofile = flag.String("memprofile", "", "内存profile文件")
  39. num_repeat_text = flag.Int("num_repeat_text", 10, "文本重复加入多少次")
  40. index_type = flag.Int("index_type", types.DocIdsIndex, "索引类型")
  41. searcher = engine.Engine{}
  42. options = types.RankOptions{
  43. OutputOffset: 0,
  44. MaxOutputs: 100,
  45. }
  46. searchQueries = []string{}
  47. NumShards = 2
  48. numQueryThreads = runtime.NumCPU() / NumShards
  49. )
  50. func main() {
  51. // 解析命令行参数
  52. flag.Parse()
  53. searchQueries = strings.Split(*queries, ",")
  54. log.Printf("待搜索的关键词为\"%s\"", searchQueries)
  55. // 初始化
  56. searcher.Init(types.EngineInitOptions{
  57. SegmenterDictionaries: *dictionaries,
  58. StopTokenFile: *stop_token_file,
  59. IndexerInitOptions: &types.IndexerInitOptions{
  60. IndexType: *index_type,
  61. },
  62. NumShards: NumShards,
  63. DefaultRankOptions: &options,
  64. })
  65. // 打开将要搜索的文件
  66. file, err := os.Open(*weibo_data)
  67. if err != nil {
  68. log.Fatal(err)
  69. }
  70. defer file.Close()
  71. // 逐行读入
  72. log.Printf("读入文本 %s", *weibo_data)
  73. scanner := bufio.NewScanner(file)
  74. lines := []string{}
  75. size := 0
  76. for scanner.Scan() {
  77. var text string
  78. data := strings.Split(scanner.Text(), "||||")
  79. if len(data) != 10 {
  80. continue
  81. }
  82. text = data[9]
  83. if text != "" {
  84. size += len(text) * (*num_repeat_text)
  85. lines = append(lines, text)
  86. }
  87. }
  88. log.Print("文件行数", len(lines))
  89. // 记录时间
  90. t0 := time.Now()
  91. // 建索引
  92. log.Print("建索引 ... ")
  93. docId := uint64(1)
  94. for i := 0; i < *num_repeat_text; i++ {
  95. for _, line := range lines {
  96. searcher.IndexDocument(docId, types.DocumentIndexData{
  97. Content: line})
  98. docId++
  99. if docId-docId/1000000*1000000 == 0 {
  100. log.Printf("已索引%d百万文档", docId/1000000)
  101. runtime.GC()
  102. }
  103. }
  104. }
  105. searcher.FlushIndex()
  106. log.Print("加入的索引总数", searcher.NumTokenIndexAdded())
  107. // 记录时间
  108. t1 := time.Now()
  109. log.Printf("建立索引花费时间 %v", t1.Sub(t0))
  110. log.Printf("建立索引速度每秒添加 %f 百万个索引",
  111. float64(searcher.NumTokenIndexAdded())/t1.Sub(t0).Seconds()/(1000000))
  112. // 写入内存profile文件
  113. if *memprofile != "" {
  114. f, err := os.Create(*memprofile)
  115. if err != nil {
  116. log.Fatal(err)
  117. }
  118. pprof.WriteHeapProfile(f)
  119. defer f.Close()
  120. }
  121. // 记录时间
  122. t2 := time.Now()
  123. // 打开处理器profile文件
  124. if *cpuprofile != "" {
  125. f, err := os.Create(*cpuprofile)
  126. if err != nil {
  127. log.Fatal(err)
  128. }
  129. pprof.StartCPUProfile(f)
  130. defer pprof.StopCPUProfile()
  131. }
  132. done := make(chan bool)
  133. for iThread := 0; iThread < numQueryThreads; iThread++ {
  134. go search(done)
  135. }
  136. for iThread := 0; iThread < numQueryThreads; iThread++ {
  137. <-done
  138. }
  139. // 停止处理器profile
  140. if *cpuprofile != "" {
  141. defer pprof.StopCPUProfile()
  142. }
  143. // 记录时间并计算分词速度
  144. t3 := time.Now()
  145. log.Printf("搜索平均响应时间 %v 毫秒",
  146. t3.Sub(t2).Seconds()*1000/float64(numRepeatQuery*len(searchQueries)))
  147. log.Printf("搜索吞吐量每秒 %v 次查询",
  148. float64(numRepeatQuery*numQueryThreads*len(searchQueries))/
  149. t3.Sub(t2).Seconds())
  150. }
  151. func search(ch chan bool) {
  152. for i := 0; i < numRepeatQuery; i++ {
  153. for _, query := range searchQueries {
  154. searcher.Search(types.SearchRequest{Text: query})
  155. }
  156. }
  157. ch <- true
  158. }