put.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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)
  42. requests := make(chan etcdserverpb.PutRequest, totalClients)
  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(context.Background(), clients[i], requests)
  58. }
  59. pdoneC := printReport(results)
  60. go func() {
  61. for i := 0; i < putTotal; i++ {
  62. requests <- etcdserverpb.PutRequest{Key: k, Value: v}
  63. }
  64. close(requests)
  65. }()
  66. wg.Wait()
  67. bar.Finish()
  68. close(results)
  69. <-pdoneC
  70. }
  71. func doPut(ctx context.Context, client etcdserverpb.KVClient, requests <-chan etcdserverpb.PutRequest) {
  72. defer wg.Done()
  73. for r := range requests {
  74. st := time.Now()
  75. _, err := client.Put(ctx, &r)
  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. }