range.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/spf13/cobra"
  21. "golang.org/x/net/context"
  22. "gopkg.in/cheggaaa/pb.v1"
  23. )
  24. // rangeCmd represents the range command
  25. var rangeCmd = &cobra.Command{
  26. Use: "range key [end-range]",
  27. Short: "Benchmark range",
  28. Run: rangeFunc,
  29. }
  30. var (
  31. rangeTotal int
  32. rangeConsistency string
  33. )
  34. func init() {
  35. RootCmd.AddCommand(rangeCmd)
  36. rangeCmd.Flags().IntVar(&rangeTotal, "total", 10000, "Total number of range requests")
  37. rangeCmd.Flags().StringVar(&rangeConsistency, "consistency", "l", "Linearizable(l) or Serializable(s)")
  38. }
  39. func rangeFunc(cmd *cobra.Command, args []string) {
  40. if len(args) == 0 || len(args) > 2 {
  41. fmt.Fprintln(os.Stderr, cmd.Usage())
  42. os.Exit(1)
  43. }
  44. k := args[0]
  45. end := ""
  46. if len(args) == 2 {
  47. end = args[1]
  48. }
  49. if rangeConsistency == "l" {
  50. fmt.Println("bench with linearizable range")
  51. } else if rangeConsistency == "s" {
  52. fmt.Println("bench with serializable range")
  53. } else {
  54. fmt.Fprintln(os.Stderr, cmd.Usage())
  55. os.Exit(1)
  56. }
  57. results = make(chan result)
  58. requests := make(chan v3.Op, totalClients)
  59. bar = pb.New(rangeTotal)
  60. clients := mustCreateClients(totalClients, totalConns)
  61. bar.Format("Bom !")
  62. bar.Start()
  63. for i := range clients {
  64. wg.Add(1)
  65. go doRange(clients[i].KV, requests)
  66. }
  67. pdoneC := printReport(results)
  68. go func() {
  69. for i := 0; i < rangeTotal; i++ {
  70. opts := []v3.OpOption{v3.WithRange(end)}
  71. if rangeConsistency == "s" {
  72. opts = append(opts, v3.WithSerializable())
  73. }
  74. op := v3.OpGet(k, opts...)
  75. requests <- op
  76. }
  77. close(requests)
  78. }()
  79. wg.Wait()
  80. bar.Finish()
  81. close(results)
  82. <-pdoneC
  83. }
  84. func doRange(client v3.KV, requests <-chan v3.Op) {
  85. defer wg.Done()
  86. for op := range requests {
  87. st := time.Now()
  88. _, err := client.Do(context.Background(), op)
  89. var errStr string
  90. if err != nil {
  91. errStr = err.Error()
  92. }
  93. results <- result{errStr: errStr, duration: time.Since(st), happened: time.Now()}
  94. bar.Increment()
  95. }
  96. }