range.go 2.4 KB

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