benchmark.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // 建索引
  100. log.Print("建索引 ... ")
  101. docId := uint64(1)
  102. for i := 0; i < *num_repeat_text; i++ {
  103. for _, line := range lines {
  104. searcher.IndexDocument(docId, types.DocumentIndexData{
  105. Content: line})
  106. docId++
  107. if docId-docId/1000000*1000000 == 0 {
  108. log.Printf("已索引%d百万文档", docId/1000000)
  109. runtime.GC()
  110. }
  111. }
  112. }
  113. searcher.FlushIndex()
  114. log.Print("加入的索引总数", searcher.NumTokenIndexAdded())
  115. // 记录时间
  116. t1 := time.Now()
  117. log.Printf("建立索引花费时间 %v", t1.Sub(t0))
  118. log.Printf("建立索引速度每秒添加 %f 百万个索引",
  119. float64(searcher.NumTokenIndexAdded())/t1.Sub(t0).Seconds()/(1000000))
  120. // 写入内存profile文件
  121. if *memprofile != "" {
  122. f, err := os.Create(*memprofile)
  123. if err != nil {
  124. log.Fatal(err)
  125. }
  126. pprof.WriteHeapProfile(f)
  127. defer f.Close()
  128. }
  129. // 记录时间
  130. t2 := time.Now()
  131. // 打开处理器profile文件
  132. if *cpuprofile != "" {
  133. f, err := os.Create(*cpuprofile)
  134. if err != nil {
  135. log.Fatal(err)
  136. }
  137. pprof.StartCPUProfile(f)
  138. defer pprof.StopCPUProfile()
  139. }
  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. // 停止处理器profile
  148. if *cpuprofile != "" {
  149. defer pprof.StopCPUProfile()
  150. }
  151. // 记录时间并计算分词速度
  152. t3 := time.Now()
  153. log.Printf("搜索平均响应时间 %v 毫秒",
  154. t3.Sub(t2).Seconds()*1000/float64(numRepeatQuery*len(searchQueries)))
  155. log.Printf("搜索吞吐量每秒 %v 次查询",
  156. float64(numRepeatQuery*numQueryThreads*len(searchQueries))/
  157. t3.Sub(t2).Seconds())
  158. if *use_persistent {
  159. searcher.Close()
  160. t4 := time.Now()
  161. searcher1 := engine.Engine{}
  162. searcher1.Init(types.EngineInitOptions{
  163. SegmenterDictionaries: *dictionaries,
  164. StopTokenFile: *stop_token_file,
  165. IndexerInitOptions: &types.IndexerInitOptions{
  166. IndexType: *index_type,
  167. },
  168. NumShards: NumShards,
  169. DefaultRankOptions: &options,
  170. UsePersistentStorage: *use_persistent,
  171. PersistentStorageFolder: *persistent_storage_folder,
  172. PersistentStorageShards: *persistent_storage_shards,
  173. })
  174. defer searcher1.Close()
  175. t5 := time.Now()
  176. t := t5.Sub(t4).Seconds() - tEndInit.Sub(tBeginInit).Seconds()
  177. log.Print("从持久存储加入的索引总数", searcher1.NumTokenIndexAdded())
  178. log.Printf("从持久存储建立索引花费时间 %v", t)
  179. log.Printf("从持久存储建立索引速度每秒添加 %f 百万个索引",
  180. float64(searcher1.NumTokenIndexAdded())/t/(1000000))
  181. }
  182. os.RemoveAll(*persistent_storage_folder)
  183. }
  184. func search(ch chan bool) {
  185. for i := 0; i < numRepeatQuery; i++ {
  186. for _, query := range searchQueries {
  187. searcher.Search(types.SearchRequest{Text: query})
  188. }
  189. }
  190. ch <- true
  191. }