watch.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "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/Godeps/_workspace/src/github.com/cheggaaa/pb"
  24. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  25. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  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. putStartNotifier 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. 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. putStartNotifier = make(chan struct{})
  91. // watching phase
  92. results = make(chan result)
  93. bar = pb.New(watchTotal)
  94. bar.Format("Bom !")
  95. bar.Start()
  96. pdoneC := printRate(results)
  97. atomic.StoreInt32(&nrWatchCompleted, int32(0))
  98. watchCompletedNotifier = make(chan struct{})
  99. for i := range streams {
  100. go doWatch(streams[i], requests)
  101. }
  102. go func() {
  103. for i := 0; i < watchTotal; i++ {
  104. requests <- watched[i%len(watched)]
  105. }
  106. close(requests)
  107. }()
  108. <-watchCompletedNotifier
  109. bar.Finish()
  110. fmt.Printf("Watch creation summary:\n")
  111. close(results)
  112. <-pdoneC
  113. // put phase
  114. // total number of puts * number of watchers on each key
  115. eventsTotal = watchPutTotal * (watchTotal / watchedKeyTotal)
  116. results = make(chan result)
  117. bar = pb.New(eventsTotal)
  118. bar.Format("Bom !")
  119. bar.Start()
  120. atomic.StoreInt32(&nrRecvCompleted, 0)
  121. recvCompletedNotifier = make(chan struct{})
  122. close(putStartNotifier)
  123. putreqc := make(chan v3.Op)
  124. for i := 0; i < watchPutTotal; i++ {
  125. go doPutForWatch(context.TODO(), clients[i%len(clients)].KV, putreqc)
  126. }
  127. pdoneC = printRate(results)
  128. go func() {
  129. for i := 0; i < eventsTotal; i++ {
  130. putreqc <- v3.OpPut(watched[i%(len(watched))], "data")
  131. // TODO: use a real rate-limiter instead of sleep.
  132. time.Sleep(time.Second / time.Duration(watchPutRate))
  133. }
  134. close(putreqc)
  135. }()
  136. <-recvCompletedNotifier
  137. bar.Finish()
  138. fmt.Printf("Watch events received summary:\n")
  139. close(results)
  140. <-pdoneC
  141. }
  142. func doWatch(stream v3.Watcher, requests <-chan string) {
  143. for r := range requests {
  144. st := time.Now()
  145. wch := stream.Watch(context.TODO(), r)
  146. var errStr string
  147. if wch == nil {
  148. errStr = "could not open watch channel"
  149. }
  150. results <- result{errStr: errStr, duration: time.Since(st), happened: time.Now()}
  151. bar.Increment()
  152. go recvWatchChan(wch)
  153. }
  154. atomic.AddInt32(&nrWatchCompleted, 1)
  155. if atomic.LoadInt32(&nrWatchCompleted) == int32(watchTotalStreams) {
  156. watchCompletedNotifier <- struct{}{}
  157. }
  158. <-putStartNotifier
  159. }
  160. func recvWatchChan(wch v3.WatchChan) {
  161. for range wch {
  162. if atomic.LoadInt32(&nrRecvCompleted) == int32(eventsTotal) {
  163. recvCompletedNotifier <- struct{}{}
  164. break
  165. }
  166. st := time.Now()
  167. results <- result{duration: time.Since(st), happened: time.Now()}
  168. bar.Increment()
  169. atomic.AddInt32(&nrRecvCompleted, 1)
  170. }
  171. }
  172. func doPutForWatch(ctx context.Context, client v3.KV, requests <-chan v3.Op) {
  173. for op := range requests {
  174. _, err := client.Do(ctx, op)
  175. if err != nil {
  176. fmt.Fprintf(os.Stderr, "failed to Put for watch benchmark: %v\n", err)
  177. os.Exit(1)
  178. }
  179. }
  180. }