stm.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/spf13/cobra"
  24. "golang.org/x/net/context"
  25. "gopkg.in/cheggaaa/pb.v1"
  26. )
  27. // stmCmd represents the STM benchmark command
  28. var stmCmd = &cobra.Command{
  29. Use: "stm",
  30. Short: "Benchmark STM",
  31. Run: stmFunc,
  32. }
  33. type stmApply func(v3sync.STM) error
  34. var (
  35. stmIsolation string
  36. stmTotal int
  37. stmKeysPerTxn int
  38. stmKeyCount int
  39. stmValSize int
  40. stmWritePercent int
  41. stmMutex bool
  42. mkSTM func(context.Context, *v3.Client, func(v3sync.STM) error) (*v3.TxnResponse, error)
  43. )
  44. func init() {
  45. RootCmd.AddCommand(stmCmd)
  46. stmCmd.Flags().StringVar(&stmIsolation, "isolation", "r", "Read Committed (c), Repeatable Reads (r), or Serializable (s)")
  47. stmCmd.Flags().IntVar(&stmKeyCount, "keys", 1, "Total unique keys accessible by the benchmark")
  48. stmCmd.Flags().IntVar(&stmTotal, "total", 10000, "Total number of completed STM transactions")
  49. stmCmd.Flags().IntVar(&stmKeysPerTxn, "keys-per-txn", 1, "Number of keys to access per transaction")
  50. stmCmd.Flags().IntVar(&stmWritePercent, "txn-wr-percent", 50, "Percentage of keys to overwrite per transaction")
  51. stmCmd.Flags().BoolVar(&stmMutex, "use-mutex", false, "Wrap STM transaction in a distributed mutex")
  52. stmCmd.Flags().IntVar(&stmValSize, "val-size", 8, "Value size of each STM put request")
  53. }
  54. func stmFunc(cmd *cobra.Command, args []string) {
  55. if stmKeyCount <= 0 {
  56. fmt.Fprintf(os.Stderr, "expected positive --keys, got (%v)", stmKeyCount)
  57. os.Exit(1)
  58. }
  59. if stmWritePercent < 0 || stmWritePercent > 100 {
  60. fmt.Fprintf(os.Stderr, "expected [0, 100] --txn-wr-percent, got (%v)", stmWritePercent)
  61. os.Exit(1)
  62. }
  63. if stmKeysPerTxn < 0 || stmKeysPerTxn > stmKeyCount {
  64. fmt.Fprintf(os.Stderr, "expected --keys-per-txn between 0 and %v, got (%v)", stmKeyCount, stmKeysPerTxn)
  65. os.Exit(1)
  66. }
  67. switch stmIsolation {
  68. case "c":
  69. mkSTM = v3sync.NewSTMReadCommitted
  70. case "r":
  71. mkSTM = v3sync.NewSTMRepeatable
  72. case "s":
  73. mkSTM = v3sync.NewSTMSerializable
  74. default:
  75. fmt.Fprintln(os.Stderr, cmd.Usage())
  76. os.Exit(1)
  77. }
  78. results = make(chan result)
  79. requests := make(chan stmApply, totalClients)
  80. bar = pb.New(stmTotal)
  81. clients := mustCreateClients(totalClients, totalConns)
  82. bar.Format("Bom !")
  83. bar.Start()
  84. for i := range clients {
  85. wg.Add(1)
  86. go doSTM(context.Background(), clients[i], requests)
  87. }
  88. pdoneC := printReport(results)
  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. wg.Wait()
  114. bar.Finish()
  115. close(results)
  116. <-pdoneC
  117. }
  118. func doSTM(ctx context.Context, client *v3.Client, requests <-chan stmApply) {
  119. defer wg.Done()
  120. var m *v3sync.Mutex
  121. if stmMutex {
  122. m = v3sync.NewMutex(client, "stmlock")
  123. }
  124. for applyf := range requests {
  125. st := time.Now()
  126. if m != nil {
  127. m.Lock(context.TODO())
  128. }
  129. _, err := mkSTM(context.TODO(), client, applyf)
  130. if m != nil {
  131. m.Unlock(context.TODO())
  132. }
  133. var errStr string
  134. if err != nil {
  135. errStr = err.Error()
  136. }
  137. results <- result{errStr: errStr, duration: time.Since(st), happened: time.Now()}
  138. bar.Increment()
  139. }
  140. }