Add timer and counter for batched write operations. (#337)

* Add timer and counter for batched write operations.

* Tweak comment
This commit is contained in:
Thomas E Lackey 2023-03-14 22:32:47 -05:00 committed by GitHub
parent 31abed2ab3
commit 3b884eae30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -67,6 +67,8 @@ type Database struct {
putTimer metrics.Timer // Timer/counter for measuring time and invocations of Put(). putTimer metrics.Timer // Timer/counter for measuring time and invocations of Put().
deleteTimer metrics.Timer // Timer/counter for measuring time and invocations of Delete(). deleteTimer metrics.Timer // Timer/counter for measuring time and invocations of Delete().
hasTimer metrics.Timer // Timer/counter for measuring time and invocations of Has(). 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 compTimeMeter metrics.Meter // Meter for measuring the total time spent in database compaction
compReadMeter metrics.Meter // Meter for measuring the data read during 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.putTimer = metrics.NewRegisteredTimer(namespace+"db/put/time", nil)
ldb.deleteTimer = metrics.NewRegisteredTimer(namespace+"db/delete/time", nil) ldb.deleteTimer = metrics.NewRegisteredTimer(namespace+"db/delete/time", nil)
ldb.hasTimer = metrics.NewRegisteredTimer(namespace+"db/has/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 // Start up the metrics gathering and return
go ldb.meter(metricsGatheringInterval) go ldb.meter(metricsGatheringInterval)
@ -234,6 +238,8 @@ func (db *Database) NewBatch() ethdb.Batch {
return &batch{ return &batch{
db: db.db, db: db.db,
b: new(leveldb.Batch), b: new(leveldb.Batch),
writeTimer: &db.batchWriteTimer,
itemCounter: &db.batchItemCounter,
} }
} }
@ -242,6 +248,8 @@ func (db *Database) NewBatchWithSize(size int) ethdb.Batch {
return &batch{ return &batch{
db: db.db, db: db.db,
b: leveldb.MakeBatch(size), b: leveldb.MakeBatch(size),
writeTimer: &db.batchWriteTimer,
itemCounter: &db.batchItemCounter,
} }
} }
@ -496,6 +504,8 @@ type batch struct {
db *leveldb.DB db *leveldb.DB
b *leveldb.Batch b *leveldb.Batch
size int size int
writeTimer *metrics.Timer
itemCounter *metrics.Counter
} }
// Put inserts the given value into the batch for later committing. // 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. // Write flushes any accumulated data to disk.
func (b *batch) Write() error { 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) return b.db.Write(b.b, nil)
} }