watch_get.go 3.0 KB

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