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.

185 lines
4.8 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. if rate < 2 {
  58. logger.Error("transaction throughput was low",
  59. "rate", rate)
  60. }
  61. return nil
  62. }
  63. }
  64. }
  65. // loadGenerate generates jobs until the context is canceled.
  66. //
  67. // The chTx has multiple consumers, thus the rate limiting of the load
  68. // generation is primarily the result of backpressure from the
  69. // broadcast transaction, though there is still some timer-based
  70. // limiting.
  71. func loadGenerate(ctx context.Context, chTx chan<- types.Tx, size int64) {
  72. timer := time.NewTimer(0)
  73. defer timer.Stop()
  74. defer close(chTx)
  75. for {
  76. select {
  77. case <-ctx.Done():
  78. return
  79. case <-timer.C:
  80. }
  81. // We keep generating the same 100 keys over and over, with different values.
  82. // This gives a reasonable load without putting too much data in the app.
  83. id := rand.Int63() % 100 // nolint: gosec
  84. bz := make([]byte, size)
  85. _, err := rand.Read(bz) // nolint: gosec
  86. if err != nil {
  87. panic(fmt.Sprintf("Failed to read random bytes: %v", err))
  88. }
  89. tx := types.Tx(fmt.Sprintf("load-%X=%x", id, bz))
  90. select {
  91. case <-ctx.Done():
  92. return
  93. case chTx <- tx:
  94. // sleep for a bit before sending the
  95. // next transaction.
  96. waitTime := (25 * time.Millisecond) + time.Duration(rand.Int63n(int64(750*time.Millisecond))) // nolint: gosec
  97. timer.Reset(waitTime)
  98. }
  99. }
  100. }
  101. // loadProcess processes transactions
  102. func loadProcess(ctx context.Context, testnet *e2e.Testnet, chTx <-chan types.Tx, chSuccess chan<- int) {
  103. // Each worker gets its own client to each usable node, which
  104. // allows for some concurrency while still bounding it.
  105. clients := make([]*rpchttp.HTTP, 0, len(testnet.Nodes))
  106. for idx := range testnet.Nodes {
  107. // Construct a list of usable nodes for the creating
  108. // load. Don't send load through seed nodes because
  109. // they do not provide the RPC endpoints required to
  110. // broadcast transaction.
  111. if testnet.Nodes[idx].Mode == e2e.ModeSeed {
  112. continue
  113. }
  114. client, err := testnet.Nodes[idx].Client()
  115. if err != nil {
  116. continue
  117. }
  118. clients = append(clients, client)
  119. }
  120. if len(clients) == 0 {
  121. panic("no clients to process load")
  122. }
  123. // Put the clients in a ring so they can be used in a
  124. // round-robin fashion.
  125. clientRing := ring.New(len(clients))
  126. for idx := range clients {
  127. clientRing.Value = clients[idx]
  128. clientRing = clientRing.Next()
  129. }
  130. successes := 0
  131. for {
  132. select {
  133. case <-ctx.Done():
  134. return
  135. case tx := <-chTx:
  136. clientRing = clientRing.Next()
  137. client := clientRing.Value.(*rpchttp.HTTP)
  138. if status, err := client.Status(ctx); err != nil {
  139. continue
  140. } else if status.SyncInfo.CatchingUp {
  141. continue
  142. }
  143. if _, err := client.BroadcastTxSync(ctx, tx); err != nil {
  144. continue
  145. }
  146. successes++
  147. select {
  148. case chSuccess <- successes:
  149. successes = 0 // reset counter for the next iteration
  150. continue
  151. case <-ctx.Done():
  152. return
  153. default:
  154. }
  155. }
  156. }
  157. }