main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 main
  15. import (
  16. "flag"
  17. "fmt"
  18. "os"
  19. "sync"
  20. "time"
  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. "github.com/rakyll/pb"
  25. )
  26. func main() {
  27. var c, n int
  28. var url string
  29. flag.IntVar(&c, "c", 50, "number of connections")
  30. flag.IntVar(&n, "n", 200, "number of requests")
  31. // TODO: config the number of concurrency in each connection
  32. flag.StringVar(&url, "u", "127.0.0.1:12379", "etcd server endpoint")
  33. flag.Parse()
  34. if flag.NArg() < 1 {
  35. flag.Usage()
  36. os.Exit(1)
  37. }
  38. if act := flag.Args()[0]; act != "get" {
  39. fmt.Errorf("unsupported action %v", act)
  40. os.Exit(1)
  41. }
  42. var rangeEnd []byte
  43. key := []byte(flag.Args()[1])
  44. if len(flag.Args()) > 2 {
  45. rangeEnd = []byte(flag.Args()[2])
  46. }
  47. results := make(chan *result, n)
  48. bar := pb.New(n)
  49. bar.Format("Bom !")
  50. bar.Start()
  51. start := time.Now()
  52. defer func() {
  53. bar.Finish()
  54. printReport(n, results, time.Now().Sub(start))
  55. }()
  56. var wg sync.WaitGroup
  57. wg.Add(c)
  58. jobs := make(chan struct{}, n)
  59. for i := 0; i < c; i++ {
  60. go func() {
  61. defer wg.Done()
  62. conn, err := grpc.Dial(url)
  63. if err != nil {
  64. fmt.Errorf("dial error: %v", err)
  65. os.Exit(1)
  66. }
  67. etcd := etcdserverpb.NewEtcdClient(conn)
  68. req := &etcdserverpb.RangeRequest{Key: key, RangeEnd: rangeEnd}
  69. for _ = range jobs {
  70. st := time.Now()
  71. resp, err := etcd.Range(context.Background(), req)
  72. var errStr string
  73. if err != nil {
  74. errStr = err.Error()
  75. } else {
  76. errStr = resp.Header.Error
  77. }
  78. results <- &result{
  79. errStr: errStr,
  80. duration: time.Now().Sub(st),
  81. }
  82. bar.Increment()
  83. }
  84. }()
  85. }
  86. for i := 0; i < n; i++ {
  87. jobs <- struct{}{}
  88. }
  89. close(jobs)
  90. wg.Wait()
  91. }