benchmark.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. use_persistent = flag.Bool("use_persistent", false, "是否使用持久存储")
  41. persistent_storage_folder = flag.String("persistent_storage_folder", "benchmark.persistent", "持久存储数据库保存的目录")
  42. persistent_storage_shards = flag.Int("persistent_storage_shards", 0, "持久数据库存储裂分数目")
  43. searcher = engine.Engine{}
  44. options = types.RankOptions{
  45. OutputOffset: 0,
  46. MaxOutputs: 100,
  47. }
  48. searchQueries = []string{}
  49. NumShards = 2
  50. numQueryThreads = runtime.NumCPU() / NumShards
  51. )
  52. func main() {
  53. // 解析命令行参数
  54. flag.Parse()
  55. searchQueries = strings.Split(*queries, ",")
  56. log.Printf("待搜索的关键词为\"%s\"", searchQueries)
  57. // 初始化
  58. tBeginInit := time.Now()
  59. searcher.Init(types.EngineInitOptions{
  60. SegmenterDictionaries: *dictionaries,
  61. StopTokenFile: *stop_token_file,
  62. IndexerInitOptions: &types.IndexerInitOptions{
  63. IndexType: *index_type,
  64. },
  65. NumShards: NumShards,
  66. DefaultRankOptions: &options,
  67. UsePersistentStorage: *use_persistent,
  68. PersistentStorageFolder: *persistent_storage_folder,
  69. PersistentStorageShards: *persistent_storage_shards,
  70. })
  71. tEndInit := time.Now()
  72. defer searcher.Close()
  73. // 打开将要搜索的文件
  74. file, err := os.Open(*weibo_data)
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. defer file.Close()
  79. // 逐行读入
  80. log.Printf("读入文本 %s", *weibo_data)
  81. scanner := bufio.NewScanner(file)
  82. lines := []string{}
  83. size := 0
  84. for scanner.Scan() {
  85. var text string
  86. data := strings.Split(scanner.Text(), "||||")
  87. if len(data) != 10 {
  88. continue
  89. }
  90. text = data[9]
  91. if text != "" {
  92. size += len(text) * (*num_repeat_text)
  93. lines = append(lines, text)
  94. }
  95. }
  96. log.Print("文件行数", len(lines))
  97. // 记录时间
  98. t0 := time.Now()
  99. // 打开处理器profile文件
  100. if *cpuprofile != "" {
  101. f, err := os.Create(*cpuprofile)
  102. if err != nil {
  103. log.Fatal(err)
  104. }
  105. pprof.StartCPUProfile(f)
  106. defer pprof.StopCPUProfile()
  107. }
  108. // 建索引
  109. log.Print("建索引 ... ")
  110. docId := uint64(1)
  111. for i := 0; i < *num_repeat_text; i++ {
  112. for _, line := range lines {
  113. searcher.IndexDocument(docId, types.DocumentIndexData{
  114. Content: line})
  115. docId++
  116. if docId-docId/1000000*1000000 == 0 {
  117. log.Printf("已索引%d百万文档", docId/1000000)
  118. runtime.GC()
  119. }
  120. }
  121. }
  122. searcher.FlushIndex()
  123. log.Print("加入的索引总数", searcher.NumTokenIndexAdded())
  124. // 记录时间
  125. t1 := time.Now()
  126. log.Printf("建立索引花费时间 %v", t1.Sub(t0))
  127. log.Printf("建立索引速度每秒添加 %f 百万个索引",
  128. float64(searcher.NumTokenIndexAdded())/t1.Sub(t0).Seconds()/(1000000))
  129. // 写入内存profile文件
  130. if *memprofile != "" {
  131. f, err := os.Create(*memprofile)
  132. if err != nil {
  133. log.Fatal(err)
  134. }
  135. pprof.WriteHeapProfile(f)
  136. defer f.Close()
  137. }
  138. // 记录时间
  139. t2 := time.Now()
  140. done := make(chan bool)
  141. for iThread := 0; iThread < numQueryThreads; iThread++ {
  142. go search(done)
  143. }
  144. for iThread := 0; iThread < numQueryThreads; iThread++ {
  145. <-done
  146. }
  147. // 记录时间并计算分词速度
  148. t3 := time.Now()
  149. log.Printf("搜索平均响应时间 %v 毫秒",
  150. t3.Sub(t2).Seconds()*1000/float64(numRepeatQuery*len(searchQueries)))
  151. log.Printf("搜索吞吐量每秒 %v 次查询",
  152. float64(numRepeatQuery*numQueryThreads*len(searchQueries))/
  153. t3.Sub(t2).Seconds())
  154. if *use_persistent {
  155. searcher.Close()
  156. t4 := time.Now()
  157. searcher1 := engine.Engine{}
  158. searcher1.Init(types.EngineInitOptions{
  159. SegmenterDictionaries: *dictionaries,
  160. StopTokenFile: *stop_token_file,
  161. IndexerInitOptions: &types.IndexerInitOptions{
  162. IndexType: *index_type,
  163. },
  164. NumShards: NumShards,
  165. DefaultRankOptions: &options,
  166. UsePersistentStorage: *use_persistent,
  167. PersistentStorageFolder: *persistent_storage_folder,
  168. PersistentStorageShards: *persistent_storage_shards,
  169. })
  170. defer searcher1.Close()
  171. t5 := time.Now()
  172. t := t5.Sub(t4).Seconds() - tEndInit.Sub(tBeginInit).Seconds()
  173. log.Print("从持久存储加入的索引总数", searcher1.NumTokenIndexAdded())
  174. log.Printf("从持久存储建立索引花费时间 %v 秒", t)
  175. log.Printf("从持久存储建立索引速度每秒添加 %f 百万个索引",
  176. float64(searcher1.NumTokenIndexAdded())/t/(1000000))
  177. }
  178. //os.RemoveAll(*persistent_storage_folder)
  179. }
  180. func search(ch chan bool) {
  181. for i := 0; i < numRepeatQuery; i++ {
  182. for _, query := range searchQueries {
  183. searcher.Search(types.SearchRequest{Text: query})
  184. }
  185. }
  186. ch <- true
  187. }