range.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2015 CoreOS, Inc.
  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. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/cheggaaa/pb"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  22. v3 "github.com/coreos/etcd/clientv3"
  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. rangeCmd.Flags().BoolVar(&sample, "sample", false, "'true' to sample requests for every second")
  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. results = make(chan result)
  59. requests := make(chan v3.Op, totalClients)
  60. bar = pb.New(rangeTotal)
  61. clients := mustCreateClients(totalClients, totalConns)
  62. bar.Format("Bom !")
  63. bar.Start()
  64. for i := range clients {
  65. wg.Add(1)
  66. go doRange(clients[i].KV, requests)
  67. }
  68. pdoneC := printReport(results)
  69. go func() {
  70. for i := 0; i < rangeTotal; i++ {
  71. opts := []v3.OpOption{v3.WithRange(end)}
  72. if rangeConsistency == "s" {
  73. opts = append(opts, v3.WithSerializable())
  74. }
  75. op := v3.OpGet(k, opts...)
  76. requests <- op
  77. }
  78. close(requests)
  79. }()
  80. wg.Wait()
  81. bar.Finish()
  82. close(results)
  83. <-pdoneC
  84. }
  85. func doRange(client v3.KV, requests <-chan v3.Op) {
  86. defer wg.Done()
  87. for op := range requests {
  88. st := time.Now()
  89. _, err := client.Do(context.Background(), op)
  90. var errStr string
  91. if err != nil {
  92. errStr = err.Error()
  93. }
  94. results <- result{errStr: errStr, duration: time.Since(st), happened: time.Now()}
  95. bar.Increment()
  96. }
  97. }