From 35b671214cae5cca4f1f47efa3b22e0615e30940 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Mon, 8 Oct 2018 17:03:38 +0400 Subject: [PATCH] tools: Refactor tm-bench (#2570) * specify time unit for FlushThrottleTimeout in TestP2PConfig Refs #2555 * [tm-bench] refactor code https://github.com/tendermint/tendermint/pull/2405#pullrequestreview-157166387 --- config/config.go | 2 +- tools/tm-bench/main.go | 35 +++++++++++++++-------------------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/config/config.go b/config/config.go index 1f9ff3e1..f2bac5c6 100644 --- a/config/config.go +++ b/config/config.go @@ -405,7 +405,7 @@ func DefaultP2PConfig() *P2PConfig { func TestP2PConfig() *P2PConfig { cfg := DefaultP2PConfig() cfg.ListenAddress = "tcp://0.0.0.0:36656" - cfg.FlushThrottleTimeout = 10 + cfg.FlushThrottleTimeout = 10 * time.Millisecond cfg.AllowDuplicateIP = true return cfg } diff --git a/tools/tm-bench/main.go b/tools/tm-bench/main.go index a7c427c0..87f12ef3 100644 --- a/tools/tm-bench/main.go +++ b/tools/tm-bench/main.go @@ -4,16 +4,16 @@ import ( "flag" "fmt" "os" + "os/signal" "strings" "sync" + "syscall" "time" "github.com/go-kit/kit/log/term" "github.com/tendermint/tendermint/libs/log" tmrpc "github.com/tendermint/tendermint/rpc/client" - "os/signal" - "syscall" ) var logger = log.NewNopLogger() @@ -53,8 +53,7 @@ Examples: if verbose { if outputFormat == "json" { - fmt.Fprintln(os.Stderr, "Verbose mode not supported with json output.") - os.Exit(1) + printErrorAndExit("Verbose mode not supported with json output.") } // Color errors red colorFn := func(keyvals ...interface{}) term.FgBgColor { @@ -71,21 +70,13 @@ Examples: } if txSize < 40 { - fmt.Fprintln( - os.Stderr, - "The size of a transaction must be greater than or equal to 40.", - ) - os.Exit(1) + printErrorAndExit("The size of a transaction must be greater than or equal to 40.") } if broadcastTxMethod != "async" && broadcastTxMethod != "sync" && broadcastTxMethod != "commit" { - fmt.Fprintln( - os.Stderr, - "broadcast-tx-method should be either 'sync', 'async' or 'commit'.", - ) - os.Exit(1) + printErrorAndExit("broadcast-tx-method should be either 'sync', 'async' or 'commit'.") } var ( @@ -103,10 +94,10 @@ Examples: "broadcast_tx_"+broadcastTxMethod, ) - //catch Interrupt and quit tm-bench + // Quit when interrupted or received SIGTERM. + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt, syscall.SIGTERM) for sig := range c { fmt.Printf("captured %v, exiting...\n", sig) for _, t := range transacters { @@ -116,7 +107,7 @@ Examples: } }() - // Wait until transacters have begun until we get the start time + // Wait until transacters have begun until we get the start time. timeStart := time.Now() logger.Info("Time last transacter started", "t", timeStart) @@ -143,8 +134,7 @@ Examples: durationInt, ) if err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) + printErrorAndExit(err.Error()) } printStatistics(stats, outputFormat) @@ -196,3 +186,8 @@ func startTransacters( return transacters } + +func printErrorAndExit(err string) { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) +}