watch_get.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2016 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. "fmt"
  17. "sync"
  18. "time"
  19. v3 "github.com/coreos/etcd/clientv3"
  20. "github.com/spf13/cobra"
  21. "golang.org/x/net/context"
  22. "gopkg.in/cheggaaa/pb.v1"
  23. )
  24. // watchGetCmd represents the watch command
  25. var watchGetCmd = &cobra.Command{
  26. Use: "watch-get",
  27. Short: "Benchmark watch with get",
  28. Long: `Benchmark for serialized key gets with many unsynced watchers`,
  29. Run: watchGetFunc,
  30. }
  31. var (
  32. watchGetTotalWatchers int
  33. watchGetTotalStreams int
  34. watchEvents int
  35. firstWatch sync.Once
  36. )
  37. func init() {
  38. RootCmd.AddCommand(watchGetCmd)
  39. watchGetCmd.Flags().IntVar(&watchGetTotalWatchers, "watchers", 10000, "Total number of watchers")
  40. watchGetCmd.Flags().IntVar(&watchGetTotalStreams, "streams", 1, "Total number of watcher streams")
  41. watchGetCmd.Flags().IntVar(&watchEvents, "events", 8, "Number of events per watcher")
  42. }
  43. func watchGetFunc(cmd *cobra.Command, args []string) {
  44. clients := mustCreateClients(totalClients, totalConns)
  45. getClient := mustCreateClients(1, 1)
  46. // setup keys for watchers
  47. watchRev := int64(0)
  48. for i := 0; i < watchEvents; i++ {
  49. v := fmt.Sprintf("%d", i)
  50. resp, err := clients[0].Put(context.TODO(), "watchkey", v)
  51. if err != nil {
  52. panic(err)
  53. }
  54. if i == 0 {
  55. watchRev = resp.Header.Revision
  56. }
  57. }
  58. streams := make([]v3.Watcher, watchGetTotalStreams)
  59. for i := range streams {
  60. streams[i] = v3.NewWatcher(clients[i%len(clients)])
  61. }
  62. // results from trying to do serialized gets with concurrent watchers
  63. results = make(chan result)
  64. bar = pb.New(watchGetTotalWatchers * watchEvents)
  65. bar.Format("Bom !")
  66. bar.Start()
  67. pdoneC := printReport(results)
  68. wg.Add(watchGetTotalWatchers)
  69. ctx, cancel := context.WithCancel(context.TODO())
  70. f := func() {
  71. doSerializedGet(ctx, getClient[0], results)
  72. }
  73. for i := 0; i < watchGetTotalWatchers; i++ {
  74. go doUnsyncWatch(streams[i%len(streams)], watchRev, f)
  75. }
  76. wg.Wait()
  77. cancel()
  78. bar.Finish()
  79. fmt.Printf("Get during watch summary:\n")
  80. <-pdoneC
  81. }
  82. func doSerializedGet(ctx context.Context, client *v3.Client, results chan result) {
  83. for {
  84. st := time.Now()
  85. _, err := client.Get(ctx, "abc", v3.WithSerializable())
  86. if ctx.Err() != nil {
  87. break
  88. }
  89. var errStr string
  90. if err != nil {
  91. errStr = err.Error()
  92. }
  93. res := result{errStr: errStr, duration: time.Since(st), happened: time.Now()}
  94. results <- res
  95. }
  96. close(results)
  97. }
  98. func doUnsyncWatch(stream v3.Watcher, rev int64, f func()) {
  99. wch := stream.Watch(context.TODO(), "watchkey", v3.WithRev(rev))
  100. if wch == nil {
  101. panic("could not open watch channel")
  102. }
  103. firstWatch.Do(func() { go f() })
  104. i := 0
  105. for i < watchEvents {
  106. wev := <-wch
  107. i += len(wev.Events)
  108. bar.Add(len(wev.Events))
  109. }
  110. wg.Done()
  111. }