Browse Source

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
pull/1943/head
Greg Szabo 7 years ago
committed by Anton Kaliaev
parent
commit
45a7ae2e62
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 37 additions and 8 deletions
  1. +13
    -1
      tm-bench/main.go
  2. +24
    -7
      tm-bench/transacter.go

+ 13
- 1
tm-bench/main.go View File

@ -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)


+ 24
- 7
tm-bench/transacter.go View File

@ -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


Loading…
Cancel
Save