watch.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2015 The etcd Authors
  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. "encoding/binary"
  17. "fmt"
  18. "math/rand"
  19. "os"
  20. "sync/atomic"
  21. "time"
  22. v3 "github.com/coreos/etcd/clientv3"
  23. "github.com/coreos/etcd/pkg/report"
  24. "github.com/spf13/cobra"
  25. "golang.org/x/net/context"
  26. "gopkg.in/cheggaaa/pb.v1"
  27. )
  28. // watchCmd represents the watch command
  29. var watchCmd = &cobra.Command{
  30. Use: "watch",
  31. Short: "Benchmark watch",
  32. Long: `Benchmark watch tests the performance of processing watch requests and
  33. sending events to watchers. It tests the sending performance by
  34. changing the value of the watched keys with concurrent put
  35. requests.
  36. During the test, each watcher watches (--total/--watchers) keys
  37. (a watcher might watch on the same key multiple times if
  38. --watched-key-total is small).
  39. Each key is watched by (--total/--watched-key-total) watchers.
  40. `,
  41. Run: watchFunc,
  42. }
  43. var (
  44. watchTotalStreams int
  45. watchTotal int
  46. watchedKeyTotal int
  47. watchPutRate int
  48. watchPutTotal int
  49. watchKeySize int
  50. watchKeySpaceSize int
  51. watchSeqKeys bool
  52. eventsTotal int
  53. nrWatchCompleted int32
  54. nrRecvCompleted int32
  55. watchCompletedNotifier chan struct{}
  56. recvCompletedNotifier chan struct{}
  57. )
  58. func init() {
  59. RootCmd.AddCommand(watchCmd)
  60. watchCmd.Flags().IntVar(&watchTotalStreams, "watchers", 10000, "Total number of watchers")
  61. watchCmd.Flags().IntVar(&watchTotal, "total", 100000, "Total number of watch requests")
  62. watchCmd.Flags().IntVar(&watchedKeyTotal, "watched-key-total", 10000, "Total number of keys to be watched")
  63. watchCmd.Flags().IntVar(&watchPutRate, "put-rate", 100, "Number of keys to put per second")
  64. watchCmd.Flags().IntVar(&watchPutTotal, "put-total", 10000, "Number of put requests")
  65. watchCmd.Flags().IntVar(&watchKeySize, "key-size", 32, "Key size of watch request")
  66. watchCmd.Flags().IntVar(&watchKeySpaceSize, "key-space-size", 1, "Maximum possible keys")
  67. watchCmd.Flags().BoolVar(&watchSeqKeys, "sequential-keys", false, "Use sequential keys")
  68. }
  69. func watchFunc(cmd *cobra.Command, args []string) {
  70. if watchKeySpaceSize <= 0 {
  71. fmt.Fprintf(os.Stderr, "expected positive --key-space-size, got (%v)", watchKeySpaceSize)
  72. os.Exit(1)
  73. }
  74. watched := make([]string, watchedKeyTotal)
  75. numWatchers := make(map[string]int)
  76. for i := range watched {
  77. k := make([]byte, watchKeySize)
  78. if watchSeqKeys {
  79. binary.PutVarint(k, int64(i%watchKeySpaceSize))
  80. } else {
  81. binary.PutVarint(k, int64(rand.Intn(watchKeySpaceSize)))
  82. }
  83. watched[i] = string(k)
  84. }
  85. requests := make(chan string, totalClients)
  86. clients := mustCreateClients(totalClients, totalConns)
  87. streams := make([]v3.Watcher, watchTotalStreams)
  88. for i := range streams {
  89. streams[i] = v3.NewWatcher(clients[i%len(clients)])
  90. }
  91. // watching phase
  92. bar = pb.New(watchTotal)
  93. bar.Format("Bom !")
  94. bar.Start()
  95. atomic.StoreInt32(&nrWatchCompleted, int32(0))
  96. watchCompletedNotifier = make(chan struct{})
  97. r := report.NewReportRate("%4.4f")
  98. for i := range streams {
  99. go doWatch(streams[i], requests, r.Results())
  100. }
  101. go func() {
  102. for i := 0; i < watchTotal; i++ {
  103. key := watched[i%len(watched)]
  104. requests <- key
  105. numWatchers[key]++
  106. }
  107. close(requests)
  108. }()
  109. rc := r.Run()
  110. <-watchCompletedNotifier
  111. bar.Finish()
  112. close(r.Results())
  113. fmt.Printf("Watch creation summary:\n%s", <-rc)
  114. // put phase
  115. eventsTotal = 0
  116. for i := 0; i < watchPutTotal; i++ {
  117. eventsTotal += numWatchers[watched[i%len(watched)]]
  118. }
  119. bar = pb.New(eventsTotal)
  120. bar.Format("Bom !")
  121. bar.Start()
  122. atomic.StoreInt32(&nrRecvCompleted, 0)
  123. recvCompletedNotifier = make(chan struct{})
  124. putreqc := make(chan v3.Op)
  125. r = report.NewReportRate("%4.4f")
  126. for i := 0; i < watchPutTotal; i++ {
  127. go func(c *v3.Client) {
  128. for op := range putreqc {
  129. if _, err := c.Do(context.TODO(), op); err != nil {
  130. fmt.Fprintf(os.Stderr, "failed to Put for watch benchmark: %v\n", err)
  131. os.Exit(1)
  132. }
  133. }
  134. }(clients[i%len(clients)])
  135. }
  136. go func() {
  137. for i := 0; i < watchPutTotal; i++ {
  138. putreqc <- v3.OpPut(watched[i%(len(watched))], "data")
  139. // TODO: use a real rate-limiter instead of sleep.
  140. time.Sleep(time.Second / time.Duration(watchPutRate))
  141. }
  142. close(putreqc)
  143. }()
  144. rc = r.Run()
  145. <-recvCompletedNotifier
  146. bar.Finish()
  147. close(r.Results())
  148. fmt.Printf("Watch events received summary:\n%s", <-rc)
  149. }
  150. func doWatch(stream v3.Watcher, requests <-chan string, results chan<- report.Result) {
  151. for r := range requests {
  152. st := time.Now()
  153. wch := stream.Watch(context.TODO(), r)
  154. results <- report.Result{Start: st, End: time.Now()}
  155. bar.Increment()
  156. go recvWatchChan(wch, results)
  157. }
  158. atomic.AddInt32(&nrWatchCompleted, 1)
  159. if atomic.LoadInt32(&nrWatchCompleted) == int32(watchTotalStreams) {
  160. watchCompletedNotifier <- struct{}{}
  161. }
  162. }
  163. func recvWatchChan(wch v3.WatchChan, results chan<- report.Result) {
  164. for r := range wch {
  165. st := time.Now()
  166. for range r.Events {
  167. results <- report.Result{Start: st, End: time.Now()}
  168. bar.Increment()
  169. atomic.AddInt32(&nrRecvCompleted, 1)
  170. }
  171. if atomic.LoadInt32(&nrRecvCompleted) == int32(eventsTotal) {
  172. recvCompletedNotifier <- struct{}{}
  173. break
  174. }
  175. }
  176. }