Skip to content

Commit

Permalink
batch writes performance
Browse files Browse the repository at this point in the history
  • Loading branch information
freemanzMrojo committed Jan 11, 2025
1 parent 5505c91 commit e6ac492
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 8 additions & 1 deletion muxdb/engine/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions muxdb/engine/metrics.go
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/lgpl-3.0.html>

// 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)
)

0 comments on commit e6ac492

Please sign in to comment.