watch.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. "sync/atomic"
  19. "time"
  20. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/cheggaaa/pb"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  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. eventsTotal int
  47. nrWatchCompleted int32
  48. nrRecvCompleted int32
  49. watchCompletedNotifier chan struct{}
  50. putStartNotifier chan struct{}
  51. recvCompletedNotifier chan struct{}
  52. )
  53. func init() {
  54. RootCmd.AddCommand(watchCmd)
  55. watchCmd.Flags().IntVar(&watchTotalStreams, "watchers", 10000, "Total number of watchers")
  56. watchCmd.Flags().IntVar(&watchTotal, "total", 100000, "Total number of watch requests")
  57. watchCmd.Flags().IntVar(&watchedKeyTotal, "watched-key-total", 10000, "Total number of keys to be watched")
  58. watchCmd.Flags().IntVar(&watchPutRate, "put-rate", 100, "Number of keys to put per second")
  59. watchCmd.Flags().IntVar(&watchPutTotal, "put-total", 10000, "Number of put requests")
  60. }
  61. func watchFunc(cmd *cobra.Command, args []string) {
  62. watched := make([][]byte, watchedKeyTotal)
  63. for i := range watched {
  64. watched[i] = mustRandBytes(32)
  65. }
  66. requests := make(chan etcdserverpb.WatchRequest, totalClients)
  67. clients := mustCreateClients(totalClients, totalConns)
  68. streams := make([]etcdserverpb.Watch_WatchClient, watchTotalStreams)
  69. var err error
  70. for i := range streams {
  71. streams[i], err = clients[i%len(clients)].Watch.Watch(context.TODO())
  72. if err != nil {
  73. fmt.Fprintln(os.Stderr, "Failed to create watch stream:", err)
  74. os.Exit(1)
  75. }
  76. }
  77. putStartNotifier = make(chan struct{})
  78. // watching phase
  79. results = make(chan result)
  80. bar = pb.New(watchTotal)
  81. bar.Format("Bom !")
  82. bar.Start()
  83. pdoneC := printRate(results)
  84. atomic.StoreInt32(&nrWatchCompleted, int32(0))
  85. watchCompletedNotifier = make(chan struct{})
  86. for i := range streams {
  87. go doWatch(streams[i], requests)
  88. }
  89. go func() {
  90. for i := 0; i < watchTotal; i++ {
  91. requests <- etcdserverpb.WatchRequest{
  92. RequestUnion: &etcdserverpb.WatchRequest_CreateRequest{
  93. CreateRequest: &etcdserverpb.WatchCreateRequest{
  94. Key: watched[i%(len(watched))]}}}
  95. }
  96. close(requests)
  97. }()
  98. <-watchCompletedNotifier
  99. bar.Finish()
  100. fmt.Printf("Watch creation summary:\n")
  101. close(results)
  102. <-pdoneC
  103. // put phase
  104. // total number of puts * number of watchers on each key
  105. eventsTotal = watchPutTotal * (watchTotal / watchedKeyTotal)
  106. results = make(chan result)
  107. bar = pb.New(eventsTotal)
  108. bar.Format("Bom !")
  109. bar.Start()
  110. atomic.StoreInt32(&nrRecvCompleted, 0)
  111. recvCompletedNotifier = make(chan struct{})
  112. close(putStartNotifier)
  113. putreqc := make(chan etcdserverpb.PutRequest)
  114. for i := 0; i < watchPutTotal; i++ {
  115. go doPutForWatch(context.TODO(), clients[i%len(clients)].KV, putreqc)
  116. }
  117. pdoneC = printRate(results)
  118. go func() {
  119. for i := 0; i < eventsTotal; i++ {
  120. putreqc <- etcdserverpb.PutRequest{
  121. Key: watched[i%(len(watched))],
  122. Value: []byte("data"),
  123. }
  124. // TODO: use a real rate-limiter instead of sleep.
  125. time.Sleep(time.Second / time.Duration(watchPutRate))
  126. }
  127. close(putreqc)
  128. }()
  129. <-recvCompletedNotifier
  130. bar.Finish()
  131. fmt.Printf("Watch events received summary:\n")
  132. close(results)
  133. <-pdoneC
  134. }
  135. func doWatch(stream etcdserverpb.Watch_WatchClient, requests <-chan etcdserverpb.WatchRequest) {
  136. for r := range requests {
  137. st := time.Now()
  138. err := stream.Send(&r)
  139. var errStr string
  140. if err != nil {
  141. errStr = err.Error()
  142. }
  143. results <- result{errStr: errStr, duration: time.Since(st)}
  144. bar.Increment()
  145. }
  146. atomic.AddInt32(&nrWatchCompleted, 1)
  147. if atomic.LoadInt32(&nrWatchCompleted) == int32(watchTotalStreams) {
  148. watchCompletedNotifier <- struct{}{}
  149. }
  150. <-putStartNotifier
  151. for {
  152. st := time.Now()
  153. _, err := stream.Recv()
  154. var errStr string
  155. if err != nil {
  156. errStr = err.Error()
  157. }
  158. results <- result{errStr: errStr, duration: time.Since(st)}
  159. bar.Increment()
  160. atomic.AddInt32(&nrRecvCompleted, 1)
  161. if atomic.LoadInt32(&nrRecvCompleted) == int32(eventsTotal) {
  162. recvCompletedNotifier <- struct{}{}
  163. }
  164. }
  165. }
  166. func doPutForWatch(ctx context.Context, client etcdserverpb.KVClient, requests <-chan etcdserverpb.PutRequest) {
  167. for r := range requests {
  168. _, err := client.Put(ctx, &r)
  169. if err != nil {
  170. fmt.Fprintln(os.Stderr, "failed to Put for watch benchmark: %s", err)
  171. os.Exit(1)
  172. }
  173. }
  174. }