tendermint/tm-bench/main.go

185 lines
4.4 KiB
Go
Raw Normal View History

package main
2017-03-13 08:10:51 -07:00
import (
"flag"
"fmt"
"os"
"strings"
2017-03-16 13:56:22 -07:00
"text/tabwriter"
2017-03-13 08:10:51 -07:00
"time"
"github.com/go-kit/kit/log/term"
2017-03-16 13:56:22 -07:00
metrics "github.com/rcrowley/go-metrics"
2017-07-28 15:13:39 -07:00
2017-03-16 13:56:22 -07:00
tmtypes "github.com/tendermint/tendermint/types"
2017-07-28 15:13:39 -07:00
"github.com/tendermint/tmlibs/log"
"github.com/tendermint/tools/tm-monitor/monitor"
"math/rand"
"crypto/md5"
2017-03-13 08:10:51 -07:00
)
var version = "0.1.0"
2017-03-13 08:10:51 -07:00
var logger = log.NewNopLogger()
2017-03-16 13:56:22 -07:00
type statistics struct {
BlockTimeSample metrics.Histogram
TxThroughputSample metrics.Histogram
BlockLatency metrics.Histogram
}
2017-03-13 08:10:51 -07:00
func main() {
2017-03-17 02:13:06 -07:00
var duration, txsRate, connections int
var verbose bool
2017-03-13 08:10:51 -07:00
2017-03-17 02:13:06 -07:00
flag.IntVar(&connections, "c", 1, "Connections to keep open per endpoint")
2017-03-13 08:10:51 -07:00
flag.IntVar(&duration, "T", 10, "Exit after the specified amount of time in seconds")
flag.IntVar(&txsRate, "r", 1000, "Txs per second to send in a connection")
flag.BoolVar(&verbose, "v", false, "Verbose output")
2017-03-13 08:10:51 -07:00
flag.Usage = func() {
2017-03-16 14:12:37 -07:00
fmt.Println(`Tendermint blockchain benchmarking tool.
2017-03-13 08:10:51 -07:00
Usage:
2017-03-17 02:13:06 -07:00
tm-bench [-c 1] [-T 10] [-r 1000] [endpoints]
2017-03-13 08:10:51 -07:00
Examples:
tm-bench localhost:46657`)
fmt.Println("Flags:")
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() == 0 {
flag.Usage()
os.Exit(1)
}
if verbose {
// Color errors red
colorFn := func(keyvals ...interface{}) term.FgBgColor {
for i := 1; i < len(keyvals); i += 2 {
if _, ok := keyvals[i].(error); ok {
return term.FgBgColor{Fg: term.White, Bg: term.Red}
}
}
return term.FgBgColor{}
}
2017-07-28 15:13:39 -07:00
logger = log.NewTMLoggerWithColorFn(log.NewSyncWriter(os.Stdout), colorFn)
}
2017-03-13 08:10:51 -07:00
fmt.Printf("Running %ds test @ %s\n", duration, flag.Arg(0))
endpoints := strings.Split(flag.Arg(0), ",")
2017-03-16 13:56:22 -07:00
blockCh := make(chan tmtypes.Header, 100)
blockLatencyCh := make(chan float64, 100)
rand.Seed(time.Now().Unix())
2017-03-16 13:56:22 -07:00
nodes := startNodes(endpoints, blockCh, blockLatencyCh)
2017-03-17 02:13:06 -07:00
transacters := startTransacters(endpoints, connections, txsRate)
2017-03-16 13:56:22 -07:00
stats := &statistics{
BlockTimeSample: metrics.NewHistogram(metrics.NewUniformSample(1000)),
TxThroughputSample: metrics.NewHistogram(metrics.NewUniformSample(1000)),
BlockLatency: metrics.NewHistogram(metrics.NewUniformSample(1000)),
2017-03-13 08:10:51 -07:00
}
2017-03-16 13:56:22 -07:00
lastBlockHeight := -1
durationTimer := time.After(time.Duration(duration) * time.Second)
ticker := time.NewTicker(1 * time.Second)
var blocks, txs int
for {
select {
case b := <-blockCh:
if lastBlockHeight < b.Height {
blocks++
txs += b.NumTxs
lastBlockHeight = b.Height
}
case l := <-blockLatencyCh:
stats.BlockLatency.Update(int64(l))
case <-ticker.C:
stats.BlockTimeSample.Update(int64(blocks))
stats.TxThroughputSample.Update(int64(txs))
blocks = 0
txs = 0
case <-durationTimer:
for _, t := range transacters {
t.Stop()
}
printStatistics(stats)
for _, n := range nodes {
n.Stop()
}
return
2017-03-13 08:10:51 -07:00
}
}
}
2017-03-16 13:56:22 -07:00
func startNodes(endpoints []string, blockCh chan<- tmtypes.Header, blockLatencyCh chan<- float64) []*monitor.Node {
nodes := make([]*monitor.Node, len(endpoints))
2017-03-13 08:10:51 -07:00
2017-03-16 13:56:22 -07:00
for i, e := range endpoints {
n := monitor.NewNode(e)
2017-07-28 15:13:39 -07:00
n.SetLogger(logger.With("node", e))
2017-03-16 13:56:22 -07:00
n.SendBlocksTo(blockCh)
n.SendBlockLatenciesTo(blockLatencyCh)
if err := n.Start(); err != nil {
fmt.Println(err)
os.Exit(1)
2017-03-13 08:10:51 -07:00
}
2017-03-16 13:56:22 -07:00
nodes[i] = n
2017-03-13 08:10:51 -07:00
}
2017-03-16 13:56:22 -07:00
return nodes
}
2017-03-17 02:13:06 -07:00
func startTransacters(endpoints []string, connections int, txsRate int) []*transacter {
var hostHash [16]byte
if hostName , err := os.Hostname(); err != nil {
hostHash = md5.Sum([]byte("127.0.0.1"))
} else {
hostHash = md5.Sum([]byte(hostName))
}
2017-03-16 13:56:22 -07:00
transacters := make([]*transacter, len(endpoints))
for i, e := range endpoints {
t := newTransacter(e, connections, txsRate, hostHash)
t.SetLogger(logger)
2017-03-16 13:56:22 -07:00
if err := t.Start(); err != nil {
fmt.Println(err)
os.Exit(1)
2017-03-16 13:56:22 -07:00
}
transacters[i] = t
2017-03-13 08:10:51 -07:00
}
2017-03-16 13:56:22 -07:00
return transacters
2017-03-13 08:10:51 -07:00
}
2017-03-16 13:56:22 -07:00
func printStatistics(stats *statistics) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', 0)
fmt.Fprintln(w, "Stats\tAvg\tStdev\tMax\t")
fmt.Fprintln(w, fmt.Sprintf("Block latency\t%.2fms\t%.2fms\t%dms\t",
stats.BlockLatency.Mean()/1000000.0,
stats.BlockLatency.StdDev()/1000000.0,
stats.BlockLatency.Max()/1000000))
fmt.Fprintln(w, fmt.Sprintf("Blocks/sec\t%.3f\t%.3f\t%d\t",
stats.BlockTimeSample.Mean(),
stats.BlockTimeSample.StdDev(),
stats.BlockTimeSample.Max()))
fmt.Fprintln(w, fmt.Sprintf("Txs/sec\t%.0f\t%.0f\t%d\t",
stats.TxThroughputSample.Mean(),
stats.TxThroughputSample.StdDev(),
stats.TxThroughputSample.Max()))
w.Flush()
2017-03-13 08:10:51 -07:00
}