Skip to content

Commit

Permalink
Add gas processing speed to logging (#10)
Browse files Browse the repository at this point in the history
* Add time taken and gas rate to log

* Update backend_impl.go
  • Loading branch information
wcgcyx authored Nov 17, 2024
1 parent ed33ce5 commit fd21b31
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion backend/backend_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package backend

import (
"context"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fd21b31

Please sign in to comment.