diff --git a/muxdb/engine/leveldb.go b/muxdb/engine/leveldb.go index b26d7acff..47d6f7600 100644 --- a/muxdb/engine/leveldb.go +++ b/muxdb/engine/leveldb.go @@ -8,7 +8,9 @@ package engine import ( "context" "sync" + "time" + "github.com/ethereum/go-ethereum/common/mclock" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/opt" "github.com/syndtr/goleveldb/leveldb/util" @@ -112,14 +114,20 @@ func (ldb *levelEngine) Bulk() kv.Bulk { return batch } flush := func(minSize int) error { - if batch != nil && len(batch.Dump()) >= minSize { - if batch.Len() > 0 { - if err := ldb.db.Write(batch, &writeOpt); err != nil { - return err + if batch != nil { + if batchBytesNo := len(batch.Dump()); batchBytesNo >= minSize { + if batch.Len() > 0 { + startTime := mclock.Now() + if err := ldb.db.Write(batch, &writeOpt); err != nil { + return err + } + metricBatchWriteBytes().Set(int64(batchBytesNo)) + batchWriteElapsed := mclock.Now() - startTime + metricBatchWriteDuration().Observe(time.Duration(batchWriteElapsed).Milliseconds()) } + ldb.batchPool.Put(batch) + batch = nil } - ldb.batchPool.Put(batch) - batch = nil } return nil } diff --git a/muxdb/engine/metrics.go b/muxdb/engine/metrics.go new file mode 100644 index 000000000..bc29baf47 --- /dev/null +++ b/muxdb/engine/metrics.go @@ -0,0 +1,17 @@ +// Copyright (c) 2024 The VeChainThor developers + +// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying +// file LICENSE or + +// Package muxdb implements the storage layer for block-chain. +// It manages instance of merkle-patricia-trie, and general purpose named kv-store. +package engine + +import ( + "github.com/vechain/thor/v2/metrics" +) + +var ( + metricBatchWriteBytes = metrics.LazyLoadGauge("batch_write_bytes") + metricBatchWriteDuration = metrics.LazyLoadHistogram("batch_write_duration_ms", metrics.Bucket10s) +)