put.go 3.6 KB

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