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.

218 lines
5.8 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "strings"
  8. "time"
  9. "github.com/go-kit/kit/log/term"
  10. metrics "github.com/rcrowley/go-metrics"
  11. "text/tabwriter"
  12. tmrpc "github.com/tendermint/tendermint/rpc/client"
  13. "github.com/tendermint/tmlibs/log"
  14. )
  15. var version = "0.3.0"
  16. var logger = log.NewNopLogger()
  17. type statistics struct {
  18. TxsThroughput metrics.Histogram `json:"txs_per_sec"`
  19. BlocksThroughput metrics.Histogram `json:"blocks_per_sec"`
  20. }
  21. func main() {
  22. var duration, txsRate, connections int
  23. var verbose bool
  24. var outputFormat, broadcastTxMethod string
  25. flag.IntVar(&connections, "c", 1, "Connections to keep open per endpoint")
  26. flag.IntVar(&duration, "T", 10, "Exit after the specified amount of time in seconds")
  27. flag.IntVar(&txsRate, "r", 1000, "Txs per second to send in a connection")
  28. flag.StringVar(&outputFormat, "output-format", "plain", "Output format: plain or json")
  29. flag.StringVar(&broadcastTxMethod, "broadcast-tx-method", "async", "Broadcast method: async (no guarantees; fastest), sync (ensures tx is checked) or commit (ensures tx is checked and committed; slowest)")
  30. flag.BoolVar(&verbose, "v", false, "Verbose output")
  31. flag.Usage = func() {
  32. fmt.Println(`Tendermint blockchain benchmarking tool.
  33. Usage:
  34. tm-bench [-c 1] [-T 10] [-r 1000] [endpoints] [-output-format <plain|json> [-broadcast-tx-method <async|sync|commit>]]
  35. Examples:
  36. tm-bench localhost:46657`)
  37. fmt.Println("Flags:")
  38. flag.PrintDefaults()
  39. }
  40. flag.Parse()
  41. if flag.NArg() == 0 {
  42. flag.Usage()
  43. os.Exit(1)
  44. }
  45. if verbose {
  46. if outputFormat == "json" {
  47. fmt.Fprintln(os.Stderr, "Verbose mode not supported with json output.")
  48. os.Exit(1)
  49. }
  50. // Color errors red
  51. colorFn := func(keyvals ...interface{}) term.FgBgColor {
  52. for i := 1; i < len(keyvals); i += 2 {
  53. if _, ok := keyvals[i].(error); ok {
  54. return term.FgBgColor{Fg: term.White, Bg: term.Red}
  55. }
  56. }
  57. return term.FgBgColor{}
  58. }
  59. logger = log.NewTMLoggerWithColorFn(log.NewSyncWriter(os.Stdout), colorFn)
  60. fmt.Printf("Running %ds test @ %s\n", duration, flag.Arg(0))
  61. }
  62. if broadcastTxMethod != "async" && broadcastTxMethod != "sync" && broadcastTxMethod != "commit" {
  63. fmt.Fprintln(os.Stderr, "broadcast-tx-method should be either 'sync', 'async' or 'commit'.")
  64. os.Exit(1)
  65. }
  66. endpoints := strings.Split(flag.Arg(0), ",")
  67. client := tmrpc.NewHTTP(endpoints[0], "/websocket")
  68. minHeight := latestBlockHeight(client)
  69. logger.Info("Latest block height", "h", minHeight)
  70. // record time start
  71. timeStart := time.Now()
  72. logger.Info("Time started", "t", timeStart)
  73. transacters := startTransacters(endpoints, connections, txsRate, "broadcast_tx_"+broadcastTxMethod)
  74. select {
  75. case <-time.After(time.Duration(duration) * time.Second):
  76. for _, t := range transacters {
  77. t.Stop()
  78. }
  79. timeStop := time.Now()
  80. logger.Info("Time stopped", "t", timeStop)
  81. stats := calculateStatistics(client, minHeight, timeStart, timeStop)
  82. printStatistics(stats, outputFormat)
  83. return
  84. }
  85. }
  86. func latestBlockHeight(client tmrpc.Client) int64 {
  87. status, err := client.Status()
  88. if err != nil {
  89. fmt.Fprintln(os.Stderr, err)
  90. os.Exit(1)
  91. }
  92. return status.SyncInfo.LatestBlockHeight
  93. }
  94. func calculateStatistics(client tmrpc.Client, minHeight int64, timeStart, timeStop time.Time) *statistics {
  95. stats := &statistics{
  96. BlocksThroughput: metrics.NewHistogram(metrics.NewUniformSample(1000)),
  97. TxsThroughput: metrics.NewHistogram(metrics.NewUniformSample(1000)),
  98. }
  99. // get blocks between minHeight and last height
  100. info, err := client.BlockchainInfo(minHeight, 0)
  101. if err != nil {
  102. fmt.Fprintln(os.Stderr, err)
  103. os.Exit(1)
  104. }
  105. numBlocksPerSec := make(map[int64]int64)
  106. numTxsPerSec := make(map[int64]int64)
  107. for _, blockMeta := range info.BlockMetas {
  108. // check if block was created after timeStart
  109. if blockMeta.Header.Time.Before(timeStart) {
  110. continue
  111. }
  112. // check if block was created before timeStop
  113. if blockMeta.Header.Time.After(timeStop) {
  114. break
  115. }
  116. sec := secondsSinceTimeStart(timeStart, blockMeta.Header.Time)
  117. // increase number of blocks for that second
  118. if _, ok := numBlocksPerSec[sec]; !ok {
  119. numBlocksPerSec[sec] = 0
  120. }
  121. numBlocksPerSec[sec]++
  122. // increase number of txs for that second
  123. if _, ok := numTxsPerSec[sec]; !ok {
  124. numTxsPerSec[sec] = 0
  125. }
  126. numTxsPerSec[sec] += blockMeta.Header.NumTxs
  127. }
  128. for _, n := range numBlocksPerSec {
  129. stats.BlocksThroughput.Update(n)
  130. }
  131. for _, n := range numTxsPerSec {
  132. stats.TxsThroughput.Update(n)
  133. }
  134. return stats
  135. }
  136. func secondsSinceTimeStart(timeStart, timePassed time.Time) int64 {
  137. return int64(timePassed.Sub(timeStart).Seconds())
  138. }
  139. func startTransacters(endpoints []string, connections, txsRate int, broadcastTxMethod string) []*transacter {
  140. transacters := make([]*transacter, len(endpoints))
  141. for i, e := range endpoints {
  142. t := newTransacter(e, connections, txsRate, broadcastTxMethod)
  143. t.SetLogger(logger)
  144. if err := t.Start(); err != nil {
  145. fmt.Fprintln(os.Stderr, err)
  146. os.Exit(1)
  147. }
  148. transacters[i] = t
  149. }
  150. return transacters
  151. }
  152. func printStatistics(stats *statistics, outputFormat string) {
  153. if outputFormat == "json" {
  154. result, err := json.Marshal(struct {
  155. TxsThroughput float64 `json:"txs_per_sec_avg"`
  156. BlocksThroughput float64 `json:"blocks_per_sec_avg"`
  157. }{stats.TxsThroughput.Mean(), stats.BlocksThroughput.Mean()})
  158. if err != nil {
  159. fmt.Fprintln(os.Stderr, err)
  160. os.Exit(1)
  161. }
  162. fmt.Println(string(result))
  163. } else {
  164. w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', 0)
  165. fmt.Fprintln(w, "Stats\tAvg\tStdDev\tMax\t")
  166. fmt.Fprintln(w, fmt.Sprintf("Txs/sec\t%.0f\t%.0f\t%d\t",
  167. stats.TxsThroughput.Mean(),
  168. stats.TxsThroughput.StdDev(),
  169. stats.TxsThroughput.Max()))
  170. fmt.Fprintln(w, fmt.Sprintf("Blocks/sec\t%.3f\t%.3f\t%d\t",
  171. stats.BlocksThroughput.Mean(),
  172. stats.BlocksThroughput.StdDev(),
  173. stats.BlocksThroughput.Max()))
  174. w.Flush()
  175. }
  176. }