range.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "math"
  19. "os"
  20. "time"
  21. v3 "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. // rangeCmd represents the range command
  28. var rangeCmd = &cobra.Command{
  29. Use: "range key [end-range]",
  30. Short: "Benchmark range",
  31. Run: rangeFunc,
  32. }
  33. var (
  34. rangeRate int
  35. rangeTotal int
  36. rangeConsistency string
  37. )
  38. func init() {
  39. RootCmd.AddCommand(rangeCmd)
  40. rangeCmd.Flags().IntVar(&rangeRate, "rate", 0, "Maximum range requests per second (0 is no limit)")
  41. rangeCmd.Flags().IntVar(&rangeTotal, "total", 10000, "Total number of range requests")
  42. rangeCmd.Flags().StringVar(&rangeConsistency, "consistency", "l", "Linearizable(l) or Serializable(s)")
  43. }
  44. func rangeFunc(cmd *cobra.Command, args []string) {
  45. if len(args) == 0 || len(args) > 2 {
  46. fmt.Fprintln(os.Stderr, cmd.Usage())
  47. os.Exit(1)
  48. }
  49. k := args[0]
  50. end := ""
  51. if len(args) == 2 {
  52. end = args[1]
  53. }
  54. if rangeConsistency == "l" {
  55. fmt.Println("bench with linearizable range")
  56. } else if rangeConsistency == "s" {
  57. fmt.Println("bench with serializable range")
  58. } else {
  59. fmt.Fprintln(os.Stderr, cmd.Usage())
  60. os.Exit(1)
  61. }
  62. if rangeRate == 0 {
  63. rangeRate = math.MaxInt32
  64. }
  65. limit := rate.NewLimiter(rate.Limit(rangeRate), 1)
  66. requests := make(chan v3.Op, totalClients)
  67. clients := mustCreateClients(totalClients, totalConns)
  68. bar = pb.New(rangeTotal)
  69. bar.Format("Bom !")
  70. bar.Start()
  71. r := newReport()
  72. for i := range clients {
  73. wg.Add(1)
  74. go func(c *v3.Client) {
  75. defer wg.Done()
  76. for op := range requests {
  77. limit.Wait(context.Background())
  78. st := time.Now()
  79. _, err := c.Do(context.Background(), op)
  80. r.Results() <- report.Result{Err: err, Start: st, End: time.Now()}
  81. bar.Increment()
  82. }
  83. }(clients[i])
  84. }
  85. go func() {
  86. for i := 0; i < rangeTotal; i++ {
  87. opts := []v3.OpOption{v3.WithRange(end)}
  88. if rangeConsistency == "s" {
  89. opts = append(opts, v3.WithSerializable())
  90. }
  91. op := v3.OpGet(k, opts...)
  92. requests <- op
  93. }
  94. close(requests)
  95. }()
  96. rc := r.Run()
  97. wg.Wait()
  98. close(r.Results())
  99. bar.Finish()
  100. fmt.Printf("%s", <-rc)
  101. }