main.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "strconv"
  20. "sync"
  21. "time"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/cheggaaa/pb"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  24. )
  25. var (
  26. bar *pb.ProgressBar
  27. results chan *result
  28. wg sync.WaitGroup
  29. )
  30. func main() {
  31. var (
  32. c, n int
  33. url string
  34. size int
  35. )
  36. flag.IntVar(&c, "c", 50, "number of connections")
  37. flag.IntVar(&n, "n", 200, "number of requests")
  38. flag.IntVar(&size, "s", 128, "size of put request")
  39. // TODO: config the number of concurrency in each connection
  40. flag.StringVar(&url, "u", "127.0.0.1:12379", "etcd server endpoint")
  41. flag.Parse()
  42. if flag.NArg() < 1 {
  43. flag.Usage()
  44. os.Exit(1)
  45. }
  46. var act string
  47. if act = flag.Args()[0]; act != "get" && act != "put" {
  48. fmt.Printf("unsupported action %v\n", act)
  49. os.Exit(1)
  50. }
  51. conn, err := grpc.Dial(url)
  52. if err != nil {
  53. fmt.Errorf("dial error: %v", err)
  54. os.Exit(1)
  55. }
  56. results = make(chan *result, n)
  57. bar = pb.New(n)
  58. bar.Format("Bom !")
  59. bar.Start()
  60. start := time.Now()
  61. if act == "get" {
  62. var rangeEnd []byte
  63. key := []byte(flag.Args()[1])
  64. if len(flag.Args()) > 2 {
  65. rangeEnd = []byte(flag.Args()[2])
  66. }
  67. benchGet(conn, key, rangeEnd, n, c)
  68. } else if act == "put" {
  69. key := []byte(flag.Args()[1])
  70. // number of different keys to put into etcd
  71. kc, err := strconv.ParseInt(flag.Args()[2], 10, 32)
  72. if err != nil {
  73. panic(err)
  74. }
  75. benchPut(conn, key, int(kc), n, c, size)
  76. }
  77. wg.Wait()
  78. bar.Finish()
  79. printReport(n, results, time.Now().Sub(start))
  80. }