put.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/rand"
  19. "os"
  20. "time"
  21. v3 "github.com/coreos/etcd/clientv3"
  22. "github.com/spf13/cobra"
  23. "golang.org/x/net/context"
  24. "gopkg.in/cheggaaa/pb.v1"
  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. compactInterval time.Duration
  39. compactIndexDelta int64
  40. )
  41. func init() {
  42. RootCmd.AddCommand(putCmd)
  43. putCmd.Flags().IntVar(&keySize, "key-size", 8, "Key size of put request")
  44. putCmd.Flags().IntVar(&valSize, "val-size", 8, "Value size of put request")
  45. putCmd.Flags().IntVar(&putTotal, "total", 10000, "Total number of put requests")
  46. putCmd.Flags().IntVar(&keySpaceSize, "key-space-size", 1, "Maximum possible keys")
  47. putCmd.Flags().BoolVar(&seqKeys, "sequential-keys", false, "Use sequential keys")
  48. 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)`)
  49. putCmd.Flags().Int64Var(&compactIndexDelta, "compact-index-delta", 1000, "Delta between current revision and compact revision (e.g. current revision 10000, compact at 9000)")
  50. }
  51. func putFunc(cmd *cobra.Command, args []string) {
  52. if keySpaceSize <= 0 {
  53. fmt.Fprintf(os.Stderr, "expected positive --key-space-size, got (%v)", keySpaceSize)
  54. os.Exit(1)
  55. }
  56. results = make(chan result)
  57. requests := make(chan v3.Op, totalClients)
  58. bar = pb.New(putTotal)
  59. k, v := make([]byte, keySize), string(mustRandBytes(valSize))
  60. clients := mustCreateClients(totalClients, totalConns)
  61. bar.Format("Bom !")
  62. bar.Start()
  63. for i := range clients {
  64. wg.Add(1)
  65. go doPut(context.Background(), clients[i], requests)
  66. }
  67. pdoneC := printReport(results)
  68. go func() {
  69. for i := 0; i < putTotal; i++ {
  70. if seqKeys {
  71. binary.PutVarint(k, int64(i%keySpaceSize))
  72. } else {
  73. binary.PutVarint(k, int64(rand.Intn(keySpaceSize)))
  74. }
  75. requests <- v3.OpPut(string(k), v)
  76. }
  77. close(requests)
  78. }()
  79. if compactInterval > 0 {
  80. go func() {
  81. for {
  82. time.Sleep(compactInterval)
  83. compactKV(clients)
  84. }
  85. }()
  86. }
  87. wg.Wait()
  88. bar.Finish()
  89. close(results)
  90. <-pdoneC
  91. }
  92. func doPut(ctx context.Context, client v3.KV, requests <-chan v3.Op) {
  93. defer wg.Done()
  94. for op := range requests {
  95. st := time.Now()
  96. _, err := client.Do(ctx, op)
  97. var errStr string
  98. if err != nil {
  99. errStr = err.Error()
  100. }
  101. results <- result{errStr: errStr, duration: time.Since(st), happened: time.Now()}
  102. bar.Increment()
  103. }
  104. }
  105. func compactKV(clients []*v3.Client) {
  106. var curRev int64
  107. for _, c := range clients {
  108. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  109. resp, err := c.KV.Get(ctx, "foo")
  110. cancel()
  111. if err != nil {
  112. panic(err)
  113. }
  114. curRev = resp.Header.Revision
  115. break
  116. }
  117. revToCompact := max(0, curRev-compactIndexDelta)
  118. for _, c := range clients {
  119. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  120. _, err := c.KV.Compact(ctx, revToCompact)
  121. cancel()
  122. if err != nil {
  123. panic(err)
  124. }
  125. break
  126. }
  127. }
  128. func max(n1, n2 int64) int64 {
  129. if n1 > n2 {
  130. return n1
  131. }
  132. return n2
  133. }