main.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/github.com/rakyll/pb"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  23. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  24. )
  25. var (
  26. bar *pb.ProgressBar
  27. results chan *result
  28. wg sync.WaitGroup
  29. )
  30. func main() {
  31. var c, n int
  32. var url string
  33. flag.IntVar(&c, "c", 50, "number of connections")
  34. flag.IntVar(&n, "n", 200, "number of requests")
  35. // TODO: config the number of concurrency in each connection
  36. flag.StringVar(&url, "u", "127.0.0.1:12379", "etcd server endpoint")
  37. flag.Parse()
  38. if flag.NArg() < 1 {
  39. flag.Usage()
  40. os.Exit(1)
  41. }
  42. if act := flag.Args()[0]; act != "get" {
  43. fmt.Errorf("unsupported action %v", act)
  44. os.Exit(1)
  45. }
  46. var rangeEnd []byte
  47. key := []byte(flag.Args()[1])
  48. if len(flag.Args()) > 2 {
  49. rangeEnd = []byte(flag.Args()[2])
  50. }
  51. results = make(chan *result, n)
  52. bar = pb.New(n)
  53. bar.Format("Bom !")
  54. bar.Start()
  55. start := time.Now()
  56. wg.Add(c)
  57. requests := make(chan struct{}, n)
  58. conn, err := grpc.Dial(url)
  59. if err != nil {
  60. fmt.Errorf("dial error: %v", err)
  61. os.Exit(1)
  62. }
  63. for i := 0; i < c; i++ {
  64. go get(etcdserverpb.NewEtcdClient(conn), key, rangeEnd, requests)
  65. }
  66. for i := 0; i < n; i++ {
  67. requests <- struct{}{}
  68. }
  69. close(requests)
  70. wg.Wait()
  71. bar.Finish()
  72. printReport(n, results, time.Now().Sub(start))
  73. }