bench_writer.go 2.8 KB

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