put.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "time"
  17. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/cheggaaa/pb"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  21. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. )
  23. // putCmd represents the put command
  24. var putCmd = &cobra.Command{
  25. Use: "put",
  26. Short: "Benchmark put",
  27. Run: putFunc,
  28. }
  29. var (
  30. keySize int
  31. valSize int
  32. putTotal int
  33. )
  34. func init() {
  35. RootCmd.AddCommand(putCmd)
  36. putCmd.Flags().IntVar(&keySize, "key-size", 8, "Key size of put request")
  37. putCmd.Flags().IntVar(&valSize, "val-size", 8, "Value size of put request")
  38. putCmd.Flags().IntVar(&putTotal, "total", 10000, "Total number of put requests")
  39. }
  40. func putFunc(cmd *cobra.Command, args []string) {
  41. results = make(chan *result, putTotal)
  42. requests := make(chan *etcdserverpb.PutRequest, putTotal)
  43. bar = pb.New(putTotal)
  44. k, v := mustRandBytes(keySize), mustRandBytes(valSize)
  45. conns := make([]*grpc.ClientConn, totalConns)
  46. for i := range conns {
  47. conns[i] = mustCreateConn()
  48. }
  49. clients := make([]etcdserverpb.KVClient, totalClients)
  50. for i := range clients {
  51. clients[i] = etcdserverpb.NewKVClient(conns[i%int(totalConns)])
  52. }
  53. bar.Format("Bom !")
  54. bar.Start()
  55. for i := range clients {
  56. wg.Add(1)
  57. go doPut(clients[i], requests)
  58. }
  59. start := time.Now()
  60. for i := 0; i < putTotal; i++ {
  61. r := &etcdserverpb.PutRequest{
  62. Key: k,
  63. Value: v,
  64. }
  65. requests <- r
  66. }
  67. close(requests)
  68. wg.Wait()
  69. bar.Finish()
  70. printReport(putTotal, results, time.Now().Sub(start))
  71. }
  72. func doPut(client etcdserverpb.KVClient, requests <-chan *etcdserverpb.PutRequest) {
  73. defer wg.Done()
  74. for r := range requests {
  75. st := time.Now()
  76. _, err := client.Put(context.Background(), r)
  77. var errStr string
  78. if err != nil {
  79. errStr = err.Error()
  80. }
  81. results <- &result{
  82. errStr: errStr,
  83. duration: time.Now().Sub(st),
  84. }
  85. bar.Increment()
  86. }
  87. }