stm.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. stmTotal int
  38. stmKeysPerTxn int
  39. stmKeyCount int
  40. stmValSize int
  41. stmWritePercent int
  42. stmMutex bool
  43. mkSTM func(context.Context, *v3.Client, func(v3sync.STM) error) (*v3.TxnResponse, error)
  44. )
  45. func init() {
  46. RootCmd.AddCommand(stmCmd)
  47. stmCmd.Flags().StringVar(&stmIsolation, "isolation", "r", "Read Committed (c), Repeatable Reads (r), or Serializable (s)")
  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. mkSTM = v3sync.NewSTMReadCommitted
  71. case "r":
  72. mkSTM = v3sync.NewSTMRepeatable
  73. case "s":
  74. mkSTM = v3sync.NewSTMSerializable
  75. default:
  76. fmt.Fprintln(os.Stderr, cmd.Usage())
  77. os.Exit(1)
  78. }
  79. requests := make(chan stmApply, totalClients)
  80. clients := mustCreateClients(totalClients, totalConns)
  81. bar = pb.New(stmTotal)
  82. bar.Format("Bom !")
  83. bar.Start()
  84. r := newReport()
  85. for i := range clients {
  86. wg.Add(1)
  87. go doSTM(clients[i], requests, r.Results())
  88. }
  89. go func() {
  90. for i := 0; i < stmTotal; i++ {
  91. kset := make(map[string]struct{})
  92. for len(kset) != stmKeysPerTxn {
  93. k := make([]byte, 16)
  94. binary.PutVarint(k, int64(rand.Intn(stmKeyCount)))
  95. s := string(k)
  96. kset[s] = struct{}{}
  97. }
  98. applyf := func(s v3sync.STM) error {
  99. wrs := int(float32(len(kset)*stmWritePercent) / 100.0)
  100. for k := range kset {
  101. s.Get(k)
  102. if wrs > 0 {
  103. s.Put(k, string(mustRandBytes(stmValSize)))
  104. wrs--
  105. }
  106. }
  107. return nil
  108. }
  109. requests <- applyf
  110. }
  111. close(requests)
  112. }()
  113. rc := r.Run()
  114. wg.Wait()
  115. close(r.Results())
  116. bar.Finish()
  117. fmt.Printf("%s", <-rc)
  118. }
  119. func doSTM(client *v3.Client, requests <-chan stmApply, results chan<- report.Result) {
  120. defer wg.Done()
  121. var m *v3sync.Mutex
  122. if stmMutex {
  123. s, err := v3sync.NewSession(client)
  124. if err != nil {
  125. panic(err)
  126. }
  127. m = v3sync.NewMutex(s, "stmlock")
  128. }
  129. for applyf := range requests {
  130. st := time.Now()
  131. if m != nil {
  132. m.Lock(context.TODO())
  133. }
  134. _, err := mkSTM(context.TODO(), client, applyf)
  135. if m != nil {
  136. m.Unlock(context.TODO())
  137. }
  138. results <- report.Result{Err: err, Start: st, End: time.Now()}
  139. bar.Increment()
  140. }
  141. }