stm.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. mkSTM func(context.Context, *v3.Client, func(v3sync.STM) error) (*v3.TxnResponse, error)
  42. )
  43. func init() {
  44. RootCmd.AddCommand(stmCmd)
  45. stmCmd.Flags().StringVar(&stmIsolation, "isolation", "r", "Repeatable Reads (r) or Serializable (s)")
  46. stmCmd.Flags().IntVar(&stmKeyCount, "keys", 1, "Total unique keys accessible by the benchmark")
  47. stmCmd.Flags().IntVar(&stmTotal, "total", 10000, "Total number of completed STM transactions")
  48. stmCmd.Flags().IntVar(&stmKeysPerTxn, "keys-per-txn", 1, "Number of keys to access per transaction")
  49. stmCmd.Flags().IntVar(&stmWritePercent, "txn-wr-percent", 50, "Percentage of keys to overwrite per transaction")
  50. stmCmd.Flags().IntVar(&stmValSize, "val-size", 8, "Value size of each STM put request")
  51. }
  52. func stmFunc(cmd *cobra.Command, args []string) {
  53. if stmKeyCount <= 0 {
  54. fmt.Fprintf(os.Stderr, "expected positive --keys, got (%v)", stmKeyCount)
  55. os.Exit(1)
  56. }
  57. if stmWritePercent < 0 || stmWritePercent > 100 {
  58. fmt.Fprintf(os.Stderr, "expected [0, 100] --txn-wr-percent, got (%v)", stmWritePercent)
  59. os.Exit(1)
  60. }
  61. if stmKeysPerTxn < 0 || stmKeysPerTxn > stmKeyCount {
  62. fmt.Fprintf(os.Stderr, "expected --keys-per-txn between 0 and %v, got (%v)", stmKeyCount, stmKeysPerTxn)
  63. os.Exit(1)
  64. }
  65. switch stmIsolation {
  66. case "r":
  67. mkSTM = v3sync.NewSTMRepeatable
  68. case "l":
  69. mkSTM = v3sync.NewSTMSerializable
  70. default:
  71. fmt.Fprintln(os.Stderr, cmd.Usage())
  72. os.Exit(1)
  73. }
  74. results = make(chan result)
  75. requests := make(chan stmApply, totalClients)
  76. bar = pb.New(stmTotal)
  77. clients := mustCreateClients(totalClients, totalConns)
  78. bar.Format("Bom !")
  79. bar.Start()
  80. for i := range clients {
  81. wg.Add(1)
  82. go doSTM(context.Background(), clients[i], requests)
  83. }
  84. pdoneC := printReport(results)
  85. go func() {
  86. for i := 0; i < stmTotal; i++ {
  87. kset := make(map[string]struct{})
  88. for len(kset) != stmKeysPerTxn {
  89. k := make([]byte, 16)
  90. binary.PutVarint(k, int64(rand.Intn(stmKeyCount)))
  91. s := string(k)
  92. kset[s] = struct{}{}
  93. }
  94. applyf := func(s v3sync.STM) error {
  95. wrs := int(float32(len(kset)*stmWritePercent) / 100.0)
  96. for k := range kset {
  97. s.Get(k)
  98. if wrs > 0 {
  99. s.Put(k, string(mustRandBytes(stmValSize)))
  100. wrs--
  101. }
  102. }
  103. return nil
  104. }
  105. requests <- applyf
  106. }
  107. close(requests)
  108. }()
  109. wg.Wait()
  110. bar.Finish()
  111. close(results)
  112. <-pdoneC
  113. }
  114. func doSTM(ctx context.Context, client *v3.Client, requests <-chan stmApply) {
  115. defer wg.Done()
  116. for applyf := range requests {
  117. st := time.Now()
  118. _, err := v3sync.NewSTMRepeatable(context.TODO(), client, applyf)
  119. var errStr string
  120. if err != nil {
  121. errStr = err.Error()
  122. }
  123. results <- result{errStr: errStr, duration: time.Since(st), happened: time.Now()}
  124. bar.Increment()
  125. }
  126. }