From 45a7ae2e62b19ca752c066a4285827f5668be584 Mon Sep 17 00:00:00 2001 From: Greg Szabo Date: Wed, 23 Aug 2017 10:57:14 -0400 Subject: [PATCH] Fixes to tm-bench transaction content - Initialize random with current time as seed so transaction messages are different at every run - Added hash of hostname to make transactions coming from different hosts different in every case - Added current time to transaction to make sure that messages are different even if random numbers are the same in subsequent runs - Shortened the transaction size to 64 bytes from the original 250 --- tm-bench/main.go | 14 +++++++++++++- tm-bench/transacter.go | 31 ++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/tm-bench/main.go b/tm-bench/main.go index 686a6e13f..f0cbaead8 100644 --- a/tm-bench/main.go +++ b/tm-bench/main.go @@ -14,6 +14,8 @@ import ( tmtypes "github.com/tendermint/tendermint/types" "github.com/tendermint/tmlibs/log" "github.com/tendermint/tools/tm-monitor/monitor" + "math/rand" + "crypto/md5" ) var version = "0.1.0" @@ -74,6 +76,8 @@ Examples: blockCh := make(chan tmtypes.Header, 100) blockLatencyCh := make(chan float64, 100) + rand.Seed(time.Now().Unix()) + nodes := startNodes(endpoints, blockCh, blockLatencyCh) transacters := startTransacters(endpoints, connections, txsRate) @@ -138,10 +142,18 @@ func startNodes(endpoints []string, blockCh chan<- tmtypes.Header, blockLatencyC } 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)) + } + transacters := make([]*transacter, len(endpoints)) for i, e := range endpoints { - t := newTransacter(e, connections, txsRate) + t := newTransacter(e, connections, txsRate, hostHash) t.SetLogger(logger) if err := t.Start(); err != nil { fmt.Println(err) diff --git a/tm-bench/transacter.go b/tm-bench/transacter.go index 894d133f0..25e7442a1 100644 --- a/tm-bench/transacter.go +++ b/tm-bench/transacter.go @@ -30,6 +30,7 @@ type transacter struct { Target string Rate int Connections int + HostHash [16]byte conns []*websocket.Conn wg sync.WaitGroup @@ -38,11 +39,12 @@ type transacter struct { logger log.Logger } -func newTransacter(target string, connections int, rate int) *transacter { +func newTransacter(target string, connections int, rate int, hosthash [16]byte) *transacter { return &transacter{ Target: target, Rate: rate, Connections: connections, + HostHash: hosthash, conns: make([]*websocket.Conn, connections), logger: log.NewNopLogger(), } @@ -136,7 +138,7 @@ func (t *transacter) sendLoop(connIndex int) { for i := 0; i < t.Rate; i++ { // each transaction embeds connection index and tx number - tx := generateTx(connIndex, txNumber) + tx := generateTx(connIndex, txNumber, t.HostHash) paramsJson, err := json.Marshal(map[string]interface{}{"tx": hex.EncodeToString(tx)}) if err != nil { fmt.Printf("failed to encode params: %v\n", err) @@ -189,11 +191,26 @@ func connect(host string) (*websocket.Conn, *http.Response, error) { return websocket.DefaultDialer.Dial(u.String(), nil) } -func generateTx(a int, b int) []byte { - tx := make([]byte, 250) - binary.PutUvarint(tx[:32], uint64(a)) - binary.PutUvarint(tx[32:64], uint64(b)) - if _, err := rand.Read(tx[234:]); err != nil { +func generateTx(a int, b int, hosthash [16]byte) []byte { + // 64 byte transaction + tx := make([]byte, 64) + + // 0-8 connection number + binary.PutUvarint(tx[:8], uint64(a)) + + // 8-16 transaction number + binary.PutUvarint(tx[8:16], uint64(b)) + + // 16-32 hostname hash + for i:=0; i < 16 ; i++ { + tx[16+i] = hosthash[i] + } + + // 32-40 current time + PutUvarint(tx[32:40], uint64(time.Now().Unix())) + + // 40-64 random data + if _, err := rand.Read(tx[40:]); err != nil { panic(errors.Wrap(err, "failed to generate transaction")) } return tx