benchmark.go 4.0 KB

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