From fd21b315b1cf92a3d56e680cd1beb975f4b7d6cf Mon Sep 17 00:00:00 2001 From: Zhenyang Shi Date: Sun, 17 Nov 2024 22:50:59 +1000 Subject: [PATCH] Add gas processing speed to logging (#10) * Add time taken and gas rate to log * Update backend_impl.go --- backend/backend_impl.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/backend/backend_impl.go b/backend/backend_impl.go index e498035..4c0b597 100644 --- a/backend/backend_impl.go +++ b/backend/backend_impl.go @@ -12,6 +12,7 @@ package backend import ( "context" + "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" @@ -41,6 +42,11 @@ type BackendImpl struct { chainHeadFeed event.Feed scope event.SubscriptionScope + + // Perf related stat + blkSinceLastReport uint64 + gasProcessed uint64 + timeTaken time.Duration } // NewBackendImpl creates a new backend. @@ -79,6 +85,7 @@ func (b *BackendImpl) StateArchive() worldstate.LayeredWorldStateArchive { // ImportBlock process and import a block. func (b *BackendImpl) ImportBlock(ctx context.Context, blk *types.Block, prvBlk *types.Block) error { + start := time.Now() // Check if this block has already been imported exists, err := b.bc.HasBlock(ctx, blk.Hash()) if err != nil { @@ -123,7 +130,19 @@ func (b *BackendImpl) ImportBlock(ctx context.Context, blk *types.Block, prvBlk if err != nil { log.Warnf("Fail to set head to %v: %v", blk.Hash(), err.Error()) } - log.Infof("Successfully processed block %v (%v): txn %v gas used %v", blk.NumberU64(), blk.Hash(), len(receipts), gasUsed) + timeTaken := time.Since(start) + log.Infof("Successfully processed block %v (%v): txn %v gas used %v, time taken %v", blk.NumberU64(), blk.Hash(), len(receipts), gasUsed, timeTaken) + // Report gas rate per 10 blocks + b.gasProcessed += gasUsed + b.timeTaken = b.timeTaken + timeTaken + b.blkSinceLastReport++ + if b.blkSinceLastReport >= 10 { + gasRate := float64(b.gasProcessed) / 1e6 / b.timeTaken.Seconds() + log.Infof("Current gas processing speed is %.2f M/s", gasRate) + b.gasProcessed = 0 + b.timeTaken = 0 + b.blkSinceLastReport = 0 + } // Send event b.chainHeadFeed.Send(core.ChainHeadEvent{Block: blk}) return nil