range.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  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 := []byte(args[0])
  45. var end []byte
  46. if len(args) == 2 {
  47. end = []byte(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 etcdserverpb.RangeRequest, 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. r := etcdserverpb.RangeRequest{Key: k, RangeEnd: end}
  71. if rangeConsistency == "s" {
  72. r.Serializable = true
  73. }
  74. requests <- r
  75. }
  76. close(requests)
  77. }()
  78. wg.Wait()
  79. bar.Finish()
  80. close(results)
  81. <-pdoneC
  82. }
  83. func doRange(client etcdserverpb.KVClient, requests <-chan etcdserverpb.RangeRequest) {
  84. defer wg.Done()
  85. for req := range requests {
  86. st := time.Now()
  87. _, err := client.Range(context.Background(), &req)
  88. var errStr string
  89. if err != nil {
  90. errStr = err.Error()
  91. }
  92. results <- result{errStr: errStr, duration: time.Since(st)}
  93. bar.Increment()
  94. }
  95. }