Add timer and counter for batched write operations. #337

Merged
telackey merged 2 commits from telackey/batchtimer into v1.11.2-statediff-v4 2023-03-15 03:32:47 +00:00

View File

@ -67,6 +67,8 @@ type Database struct {
putTimer metrics.Timer // Timer/counter for measuring time and invocations of Put().
deleteTimer metrics.Timer // Timer/counter for measuring time and invocations of Delete().
hasTimer metrics.Timer // Timer/counter for measuring time and invocations of Has().
batchWriteTimer metrics.Timer // Timer/counter for measuring time and invocations of batch writes.
batchItemCounter metrics.Counter // Counter for measuring number of batched items written.
compTimeMeter metrics.Meter // Meter for measuring the total time spent in database compaction
compReadMeter metrics.Meter // Meter for measuring the data read during compaction
@ -155,6 +157,8 @@ func NewCustom(file string, namespace string, customize func(options *opt.Option
ldb.putTimer = metrics.NewRegisteredTimer(namespace+"db/put/time", nil)
ldb.deleteTimer = metrics.NewRegisteredTimer(namespace+"db/delete/time", nil)
ldb.hasTimer = metrics.NewRegisteredTimer(namespace+"db/has/time", nil)
ldb.batchWriteTimer = metrics.NewRegisteredTimer(namespace+"db/batch_write/time", nil)
ldb.batchItemCounter = metrics.NewRegisteredCounter(namespace+"db/batch_item/count", nil)
// Start up the metrics gathering and return
go ldb.meter(metricsGatheringInterval)
@ -234,6 +238,8 @@ func (db *Database) NewBatch() ethdb.Batch {
return &batch{
db: db.db,
b: new(leveldb.Batch),
writeTimer: &db.batchWriteTimer,
itemCounter: &db.batchItemCounter,
}
}
@ -242,6 +248,8 @@ func (db *Database) NewBatchWithSize(size int) ethdb.Batch {
return &batch{
db: db.db,
b: leveldb.MakeBatch(size),
writeTimer: &db.batchWriteTimer,
itemCounter: &db.batchItemCounter,
}
}
@ -496,6 +504,8 @@ type batch struct {
db *leveldb.DB
b *leveldb.Batch
size int
writeTimer *metrics.Timer
itemCounter *metrics.Counter
}
// Put inserts the given value into the batch for later committing.
@ -519,6 +529,12 @@ func (b *batch) ValueSize() int {
// Write flushes any accumulated data to disk.
func (b *batch) Write() error {
if nil != *b.writeTimer {
defer func(start time.Time) { (*b.writeTimer).UpdateSince(start) }(time.Now())
}
if nil != *b.itemCounter {
(*b.itemCounter).Inc(int64(b.size))
}
return b.db.Write(b.b, nil)
}