diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-07-31 11:54:48 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-31 11:54:48 +0800 |
commit | 36d069fbecaf270974c920e6e27f0def22936fb9 (patch) | |
tree | add599d24654346d974f29e89c709a0890d57ef8 /simulation/verification.go | |
parent | d99bc645ee0138c6ea628f77c6475eec1a61253e (diff) | |
download | tangerine-consensus-36d069fbecaf270974c920e6e27f0def22936fb9.tar tangerine-consensus-36d069fbecaf270974c920e6e27f0def22936fb9.tar.gz tangerine-consensus-36d069fbecaf270974c920e6e27f0def22936fb9.tar.bz2 tangerine-consensus-36d069fbecaf270974c920e6e27f0def22936fb9.tar.lz tangerine-consensus-36d069fbecaf270974c920e6e27f0def22936fb9.tar.xz tangerine-consensus-36d069fbecaf270974c920e6e27f0def22936fb9.tar.zst tangerine-consensus-36d069fbecaf270974c920e6e27f0def22936fb9.zip |
Print confirmed blocks per second when Peer Server stopped. (#22)
Diffstat (limited to 'simulation/verification.go')
-rw-r--r-- | simulation/verification.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/simulation/verification.go b/simulation/verification.go index 0e111be..bc1afc4 100644 --- a/simulation/verification.go +++ b/simulation/verification.go @@ -21,17 +21,28 @@ import ( "container/heap" "log" "math" + "time" "github.com/dexon-foundation/dexon-consensus-core/common" "github.com/dexon-foundation/dexon-consensus-core/core/types" ) +type timeStamp struct { + time time.Time + length int +} + +type totalOrderStatus struct { + blockReceive []timeStamp +} + // TotalOrderResult is the object maintaining peer's result of // Total Ordering Algorithm. type TotalOrderResult struct { hashList common.Hashes curID int pendingBlockList PendingBlockList + status totalOrderStatus } // PeerTotalOrder stores the TotalOrderResult of each validator. @@ -47,6 +58,11 @@ func NewTotalOrderResult() *TotalOrderResult { // PushBlocks push a BlockList into the TotalOrderResult and return true if // there is new blocks ready for verifiy func (totalOrder *TotalOrderResult) PushBlocks(blocks BlockList) (ready bool) { + totalOrder.status.blockReceive = append(totalOrder.status.blockReceive, + timeStamp{ + time: time.Now(), + length: len(blocks.BlockHash), + }) if blocks.ID != totalOrder.curID { heap.Push(&totalOrder.pendingBlockList, &blocks) return false @@ -65,6 +81,28 @@ func (totalOrder *TotalOrderResult) PushBlocks(blocks BlockList) (ready bool) { return true } +// CalculateBlocksPerSecond calculates the result using status.blockReceive +func (totalOrder *TotalOrderResult) CalculateBlocksPerSecond() float64 { + ts := totalOrder.status.blockReceive + if len(ts) < 2 { + return 0 + } + + diffTime := ts[len(ts)-1].time.Sub(ts[0].time).Seconds() + if diffTime == 0 { + return 0 + } + totalBlocks := 0 + for _, blocks := range ts { + // Blocks received at time zero are confirmed beforehand. + if blocks.time == ts[0].time { + continue + } + totalBlocks += blocks.length + } + return float64(totalBlocks) / diffTime +} + // VerifyTotalOrder verifies if the result of Total Ordering Algorithm // returned by all validators are the same. However, the length of result // of each validators may not be the same, so only the common part is verified. @@ -109,3 +147,10 @@ func VerifyTotalOrder(id types.ValidatorID, } return totalOrder, !hasError, length } + +// LogStatus prints all the status to log. +func LogStatus(peerTotalOrder PeerTotalOrder) { + for vID, totalOrder := range peerTotalOrder { + log.Printf("[Validator %s] BPS: %.6f\n", vID, totalOrder.CalculateBlocksPerSecond()) + } +} |