watch_latency.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "context"
  17. "fmt"
  18. "os"
  19. "sync"
  20. "time"
  21. "go.etcd.io/etcd/clientv3"
  22. "go.etcd.io/etcd/pkg/report"
  23. "github.com/spf13/cobra"
  24. "golang.org/x/time/rate"
  25. "gopkg.in/cheggaaa/pb.v1"
  26. )
  27. // watchLatencyCmd represents the watch latency command
  28. var watchLatencyCmd = &cobra.Command{
  29. Use: "watch-latency",
  30. Short: "Benchmark watch latency",
  31. Long: `Benchmarks the latency for watches by measuring
  32. the latency between writing to a key and receiving the
  33. associated watch response.`,
  34. Run: watchLatencyFunc,
  35. }
  36. var (
  37. watchLTotal int
  38. watchLPutRate int
  39. watchLKeySize int
  40. watchLValueSize int
  41. )
  42. func init() {
  43. RootCmd.AddCommand(watchLatencyCmd)
  44. watchLatencyCmd.Flags().IntVar(&watchLTotal, "total", 10000, "Total number of put requests")
  45. watchLatencyCmd.Flags().IntVar(&watchLPutRate, "put-rate", 100, "Number of keys to put per second")
  46. watchLatencyCmd.Flags().IntVar(&watchLKeySize, "key-size", 32, "Key size of watch response")
  47. watchLatencyCmd.Flags().IntVar(&watchLValueSize, "val-size", 32, "Value size of watch response")
  48. }
  49. func watchLatencyFunc(cmd *cobra.Command, args []string) {
  50. key := string(mustRandBytes(watchLKeySize))
  51. value := string(mustRandBytes(watchLValueSize))
  52. clients := mustCreateClients(totalClients, totalConns)
  53. putClient := mustCreateConn()
  54. wchs := make([]clientv3.WatchChan, len(clients))
  55. for i := range wchs {
  56. wchs[i] = clients[i].Watch(context.TODO(), key)
  57. }
  58. bar = pb.New(watchLTotal)
  59. bar.Format("Bom !")
  60. bar.Start()
  61. limiter := rate.NewLimiter(rate.Limit(watchLPutRate), watchLPutRate)
  62. r := newReport()
  63. rc := r.Run()
  64. for i := 0; i < watchLTotal; i++ {
  65. // limit key put as per reqRate
  66. if err := limiter.Wait(context.TODO()); err != nil {
  67. break
  68. }
  69. var st time.Time
  70. var wg sync.WaitGroup
  71. wg.Add(len(clients))
  72. barrierc := make(chan struct{})
  73. for _, wch := range wchs {
  74. ch := wch
  75. go func() {
  76. <-barrierc
  77. <-ch
  78. r.Results() <- report.Result{Start: st, End: time.Now()}
  79. wg.Done()
  80. }()
  81. }
  82. if _, err := putClient.Put(context.TODO(), key, value); err != nil {
  83. fmt.Fprintf(os.Stderr, "Failed to Put for watch latency benchmark: %v\n", err)
  84. os.Exit(1)
  85. }
  86. st = time.Now()
  87. close(barrierc)
  88. wg.Wait()
  89. bar.Increment()
  90. }
  91. close(r.Results())
  92. bar.Finish()
  93. fmt.Printf("%s", <-rc)
  94. }