diff --git a/consensus/istanbul/core/roundstate_db.go b/consensus/istanbul/core/roundstate_db.go index c256cabd78..3ac0f1c0cf 100644 --- a/consensus/istanbul/core/roundstate_db.go +++ b/consensus/istanbul/core/roundstate_db.go @@ -28,6 +28,7 @@ import ( "github.com/celo-org/celo-blockchain/common/task" "github.com/celo-org/celo-blockchain/consensus/istanbul" "github.com/celo-org/celo-blockchain/log" + "github.com/celo-org/celo-blockchain/metrics" "github.com/celo-org/celo-blockchain/rlp" "github.com/syndtr/goleveldb/leveldb" lvlerrors "github.com/syndtr/goleveldb/leveldb/errors" @@ -66,7 +67,11 @@ type roundStateDBImpl struct { db *leveldb.DB stopGarbageCollector task.StopFn opts RoundStateDBOptions - logger log.Logger + + roundStateRLPMeter metrics.Meter + rlpTimer metrics.Timer + dbSaveTimer metrics.Timer + logger log.Logger } var defaultRoundStateDBOptions = RoundStateDBOptions{ @@ -108,9 +113,12 @@ func newRoundStateDB(path string, opts *RoundStateDBOptions) (RoundStateDB, erro } rsdb := &roundStateDBImpl{ - db: db, - opts: coerceOptions(opts), - logger: logger, + db: db, + opts: coerceOptions(opts), + roundStateRLPMeter: metrics.NewRegisteredMeter("consensus/istanbul/core/roundstate/rlp/size", nil), + rlpTimer: metrics.NewRegisteredTimer("consensus/istanbul/core/roundstate/rlp/time", nil), + dbSaveTimer: metrics.NewRegisteredTimer("consensus/istanbul/core/roundstate/db/save/time", nil), + logger: logger, } if rsdb.opts.withGarbageCollector { @@ -283,12 +291,18 @@ func (rsdb *roundStateDBImpl) UpdateLastRoundState(rs RoundState) error { logger := rsdb.logger.New("func", "UpdateLastRoundState") viewKey := view2Key(rs.View()) + // Encode and measure time + before := time.Now() entryBytes, err := rlp.EncodeToBytes(rs) + rsdb.rlpTimer.UpdateSince(before) + if err != nil { logger.Error("Failed to save roundState", "reason", "rlp encoding", "err", err) return err } + rsdb.roundStateRLPMeter.Mark(int64(len(entryBytes))) + before = time.Now() batch := new(leveldb.Batch) batch.Put([]byte(lastViewKey), viewKey) batch.Put(viewKey, entryBytes) @@ -297,6 +311,7 @@ func (rsdb *roundStateDBImpl) UpdateLastRoundState(rs RoundState) error { if err != nil { logger.Error("Failed to save roundState", "reason", "levelDB write", "err", err, "func") } + rsdb.dbSaveTimer.UpdateSince(before) return err }