watch.go 5.6 KB

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