range.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/Godeps/_workspace/src/google.golang.org/grpc"
  23. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  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. )
  34. func init() {
  35. RootCmd.AddCommand(rangeCmd)
  36. rangeCmd.Flags().IntVar(&rangeTotal, "total", 10000, "Total number of range requests")
  37. }
  38. func rangeFunc(cmd *cobra.Command, args []string) {
  39. if len(args) == 0 || len(args) > 2 {
  40. fmt.Fprintln(os.Stderr, cmd.Usage())
  41. os.Exit(1)
  42. }
  43. k := []byte(args[0])
  44. var end []byte
  45. if len(args) == 1 {
  46. end = []byte(args[1])
  47. }
  48. results = make(chan *result, rangeTotal)
  49. requests := make(chan *etcdserverpb.RangeRequest, rangeTotal)
  50. bar = pb.New(rangeTotal)
  51. conns := make([]*grpc.ClientConn, totalConns)
  52. for i := range conns {
  53. conns[i] = mustCreateConn()
  54. }
  55. clients := make([]etcdserverpb.KVClient, totalClients)
  56. for i := range clients {
  57. clients[i] = etcdserverpb.NewKVClient(conns[i%int(totalConns)])
  58. }
  59. bar.Format("Bom !")
  60. bar.Start()
  61. for i := range clients {
  62. wg.Add(1)
  63. go doRange(clients[i], requests)
  64. }
  65. start := time.Now()
  66. for i := 0; i < rangeTotal; i++ {
  67. r := &etcdserverpb.RangeRequest{
  68. Key: k,
  69. RangeEnd: end,
  70. }
  71. requests <- r
  72. }
  73. close(requests)
  74. wg.Wait()
  75. bar.Finish()
  76. printReport(rangeTotal, results, time.Now().Sub(start))
  77. }
  78. func doRange(client etcdserverpb.KVClient, requests <-chan *etcdserverpb.RangeRequest) {
  79. defer wg.Done()
  80. for req := range requests {
  81. st := time.Now()
  82. _, err := client.Range(context.Background(), req)
  83. var errStr string
  84. if err != nil {
  85. errStr = err.Error()
  86. }
  87. results <- &result{
  88. errStr: errStr,
  89. duration: time.Now().Sub(st),
  90. }
  91. bar.Increment()
  92. }
  93. }