watch_latency.go 2.5 KB

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