bench_reader.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package main
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "net"
  8. "runtime"
  9. "strings"
  10. "sync"
  11. "sync/atomic"
  12. "time"
  13. "github.com/nsqio/go-nsq"
  14. )
  15. var (
  16. runfor = flag.Duration("runfor", 10*time.Second, "duration of time to run")
  17. tcpAddress = flag.String("nsqd-tcp-address", "127.0.0.1:4150", "<addr>:<port> to connect to nsqd")
  18. size = flag.Int("size", 200, "size of messages")
  19. topic = flag.String("topic", "sub_bench", "topic to receive messages on")
  20. channel = flag.String("channel", "ch", "channel to receive messages on")
  21. deadline = flag.String("deadline", "", "deadline to start the benchmark run")
  22. rdy = flag.Int("rdy", 2500, "RDY count to use")
  23. )
  24. var totalMsgCount int64
  25. func main() {
  26. flag.Parse()
  27. var wg sync.WaitGroup
  28. log.SetPrefix("[bench_reader] ")
  29. goChan := make(chan int)
  30. rdyChan := make(chan int)
  31. workers := runtime.GOMAXPROCS(0)
  32. for j := 0; j < workers; j++ {
  33. wg.Add(1)
  34. go func(id int) {
  35. subWorker(*runfor, workers, *tcpAddress, *topic, *channel, rdyChan, goChan, id)
  36. wg.Done()
  37. }(j)
  38. <-rdyChan
  39. }
  40. if *deadline != "" {
  41. t, err := time.Parse("2006-01-02 15:04:05", *deadline)
  42. if err != nil {
  43. log.Fatal(err)
  44. }
  45. d := t.Sub(time.Now())
  46. log.Printf("sleeping until %s (%s)", t, d)
  47. time.Sleep(d)
  48. }
  49. start := time.Now()
  50. close(goChan)
  51. wg.Wait()
  52. end := time.Now()
  53. duration := end.Sub(start)
  54. tmc := atomic.LoadInt64(&totalMsgCount)
  55. log.Printf("duration: %s - %.03fmb/s - %.03fops/s - %.03fus/op",
  56. duration,
  57. float64(tmc*int64(*size))/duration.Seconds()/1024/1024,
  58. float64(tmc)/duration.Seconds(),
  59. float64(duration/time.Microsecond)/float64(tmc))
  60. }
  61. func subWorker(td time.Duration, workers int, tcpAddr string, topic string, channel string, rdyChan chan int, goChan chan int, id int) {
  62. conn, err := net.DialTimeout("tcp", tcpAddr, time.Second)
  63. if err != nil {
  64. panic(err.Error())
  65. }
  66. conn.Write(nsq.MagicV2)
  67. rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
  68. ci := make(map[string]interface{})
  69. ci["client_id"] = "reader"
  70. ci["hostname"] = "reader"
  71. ci["user_agent"] = fmt.Sprintf("bench_reader/%s", nsq.VERSION)
  72. cmd, _ := nsq.Identify(ci)
  73. cmd.WriteTo(rw)
  74. nsq.Subscribe(topic, channel).WriteTo(rw)
  75. rdyChan <- 1
  76. <-goChan
  77. nsq.Ready(*rdy).WriteTo(rw)
  78. rw.Flush()
  79. nsq.ReadResponse(rw)
  80. nsq.ReadResponse(rw)
  81. var msgCount int64
  82. go func() {
  83. time.Sleep(td)
  84. conn.Close()
  85. }()
  86. for {
  87. resp, err := nsq.ReadResponse(rw)
  88. if err != nil {
  89. if strings.Contains(err.Error(), "use of closed network connection") {
  90. break
  91. }
  92. panic(err.Error())
  93. }
  94. frameType, data, err := nsq.UnpackResponse(resp)
  95. if err != nil {
  96. panic(err.Error())
  97. }
  98. if frameType == nsq.FrameTypeError {
  99. panic(string(data))
  100. } else if frameType == nsq.FrameTypeResponse {
  101. continue
  102. }
  103. msg, err := nsq.DecodeMessage(data)
  104. if err != nil {
  105. panic(err.Error())
  106. }
  107. nsq.Finish(msg.ID).WriteTo(rw)
  108. msgCount++
  109. if float64(msgCount%int64(*rdy)) > float64(*rdy)*0.75 {
  110. rw.Flush()
  111. }
  112. }
  113. atomic.AddInt64(&totalMsgCount, msgCount)
  114. }