watch.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cmd
  15. import (
  16. "fmt"
  17. "os"
  18. "time"
  19. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/cheggaaa/pb"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  24. )
  25. // watchCmd represents the watch command
  26. var watchCmd = &cobra.Command{
  27. Use: "watch",
  28. Short: "Benchmark watch",
  29. Long: `Benchmark watch tests the performance of processing watch requests and
  30. sending events to watchers. It tests the sending performance by
  31. changing the value of the watched keys with concurrent put
  32. requests.
  33. During the test, each watcher watches (--total/--watchers) keys
  34. (a watcher might watch on the same key multiple times if
  35. --watched-key-total is small).
  36. Each key is watched by (--total/--watched-key-total) watchers.
  37. `,
  38. Run: watchFunc,
  39. }
  40. var (
  41. watchTotalStreams int
  42. watchTotal int
  43. watchedKeyTotal int
  44. watchPutRate int
  45. watchPutTotal int
  46. )
  47. func init() {
  48. RootCmd.AddCommand(watchCmd)
  49. watchCmd.Flags().IntVar(&watchTotalStreams, "watchers", 10000, "Total number of watchers")
  50. watchCmd.Flags().IntVar(&watchTotal, "total", 100000, "Total number of watch requests")
  51. watchCmd.Flags().IntVar(&watchedKeyTotal, "watched-key-total", 10000, "Total number of keys to be watched")
  52. watchCmd.Flags().IntVar(&watchPutRate, "put-rate", 100, "Number of keys to put per second")
  53. watchCmd.Flags().IntVar(&watchPutTotal, "put-total", 10000, "Number of put requests")
  54. }
  55. func watchFunc(cmd *cobra.Command, args []string) {
  56. watched := make([][]byte, watchedKeyTotal)
  57. for i := range watched {
  58. watched[i] = mustRandBytes(32)
  59. }
  60. requests := make(chan etcdserverpb.WatchRequest, totalClients)
  61. conns := make([]*grpc.ClientConn, totalConns)
  62. for i := range conns {
  63. conns[i] = mustCreateConn()
  64. }
  65. clients := make([]etcdserverpb.WatchClient, totalClients)
  66. for i := range clients {
  67. clients[i] = etcdserverpb.NewWatchClient(conns[i%int(totalConns)])
  68. }
  69. streams := make([]etcdserverpb.Watch_WatchClient, watchTotalStreams)
  70. var err error
  71. for i := range streams {
  72. streams[i], err = clients[i%int(totalClients)].Watch(context.TODO())
  73. if err != nil {
  74. fmt.Fprintln(os.Stderr, "Failed to create watch stream:", err)
  75. os.Exit(1)
  76. }
  77. }
  78. for i := range streams {
  79. wg.Add(1)
  80. go doWatch(streams[i], requests)
  81. }
  82. // watching phase
  83. results = make(chan result)
  84. bar = pb.New(watchTotal)
  85. bar.Format("Bom !")
  86. bar.Start()
  87. pdoneC := printRate(results)
  88. go func() {
  89. for i := 0; i < watchTotal; i++ {
  90. requests <- etcdserverpb.WatchRequest{
  91. CreateRequest: &etcdserverpb.WatchCreateRequest{Key: watched[i%(len(watched))]},
  92. }
  93. }
  94. close(requests)
  95. }()
  96. wg.Wait()
  97. bar.Finish()
  98. fmt.Printf("Watch creation summary:\n")
  99. close(results)
  100. <-pdoneC
  101. // put phase
  102. kv := etcdserverpb.NewKVClient(conns[0])
  103. // total number of puts * number of watchers on each key
  104. eventsTotal := watchPutTotal * (watchTotal / watchedKeyTotal)
  105. results = make(chan result)
  106. bar = pb.New(eventsTotal)
  107. bar.Format("Bom !")
  108. bar.Start()
  109. putreqc := make(chan etcdserverpb.PutRequest)
  110. for i := 0; i < watchPutTotal; i++ {
  111. wg.Add(1)
  112. go doPut(context.TODO(), kv, putreqc)
  113. }
  114. pdoneC = printRate(results)
  115. go func() {
  116. for i := 0; i < eventsTotal; i++ {
  117. putreqc <- etcdserverpb.PutRequest{
  118. Key: watched[i%(len(watched))],
  119. Value: []byte("data"),
  120. }
  121. // TODO: use a real rate-limiter instead of sleep.
  122. time.Sleep(time.Second / time.Duration(watchPutRate))
  123. }
  124. close(putreqc)
  125. }()
  126. wg.Wait()
  127. bar.Finish()
  128. fmt.Printf("Watch events received summary:\n")
  129. close(results)
  130. <-pdoneC
  131. }
  132. func doWatch(stream etcdserverpb.Watch_WatchClient, requests <-chan etcdserverpb.WatchRequest) {
  133. for r := range requests {
  134. st := time.Now()
  135. err := stream.Send(&r)
  136. var errStr string
  137. if err != nil {
  138. errStr = err.Error()
  139. }
  140. results <- result{errStr: errStr, duration: time.Since(st)}
  141. bar.Increment()
  142. }
  143. for {
  144. _, err := stream.Recv()
  145. var errStr string
  146. if err != nil {
  147. errStr = err.Error()
  148. }
  149. results <- result{errStr: errStr}
  150. bar.Increment()
  151. }
  152. wg.Done()
  153. }