watch.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. v3 "github.com/coreos/etcd/clientv3"
  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([]string, watchedKeyTotal)
  63. for i := range watched {
  64. watched[i] = string(mustRandBytes(32))
  65. }
  66. requests := make(chan string, totalClients)
  67. clients := mustCreateClients(totalClients, totalConns)
  68. streams := make([]v3.Watcher, watchTotalStreams)
  69. for i := range streams {
  70. streams[i] = v3.NewWatcher(clients[i%len(clients)])
  71. }
  72. putStartNotifier = make(chan struct{})
  73. // watching phase
  74. results = make(chan result)
  75. bar = pb.New(watchTotal)
  76. bar.Format("Bom !")
  77. bar.Start()
  78. pdoneC := printRate(results)
  79. atomic.StoreInt32(&nrWatchCompleted, int32(0))
  80. watchCompletedNotifier = make(chan struct{})
  81. for i := range streams {
  82. go doWatch(streams[i], requests)
  83. }
  84. go func() {
  85. for i := 0; i < watchTotal; i++ {
  86. requests <- watched[i%len(watched)]
  87. }
  88. close(requests)
  89. }()
  90. <-watchCompletedNotifier
  91. bar.Finish()
  92. fmt.Printf("Watch creation summary:\n")
  93. close(results)
  94. <-pdoneC
  95. // put phase
  96. // total number of puts * number of watchers on each key
  97. eventsTotal = watchPutTotal * (watchTotal / watchedKeyTotal)
  98. results = make(chan result)
  99. bar = pb.New(eventsTotal)
  100. bar.Format("Bom !")
  101. bar.Start()
  102. atomic.StoreInt32(&nrRecvCompleted, 0)
  103. recvCompletedNotifier = make(chan struct{})
  104. close(putStartNotifier)
  105. putreqc := make(chan v3.Op)
  106. for i := 0; i < watchPutTotal; i++ {
  107. go doPutForWatch(context.TODO(), clients[i%len(clients)].KV, putreqc)
  108. }
  109. pdoneC = printRate(results)
  110. go func() {
  111. for i := 0; i < eventsTotal; i++ {
  112. putreqc <- v3.OpPut(watched[i%(len(watched))], "data")
  113. // TODO: use a real rate-limiter instead of sleep.
  114. time.Sleep(time.Second / time.Duration(watchPutRate))
  115. }
  116. close(putreqc)
  117. }()
  118. <-recvCompletedNotifier
  119. bar.Finish()
  120. fmt.Printf("Watch events received summary:\n")
  121. close(results)
  122. <-pdoneC
  123. }
  124. func doWatch(stream v3.Watcher, requests <-chan string) {
  125. for r := range requests {
  126. st := time.Now()
  127. wch := stream.Watch(context.TODO(), r)
  128. var errStr string
  129. if wch == nil {
  130. errStr = "could not open watch channel"
  131. }
  132. results <- result{errStr: errStr, duration: time.Since(st)}
  133. bar.Increment()
  134. go recvWatchChan(wch)
  135. }
  136. atomic.AddInt32(&nrWatchCompleted, 1)
  137. if atomic.LoadInt32(&nrWatchCompleted) == int32(watchTotalStreams) {
  138. watchCompletedNotifier <- struct{}{}
  139. }
  140. <-putStartNotifier
  141. }
  142. func recvWatchChan(wch v3.WatchChan) {
  143. for range wch {
  144. st := time.Now()
  145. results <- result{duration: time.Since(st)}
  146. bar.Increment()
  147. atomic.AddInt32(&nrRecvCompleted, 1)
  148. if atomic.LoadInt32(&nrRecvCompleted) == int32(eventsTotal) {
  149. recvCompletedNotifier <- struct{}{}
  150. }
  151. }
  152. }
  153. func doPutForWatch(ctx context.Context, client v3.KV, requests <-chan v3.Op) {
  154. for op := range requests {
  155. _, err := client.Do(ctx, op)
  156. if err != nil {
  157. fmt.Fprintf(os.Stderr, "failed to Put for watch benchmark: %v\n", err)
  158. os.Exit(1)
  159. }
  160. }
  161. }