range.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. )
  33. func init() {
  34. RootCmd.AddCommand(rangeCmd)
  35. rangeCmd.Flags().IntVar(&rangeTotal, "total", 10000, "Total number of range requests")
  36. }
  37. func rangeFunc(cmd *cobra.Command, args []string) {
  38. if len(args) == 0 || len(args) > 2 {
  39. fmt.Fprintln(os.Stderr, cmd.Usage())
  40. os.Exit(1)
  41. }
  42. k := []byte(args[0])
  43. var end []byte
  44. if len(args) == 1 {
  45. end = []byte(args[1])
  46. }
  47. results = make(chan result)
  48. requests := make(chan etcdserverpb.RangeRequest, totalClients)
  49. bar = pb.New(rangeTotal)
  50. clients := mustCreateClients(totalClients, totalConns)
  51. bar.Format("Bom !")
  52. bar.Start()
  53. for i := range clients {
  54. wg.Add(1)
  55. go doRange(clients[i].KV, requests)
  56. }
  57. pdoneC := printReport(results)
  58. go func() {
  59. for i := 0; i < rangeTotal; i++ {
  60. requests <- etcdserverpb.RangeRequest{
  61. Key: k,
  62. RangeEnd: end}
  63. }
  64. close(requests)
  65. }()
  66. wg.Wait()
  67. bar.Finish()
  68. close(results)
  69. <-pdoneC
  70. }
  71. func doRange(client etcdserverpb.KVClient, requests <-chan etcdserverpb.RangeRequest) {
  72. defer wg.Done()
  73. for req := range requests {
  74. st := time.Now()
  75. _, err := client.Range(context.Background(), &req)
  76. var errStr string
  77. if err != nil {
  78. errStr = err.Error()
  79. }
  80. results <- result{errStr: errStr, duration: time.Since(st)}
  81. bar.Increment()
  82. }
  83. }