put.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2015 The etcd Authors
  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"
  19. "math/rand"
  20. "os"
  21. "time"
  22. v3 "github.com/coreos/etcd/clientv3"
  23. "github.com/coreos/etcd/pkg/report"
  24. "github.com/spf13/cobra"
  25. "golang.org/x/net/context"
  26. "golang.org/x/time/rate"
  27. "gopkg.in/cheggaaa/pb.v1"
  28. )
  29. // putCmd represents the put command
  30. var putCmd = &cobra.Command{
  31. Use: "put",
  32. Short: "Benchmark put",
  33. Run: putFunc,
  34. }
  35. var (
  36. keySize int
  37. valSize int
  38. putTotal int
  39. putRate int
  40. keySpaceSize int
  41. seqKeys bool
  42. compactInterval time.Duration
  43. compactIndexDelta int64
  44. )
  45. func init() {
  46. RootCmd.AddCommand(putCmd)
  47. putCmd.Flags().IntVar(&keySize, "key-size", 8, "Key size of put request")
  48. putCmd.Flags().IntVar(&valSize, "val-size", 8, "Value size of put request")
  49. putCmd.Flags().IntVar(&putRate, "rate", 0, "Maximum puts per second (0 is no limit)")
  50. putCmd.Flags().IntVar(&putTotal, "total", 10000, "Total number of put requests")
  51. putCmd.Flags().IntVar(&keySpaceSize, "key-space-size", 1, "Maximum possible keys")
  52. putCmd.Flags().BoolVar(&seqKeys, "sequential-keys", false, "Use sequential keys")
  53. putCmd.Flags().DurationVar(&compactInterval, "compact-interval", 0, `Interval to compact database (do not duplicate this with etcd's 'auto-compaction-retention' flag) (e.g. --compact-interval=5m compacts every 5-minute)`)
  54. putCmd.Flags().Int64Var(&compactIndexDelta, "compact-index-delta", 1000, "Delta between current revision and compact revision (e.g. current revision 10000, compact at 9000)")
  55. }
  56. func putFunc(cmd *cobra.Command, args []string) {
  57. if keySpaceSize <= 0 {
  58. fmt.Fprintf(os.Stderr, "expected positive --key-space-size, got (%v)", keySpaceSize)
  59. os.Exit(1)
  60. }
  61. requests := make(chan v3.Op, totalClients)
  62. if putRate == 0 {
  63. putRate = math.MaxInt32
  64. }
  65. limit := rate.NewLimiter(rate.Limit(putRate), 1)
  66. clients := mustCreateClients(totalClients, totalConns)
  67. k, v := make([]byte, keySize), string(mustRandBytes(valSize))
  68. bar = pb.New(putTotal)
  69. bar.Format("Bom !")
  70. bar.Start()
  71. r := newReport()
  72. for i := range clients {
  73. wg.Add(1)
  74. go func(c *v3.Client) {
  75. defer wg.Done()
  76. for op := range requests {
  77. limit.Wait(context.Background())
  78. st := time.Now()
  79. _, err := c.Do(context.Background(), op)
  80. r.Results() <- report.Result{Err: err, Start: st, End: time.Now()}
  81. bar.Increment()
  82. }
  83. }(clients[i])
  84. }
  85. go func() {
  86. for i := 0; i < putTotal; i++ {
  87. if seqKeys {
  88. binary.PutVarint(k, int64(i%keySpaceSize))
  89. } else {
  90. binary.PutVarint(k, int64(rand.Intn(keySpaceSize)))
  91. }
  92. requests <- v3.OpPut(string(k), v)
  93. }
  94. close(requests)
  95. }()
  96. if compactInterval > 0 {
  97. go func() {
  98. for {
  99. time.Sleep(compactInterval)
  100. compactKV(clients)
  101. }
  102. }()
  103. }
  104. rc := r.Run()
  105. wg.Wait()
  106. close(r.Results())
  107. bar.Finish()
  108. fmt.Println(<-rc)
  109. }
  110. func compactKV(clients []*v3.Client) {
  111. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  112. resp, err := clients[0].KV.Get(ctx, "foo")
  113. cancel()
  114. if err != nil {
  115. panic(err)
  116. }
  117. revToCompact := max(0, resp.Header.Revision-compactIndexDelta)
  118. ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
  119. _, err = clients[0].KV.Compact(ctx, revToCompact)
  120. cancel()
  121. if err != nil {
  122. panic(err)
  123. }
  124. }
  125. func max(n1, n2 int64) int64 {
  126. if n1 > n2 {
  127. return n1
  128. }
  129. return n2
  130. }