You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.0 KiB

  1. package main
  2. import (
  3. "container/ring"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "math/rand"
  8. "time"
  9. rpchttp "github.com/tendermint/tendermint/rpc/client/http"
  10. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. // Load generates transactions against the network until the given context is
  14. // canceled.
  15. func Load(ctx context.Context, testnet *e2e.Testnet) error {
  16. // Since transactions are executed across all nodes in the network, we need
  17. // to reduce transaction load for larger networks to avoid using too much
  18. // CPU. This gives high-throughput small networks and low-throughput large ones.
  19. // This also limits the number of TCP connections, since each worker has
  20. // a connection to all nodes.
  21. concurrency := 64 / len(testnet.Nodes)
  22. if concurrency == 0 {
  23. concurrency = 1
  24. }
  25. chTx := make(chan types.Tx)
  26. chSuccess := make(chan int) // success counts per iteration
  27. ctx, cancel := context.WithCancel(ctx)
  28. defer cancel()
  29. // Spawn job generator and processors.
  30. logger.Info(fmt.Sprintf("Starting transaction load (%v workers)...", concurrency))
  31. started := time.Now()
  32. go loadGenerate(ctx, chTx, testnet.TxSize)
  33. for w := 0; w < concurrency; w++ {
  34. go loadProcess(ctx, testnet, chTx, chSuccess)
  35. }
  36. // Montior transaction to ensure load propagates to the network
  37. //
  38. // This loop doesn't check or time out for stalls, since a stall here just
  39. // aborts the load generator sooner and could obscure backpressure
  40. // from the test harness, and there are other checks for
  41. // stalls in the framework. Ideally we should monitor latency as a guide
  42. // for when to give up, but we don't have a good way to track that yet.
  43. success := 0
  44. for {
  45. select {
  46. case numSeen := <-chSuccess:
  47. success += numSeen
  48. case <-ctx.Done():
  49. if success == 0 {
  50. return errors.New("failed to submit any transactions")
  51. }
  52. rate := float64(success) / time.Since(started).Seconds()
  53. logger.Info("ending transaction load",
  54. "dur_secs", time.Since(started).Seconds(),
  55. "txns", success,
  56. "rate", rate,
  57. "slow", rate < 1)
  58. return nil
  59. }
  60. }
  61. }
  62. // loadGenerate generates jobs until the context is canceled.
  63. //
  64. // The chTx has multiple consumers, thus the rate limiting of the load
  65. // generation is primarily the result of backpressure from the
  66. // broadcast transaction, though there is still some timer-based
  67. // limiting.
  68. func loadGenerate(ctx context.Context, chTx chan<- types.Tx, size int64) {
  69. timer := time.NewTimer(0)
  70. defer timer.Stop()
  71. defer close(chTx)
  72. for {
  73. select {
  74. case <-ctx.Done():
  75. return
  76. case <-timer.C:
  77. }
  78. // We keep generating the same 100 keys over and over, with different values.
  79. // This gives a reasonable load without putting too much data in the app.
  80. id := rand.Int63() % 100 // nolint: gosec
  81. bz := make([]byte, size)
  82. _, err := rand.Read(bz) // nolint: gosec
  83. if err != nil {
  84. panic(fmt.Sprintf("Failed to read random bytes: %v", err))
  85. }
  86. tx := types.Tx(fmt.Sprintf("load-%X=%x", id, bz))
  87. select {
  88. case <-ctx.Done():
  89. return
  90. case chTx <- tx:
  91. // sleep for a bit before sending the
  92. // next transaction.
  93. timer.Reset(loadGenerateWaitTime(size))
  94. }
  95. }
  96. }
  97. func loadGenerateWaitTime(size int64) time.Duration {
  98. const (
  99. min = int64(100 * time.Millisecond)
  100. max = int64(time.Second)
  101. )
  102. var (
  103. baseJitter = rand.Int63n(max-min+1) + min // nolint: gosec
  104. sizeFactor = size * int64(time.Millisecond)
  105. sizeJitter = rand.Int63n(sizeFactor-min+1) + min // nolint: gosec
  106. )
  107. return time.Duration(baseJitter + sizeJitter)
  108. }
  109. // loadProcess processes transactions
  110. func loadProcess(ctx context.Context, testnet *e2e.Testnet, chTx <-chan types.Tx, chSuccess chan<- int) {
  111. // Each worker gets its own client to each usable node, which
  112. // allows for some concurrency while still bounding it.
  113. clients := make([]*rpchttp.HTTP, 0, len(testnet.Nodes))
  114. for idx := range testnet.Nodes {
  115. // Construct a list of usable nodes for the creating
  116. // load. Don't send load through seed nodes because
  117. // they do not provide the RPC endpoints required to
  118. // broadcast transaction.
  119. if testnet.Nodes[idx].Mode == e2e.ModeSeed {
  120. continue
  121. }
  122. client, err := testnet.Nodes[idx].Client()
  123. if err != nil {
  124. continue
  125. }
  126. clients = append(clients, client)
  127. }
  128. if len(clients) == 0 {
  129. panic("no clients to process load")
  130. }
  131. // Put the clients in a ring so they can be used in a
  132. // round-robin fashion.
  133. clientRing := ring.New(len(clients))
  134. for idx := range clients {
  135. clientRing.Value = clients[idx]
  136. clientRing = clientRing.Next()
  137. }
  138. successes := 0
  139. for {
  140. select {
  141. case <-ctx.Done():
  142. return
  143. case tx := <-chTx:
  144. clientRing = clientRing.Next()
  145. client := clientRing.Value.(*rpchttp.HTTP)
  146. if status, err := client.Status(ctx); err != nil {
  147. continue
  148. } else if status.SyncInfo.CatchingUp {
  149. continue
  150. }
  151. if _, err := client.BroadcastTxSync(ctx, tx); err != nil {
  152. continue
  153. }
  154. successes++
  155. select {
  156. case chSuccess <- successes:
  157. successes = 0 // reset counter for the next iteration
  158. continue
  159. case <-ctx.Done():
  160. return
  161. default:
  162. }
  163. }
  164. }
  165. }