put.go 2.7 KB

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