From 17fe3fd35ade7c0c50bbb8d7a65fd68d2e6e6c84 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Tue, 4 Sep 2018 13:11:50 -0700 Subject: [PATCH] Change stats reporting to account for state block --- consensus/consensus_leader.go | 66 ++++++++++++++++++----------------- node/node_handler.go | 43 +++++++++++++++-------- 2 files changed, 62 insertions(+), 47 deletions(-) diff --git a/consensus/consensus_leader.go b/consensus/consensus_leader.go index 4ddc60f38..6ec740564 100644 --- a/consensus/consensus_leader.go +++ b/consensus/consensus_leader.go @@ -404,39 +404,41 @@ func (consensus *Consensus) verifyResponse(commitments *map[uint16]kyber.Point, } func (consensus *Consensus) reportMetrics(block blockchain.Block) { - endTime := time.Now() - timeElapsed := endTime.Sub(startTime) - numOfTxs := block.NumTransactions - tps := float64(numOfTxs) / timeElapsed.Seconds() - consensus.Log.Info("TPS Report", - "numOfTXs", numOfTxs, - "startTime", startTime, - "endTime", endTime, - "timeElapsed", timeElapsed, - "TPS", tps, - "consensus", consensus) - - // Post metrics - URL := "http://localhost:3000/report" - txHashes := []string{} - for i := 1; i <= 3; i++ { - if len(block.TransactionIds)-i >= 0 { - txHashes = append(txHashes, hex.EncodeToString(block.TransactionIds[len(block.TransactionIds)-i][:])) + if !block.IsStateBlock() { // Skip state block stats + endTime := time.Now() + timeElapsed := endTime.Sub(startTime) + numOfTxs := block.NumTransactions + tps := float64(numOfTxs) / timeElapsed.Seconds() + consensus.Log.Info("TPS Report", + "numOfTXs", numOfTxs, + "startTime", startTime, + "endTime", endTime, + "timeElapsed", timeElapsed, + "TPS", tps, + "consensus", consensus) + + // Post metrics + URL := "http://localhost:3000/report" + txHashes := []string{} + for i := 1; i <= 3; i++ { + if len(block.TransactionIds)-i >= 0 { + txHashes = append(txHashes, hex.EncodeToString(block.TransactionIds[len(block.TransactionIds)-i][:])) + } + } + form := url.Values{ + "key": {consensus.pubKey.String()}, + "tps": {strconv.FormatFloat(tps, 'f', 2, 64)}, + "txCount": {strconv.Itoa(int(numOfTxs))}, + "nodeCount": {strconv.Itoa(len(consensus.validators) + 1)}, + "latestBlockHash": {hex.EncodeToString(consensus.blockHash[:])}, + "latestTxHashes": txHashes, + "blockLatency": {strconv.Itoa(int(timeElapsed / time.Millisecond))}, } - } - form := url.Values{ - "key": {consensus.pubKey.String()}, - "tps": {strconv.FormatFloat(tps, 'f', 2, 64)}, - "txCount": {strconv.Itoa(int(numOfTxs))}, - "nodeCount": {strconv.Itoa(len(consensus.validators) + 1)}, - "latestBlockHash": {hex.EncodeToString(consensus.blockHash[:])}, - "latestTxHashes": txHashes, - "blockLatency": {strconv.Itoa(int(timeElapsed / time.Millisecond))}, - } - body := bytes.NewBufferString(form.Encode()) - rsp, err := http.Post(URL, "application/x-www-form-urlencoded", body) - if err == nil { - defer rsp.Body.Close() + body := bytes.NewBufferString(form.Encode()) + rsp, err := http.Post(URL, "application/x-www-form-urlencoded", body) + if err == nil { + defer rsp.Body.Close() + } } } diff --git a/node/node_handler.go b/node/node_handler.go index 8545c09da..dd6008427 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -21,7 +21,7 @@ const ( // The max number of transaction per a block. MaxNumberOfTransactionsPerBlock = 3000 // The number of blocks allowed before generating state block - NumBlocksBeforeStateBlock = 10 + NumBlocksBeforeStateBlock = 100 ) // NodeHandler handles a new incoming connection. @@ -105,25 +105,38 @@ func (node *Node) NodeHandler(conn net.Conn) { avgBlockSizeInBytes := 0 txCount := 0 + blockCount := 0 + totalTxCount := 0 + totalBlockCount := 0 avgTxSize := 0 for _, block := range node.blockchain.Blocks { - byteBuffer := bytes.NewBuffer([]byte{}) - encoder := gob.NewEncoder(byteBuffer) - encoder.Encode(block) - avgBlockSizeInBytes += len(byteBuffer.Bytes()) - - txCount += len(block.Transactions) - - byteBuffer = bytes.NewBuffer([]byte{}) - encoder = gob.NewEncoder(byteBuffer) - encoder.Encode(block.Transactions) - avgTxSize += len(byteBuffer.Bytes()) + if block.IsStateBlock() { + totalTxCount += int(block.State.NumTransactions) + totalBlockCount += int(block.State.NumBlocks) + } else { + byteBuffer := bytes.NewBuffer([]byte{}) + encoder := gob.NewEncoder(byteBuffer) + encoder.Encode(block) + avgBlockSizeInBytes += len(byteBuffer.Bytes()) + + txCount += len(block.Transactions) + blockCount += 1 + totalTxCount += len(block.TransactionIds) + totalBlockCount += 1 + + byteBuffer = bytes.NewBuffer([]byte{}) + encoder = gob.NewEncoder(byteBuffer) + encoder.Encode(block.Transactions) + avgTxSize += len(byteBuffer.Bytes()) + } + } + if blockCount != 0 { + avgBlockSizeInBytes = avgBlockSizeInBytes / blockCount + avgTxSize = avgTxSize / txCount } - avgBlockSizeInBytes = avgBlockSizeInBytes / len(node.blockchain.Blocks) - avgTxSize = avgTxSize / txCount - node.log.Debug("Blockchain Report", "numBlocks", len(node.blockchain.Blocks), "avgBlockSize", avgBlockSizeInBytes, "numTxs", txCount, "avgTxSzie", avgTxSize) + node.log.Debug("Blockchain Report", "totalNumBlocks", totalBlockCount, "avgBlockSizeInCurrentEpoch", avgBlockSizeInBytes, "totalNumTxs", totalTxCount, "avgTxSzieInCurrentEpoch", avgTxSize) os.Exit(0) }