watch.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. "time"
  19. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/cheggaaa/pb"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  23. )
  24. // watchCmd represents the watch command
  25. var watchCmd = &cobra.Command{
  26. Use: "watch",
  27. Short: "Benchmark watch",
  28. Long: `Benchmark watch tests the performance of processing watch requests and
  29. sending events to watchers. It tests the sending performance by
  30. changing the value of the watched keys with concurrent put
  31. requests.
  32. During the test, each watcher watches (--total/--watchers) keys
  33. (a watcher might watch on the same key multiple times if
  34. --watched-key-total is small).
  35. Each key is watched by (--total/--watched-key-total) watchers.
  36. `,
  37. Run: watchFunc,
  38. }
  39. var (
  40. watchTotalStreams int
  41. watchTotal int
  42. watchedKeyTotal int
  43. watchPutRate int
  44. watchPutTotal int
  45. )
  46. func init() {
  47. RootCmd.AddCommand(watchCmd)
  48. watchCmd.Flags().IntVar(&watchTotalStreams, "watchers", 10000, "Total number of watchers")
  49. watchCmd.Flags().IntVar(&watchTotal, "total", 100000, "Total number of watch requests")
  50. watchCmd.Flags().IntVar(&watchedKeyTotal, "watched-key-total", 10000, "Total number of keys to be watched")
  51. watchCmd.Flags().IntVar(&watchPutRate, "put-rate", 100, "Number of keys to put per second")
  52. watchCmd.Flags().IntVar(&watchPutTotal, "put-total", 10000, "Number of put requests")
  53. }
  54. func watchFunc(cmd *cobra.Command, args []string) {
  55. watched := make([][]byte, watchedKeyTotal)
  56. for i := range watched {
  57. watched[i] = mustRandBytes(32)
  58. }
  59. requests := make(chan etcdserverpb.WatchRequest, totalClients)
  60. clients := mustCreateClients(totalClients, totalConns)
  61. streams := make([]etcdserverpb.Watch_WatchClient, watchTotalStreams)
  62. var err error
  63. for i := range streams {
  64. streams[i], err = clients[i%len(clients)].Watch.Watch(context.TODO())
  65. if err != nil {
  66. fmt.Fprintln(os.Stderr, "Failed to create watch stream:", err)
  67. os.Exit(1)
  68. }
  69. }
  70. for i := range streams {
  71. wg.Add(1)
  72. go doWatch(streams[i], requests)
  73. }
  74. // watching phase
  75. results = make(chan result)
  76. bar = pb.New(watchTotal)
  77. bar.Format("Bom !")
  78. bar.Start()
  79. pdoneC := printRate(results)
  80. go func() {
  81. for i := 0; i < watchTotal; i++ {
  82. requests <- etcdserverpb.WatchRequest{
  83. RequestUnion: &etcdserverpb.WatchRequest_CreateRequest{
  84. CreateRequest: &etcdserverpb.WatchCreateRequest{
  85. Key: watched[i%(len(watched))]}}}
  86. }
  87. close(requests)
  88. }()
  89. wg.Wait()
  90. bar.Finish()
  91. fmt.Printf("Watch creation summary:\n")
  92. close(results)
  93. <-pdoneC
  94. // put phase
  95. // total number of puts * number of watchers on each key
  96. eventsTotal := watchPutTotal * (watchTotal / watchedKeyTotal)
  97. results = make(chan result)
  98. bar = pb.New(eventsTotal)
  99. bar.Format("Bom !")
  100. bar.Start()
  101. putreqc := make(chan etcdserverpb.PutRequest)
  102. for i := 0; i < watchPutTotal; i++ {
  103. wg.Add(1)
  104. go doPut(context.TODO(), clients[i%len(clients)].KV, putreqc)
  105. }
  106. pdoneC = printRate(results)
  107. go func() {
  108. for i := 0; i < eventsTotal; i++ {
  109. putreqc <- etcdserverpb.PutRequest{
  110. Key: watched[i%(len(watched))],
  111. Value: []byte("data"),
  112. }
  113. // TODO: use a real rate-limiter instead of sleep.
  114. time.Sleep(time.Second / time.Duration(watchPutRate))
  115. }
  116. close(putreqc)
  117. }()
  118. wg.Wait()
  119. bar.Finish()
  120. fmt.Printf("Watch events received summary:\n")
  121. close(results)
  122. <-pdoneC
  123. }
  124. func doWatch(stream etcdserverpb.Watch_WatchClient, requests <-chan etcdserverpb.WatchRequest) {
  125. for r := range requests {
  126. st := time.Now()
  127. err := stream.Send(&r)
  128. var errStr string
  129. if err != nil {
  130. errStr = err.Error()
  131. }
  132. results <- result{errStr: errStr, duration: time.Since(st)}
  133. bar.Increment()
  134. }
  135. for {
  136. _, err := stream.Recv()
  137. var errStr string
  138. if err != nil {
  139. errStr = err.Error()
  140. }
  141. results <- result{errStr: errStr}
  142. bar.Increment()
  143. }
  144. wg.Done()
  145. }