benchmark.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "math/rand"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. "go-common/app/service/main/riot-search/model"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. xtime "go-common/library/time"
  13. "github.com/ivpusic/grpool"
  14. )
  15. var (
  16. minID uint64 = 1
  17. maxID uint64 = 28731894
  18. keyword = []string{"世界", "鬼畜", "自制", "搬运", "动漫", "崩坏", "搞笑", "德国", "弹幕", "乱入", "吸血鬼", "可怕", "骑士", "团长", "守护"}
  19. times int
  20. count int
  21. thread int
  22. client *bm.Client
  23. args []*model.RiotSearchReq
  24. uri string
  25. maxElapsedTime []int64
  26. avgElapsedTime []int64
  27. )
  28. //生成count个[start,end)结束的不重复的随机数
  29. func generateRandomNumber(start uint64, end uint64, count int) []uint64 {
  30. if end < start || (end-start) < uint64(count) {
  31. return nil
  32. }
  33. nums := make([]uint64, 0)
  34. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  35. for len(nums) < count {
  36. num := uint64(r.Intn(int(end-start))) + start
  37. exist := false
  38. for _, v := range nums {
  39. if v == num {
  40. exist = true
  41. break
  42. }
  43. }
  44. if !exist {
  45. nums = append(nums, num)
  46. }
  47. }
  48. return nums
  49. }
  50. func benchmarkSearch(count int, times int, gid int) {
  51. var totalTime int64
  52. var maxTime int64
  53. for i := 0; i < times; i++ {
  54. // random chose params to use
  55. arg := args[rand.Intn(len(args))]
  56. params := url.Values{}
  57. var aids string
  58. for _, id := range arg.IDs {
  59. aids += strconv.FormatUint(id, 10)
  60. }
  61. text := arg.Keyword
  62. params.Set("aids", aids)
  63. params.Set("keyword", text)
  64. params.Set("pn", "1")
  65. params.Set("ps", "20")
  66. start := time.Now()
  67. err := client.Post(context.TODO(), uri, "", params, nil)
  68. if err != nil {
  69. panic(err)
  70. }
  71. elapsed := time.Since(start)
  72. if int64(elapsed) > maxTime {
  73. maxTime = int64(elapsed)
  74. }
  75. totalTime += int64(elapsed)
  76. }
  77. avgElapsedTime[gid] = totalTime / (1000 * 1000 * int64(times))
  78. maxElapsedTime[gid] = maxTime / (1000 * 1000)
  79. }
  80. func init() {
  81. flag.IntVar(&times, "times", 100, "单个线程测试次数")
  82. flag.IntVar(&count, "count", 100000, "每次测试aid个数")
  83. flag.IntVar(&thread, "thread", 10, "线程数")
  84. flag.StringVar(&uri, "uri", "http://127.0.0.1:7871/x/internal/riot-search/arc/ids", "请求url")
  85. flag.Parse()
  86. log.Info("times: %d, count:%d, thread:%d, uri:%s", times, count, thread, uri)
  87. log.Info("init http client")
  88. app := &bm.App{
  89. Key: "test",
  90. Secret: "test",
  91. }
  92. clientConf := &bm.ClientConfig{
  93. App: app,
  94. Timeout: xtime.Duration(time.Second * 1),
  95. Dial: xtime.Duration(time.Second),
  96. KeepAlive: xtime.Duration(time.Second * 60),
  97. }
  98. client = bm.NewClient(clientConf)
  99. log.Info("init 10 http request params, random chose one to test")
  100. args = make([]*model.RiotSearchReq, 10)
  101. rand.Seed(time.Now().UnixNano())
  102. for i := 0; i < 10; i++ {
  103. arg := &model.RiotSearchReq{
  104. IDs: generateRandomNumber(minID, maxID, count),
  105. Keyword: keyword[rand.Intn(len(keyword))],
  106. }
  107. args[i] = arg
  108. }
  109. maxElapsedTime = make([]int64, thread)
  110. avgElapsedTime = make([]int64, thread)
  111. log.Info("init params finished")
  112. }
  113. func main() {
  114. log.Info("start test")
  115. if thread >= 1000 {
  116. panic("thread large than 1000 is not allowed")
  117. }
  118. pool := grpool.NewPool(thread, 10240)
  119. defer pool.Release()
  120. pool.WaitCount(thread)
  121. for i := 0; i < thread; i++ {
  122. threadNum := i
  123. pool.JobQueue <- func() {
  124. benchmarkSearch(count, times, threadNum)
  125. pool.JobDone()
  126. }
  127. }
  128. pool.WaitAll()
  129. log.Info("avg elapsed times list: %v", avgElapsedTime)
  130. log.Info("max elapsed times list: %v", maxElapsedTime)
  131. log.Info("test finished")
  132. }