put.go 3.0 KB

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