From 36d069fbecaf270974c920e6e27f0def22936fb9 Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Tue, 31 Jul 2018 11:54:48 +0800 Subject: Print confirmed blocks per second when Peer Server stopped. (#22) --- simulation/peer-server.go | 2 ++ simulation/verification.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/simulation/peer-server.go b/simulation/peer-server.go index 9cace94..1b8432f 100644 --- a/simulation/peer-server.go +++ b/simulation/peer-server.go @@ -217,6 +217,8 @@ func (p *PeerServer) Run(configPath string) { go func() { <-stopServer + LogStatus(p.peerTotalOrder) + log.Printf("Shutting down peerServer.\n") ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() 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()) + } +} -- cgit v1.2.3