stm.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2016 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. v3sync "github.com/coreos/etcd/clientv3/concurrency"
  23. "github.com/coreos/etcd/pkg/report"
  24. "github.com/spf13/cobra"
  25. "golang.org/x/net/context"
  26. "gopkg.in/cheggaaa/pb.v1"
  27. )
  28. // stmCmd represents the STM benchmark command
  29. var stmCmd = &cobra.Command{
  30. Use: "stm",
  31. Short: "Benchmark STM",
  32. Run: stmFunc,
  33. }
  34. type stmApply func(v3sync.STM) error
  35. var (
  36. stmIsolation string
  37. stmIso v3sync.Isolation
  38. stmTotal int
  39. stmKeysPerTxn int
  40. stmKeyCount int
  41. stmValSize int
  42. stmWritePercent int
  43. stmMutex bool
  44. )
  45. func init() {
  46. RootCmd.AddCommand(stmCmd)
  47. stmCmd.Flags().StringVar(&stmIsolation, "isolation", "r", "Read Committed (c), Repeatable Reads (r), Serializable (s), or Snapshot (ss)")
  48. stmCmd.Flags().IntVar(&stmKeyCount, "keys", 1, "Total unique keys accessible by the benchmark")
  49. stmCmd.Flags().IntVar(&stmTotal, "total", 10000, "Total number of completed STM transactions")
  50. stmCmd.Flags().IntVar(&stmKeysPerTxn, "keys-per-txn", 1, "Number of keys to access per transaction")
  51. stmCmd.Flags().IntVar(&stmWritePercent, "txn-wr-percent", 50, "Percentage of keys to overwrite per transaction")
  52. stmCmd.Flags().BoolVar(&stmMutex, "use-mutex", false, "Wrap STM transaction in a distributed mutex")
  53. stmCmd.Flags().IntVar(&stmValSize, "val-size", 8, "Value size of each STM put request")
  54. }
  55. func stmFunc(cmd *cobra.Command, args []string) {
  56. if stmKeyCount <= 0 {
  57. fmt.Fprintf(os.Stderr, "expected positive --keys, got (%v)", stmKeyCount)
  58. os.Exit(1)
  59. }
  60. if stmWritePercent < 0 || stmWritePercent > 100 {
  61. fmt.Fprintf(os.Stderr, "expected [0, 100] --txn-wr-percent, got (%v)", stmWritePercent)
  62. os.Exit(1)
  63. }
  64. if stmKeysPerTxn < 0 || stmKeysPerTxn > stmKeyCount {
  65. fmt.Fprintf(os.Stderr, "expected --keys-per-txn between 0 and %v, got (%v)", stmKeyCount, stmKeysPerTxn)
  66. os.Exit(1)
  67. }
  68. switch stmIsolation {
  69. case "c":
  70. stmIso = v3sync.ReadCommitted
  71. case "r":
  72. stmIso = v3sync.RepeatableReads
  73. case "s":
  74. stmIso = v3sync.Serializable
  75. case "ss":
  76. stmIso = v3sync.Snapshot
  77. default:
  78. fmt.Fprintln(os.Stderr, cmd.Usage())
  79. os.Exit(1)
  80. }
  81. requests := make(chan stmApply, totalClients)
  82. clients := mustCreateClients(totalClients, totalConns)
  83. bar = pb.New(stmTotal)
  84. bar.Format("Bom !")
  85. bar.Start()
  86. r := newReport()
  87. for i := range clients {
  88. wg.Add(1)
  89. go doSTM(clients[i], requests, r.Results())
  90. }
  91. go func() {
  92. for i := 0; i < stmTotal; i++ {
  93. kset := make(map[string]struct{})
  94. for len(kset) != stmKeysPerTxn {
  95. k := make([]byte, 16)
  96. binary.PutVarint(k, int64(rand.Intn(stmKeyCount)))
  97. s := string(k)
  98. kset[s] = struct{}{}
  99. }
  100. applyf := func(s v3sync.STM) error {
  101. wrs := int(float32(len(kset)*stmWritePercent) / 100.0)
  102. for k := range kset {
  103. s.Get(k)
  104. if wrs > 0 {
  105. s.Put(k, string(mustRandBytes(stmValSize)))
  106. wrs--
  107. }
  108. }
  109. return nil
  110. }
  111. requests <- applyf
  112. }
  113. close(requests)
  114. }()
  115. rc := r.Run()
  116. wg.Wait()
  117. close(r.Results())
  118. bar.Finish()
  119. fmt.Printf("%s", <-rc)
  120. }
  121. func doSTM(client *v3.Client, requests <-chan stmApply, results chan<- report.Result) {
  122. defer wg.Done()
  123. var m *v3sync.Mutex
  124. if stmMutex {
  125. s, err := v3sync.NewSession(client)
  126. if err != nil {
  127. panic(err)
  128. }
  129. m = v3sync.NewMutex(s, "stmlock")
  130. }
  131. for applyf := range requests {
  132. st := time.Now()
  133. if m != nil {
  134. m.Lock(context.TODO())
  135. }
  136. _, err := v3sync.NewSTM(client, applyf, v3sync.WithIsolation(stmIso))
  137. if m != nil {
  138. m.Unlock(context.TODO())
  139. }
  140. results <- report.Result{Err: err, Start: st, End: time.Now()}
  141. bar.Increment()
  142. }
  143. }