From e6ac4924735ffb84a68c965549a4ee36499620bd Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Sat, 11 Jan 2025 12:30:43 +0000 Subject: [PATCH] batch writes performance --- muxdb/engine/leveldb.go | 9 ++++++++- muxdb/engine/metrics.go | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 muxdb/engine/metrics.go diff --git a/muxdb/engine/leveldb.go b/muxdb/engine/leveldb.go index b26d7acff..33a1804ac 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,11 +114,16 @@ func (ldb *levelEngine) Bulk() kv.Bulk { return batch } flush := func(minSize int) error { - if batch != nil && len(batch.Dump()) >= minSize { + batchBytesNo := len(batch.Dump()) + if batch != nil && 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 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) +)