put.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. compactInterval time.Duration
  39. compactIndexDelta int64
  40. sample bool
  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. putCmd.Flags().BoolVar(&sample, "sample", false, "'true' to sample requests for every second")
  52. }
  53. func putFunc(cmd *cobra.Command, args []string) {
  54. if keySpaceSize <= 0 {
  55. fmt.Fprintf(os.Stderr, "expected positive --key-space-size, got (%v)", keySpaceSize)
  56. os.Exit(1)
  57. }
  58. results = make(chan result)
  59. requests := make(chan v3.Op, totalClients)
  60. bar = pb.New(putTotal)
  61. k, v := make([]byte, keySize), string(mustRandBytes(valSize))
  62. clients := mustCreateClients(totalClients, totalConns)
  63. bar.Format("Bom !")
  64. bar.Start()
  65. for i := range clients {
  66. wg.Add(1)
  67. go doPut(context.Background(), clients[i], requests)
  68. }
  69. pdoneC := printReport(results)
  70. go func() {
  71. for i := 0; i < putTotal; i++ {
  72. if seqKeys {
  73. binary.PutVarint(k, int64(i%keySpaceSize))
  74. } else {
  75. binary.PutVarint(k, int64(rand.Intn(keySpaceSize)))
  76. }
  77. requests <- v3.OpPut(string(k), v)
  78. }
  79. close(requests)
  80. }()
  81. if compactInterval > 0 {
  82. go func() {
  83. for {
  84. time.Sleep(compactInterval)
  85. compactKV(clients)
  86. }
  87. }()
  88. }
  89. wg.Wait()
  90. bar.Finish()
  91. close(results)
  92. <-pdoneC
  93. }
  94. func doPut(ctx context.Context, client v3.KV, requests <-chan v3.Op) {
  95. defer wg.Done()
  96. for op := range requests {
  97. st := time.Now()
  98. _, err := client.Do(ctx, op)
  99. var errStr string
  100. if err != nil {
  101. errStr = err.Error()
  102. }
  103. results <- result{errStr: errStr, duration: time.Since(st), happened: time.Now()}
  104. bar.Increment()
  105. }
  106. }
  107. func compactKV(clients []*v3.Client) {
  108. var curRev int64
  109. for _, c := range clients {
  110. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  111. resp, err := c.KV.Get(ctx, "foo")
  112. cancel()
  113. if err != nil {
  114. panic(err)
  115. }
  116. curRev = resp.Header.Revision
  117. break
  118. }
  119. revToCompact := max(0, curRev-compactIndexDelta)
  120. for _, c := range clients {
  121. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  122. err := c.KV.Compact(ctx, revToCompact)
  123. cancel()
  124. if err != nil {
  125. panic(err)
  126. }
  127. break
  128. }
  129. }
  130. func max(n1, n2 int64) int64 {
  131. if n1 > n2 {
  132. return n1
  133. }
  134. return n2
  135. }