Skip to content

Commit 404c50e

Browse files
committed
improve write metric
1 parent 10fffef commit 404c50e

File tree

6 files changed

+25
-18
lines changed

6 files changed

+25
-18
lines changed

v2/cmd/bench/bench.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ $ go run ./cmd snapshot --db /tmp/iavl-v2 --version 1
6969
treeOpts.MetricsProxy = newPrometheusMetricsProxy()
7070
}
7171

72-
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{AddSource: true, Level: slog.LevelDebug}))
72+
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
7373
var multiTree *iavl.MultiTree
7474
if loadSnapshot {
7575
var err error

v2/metrics/metrics.go

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ func (s *StructMetrics) IncrCounter(val float32, keys ...string) {
7979
s.QueryLeafMiss += int64(val)
8080
case "db_write_leaf":
8181
s.WriteLeaves += int64(val)
82+
case "db_write_branch":
83+
s.WriteBranch += int64(val)
8284
}
8385
}
8486

@@ -118,6 +120,7 @@ type DbMetrics struct {
118120
WriteDurations []time.Duration
119121
WriteTime time.Duration
120122
WriteLeaves int64
123+
WriteBranch int64
121124

122125
QueryDurations []time.Duration
123126
QueryTime time.Duration
@@ -192,6 +195,7 @@ func (s *StructMetrics) Add(os *StructMetrics) {
192195
s.WriteDurations = append(s.WriteDurations, os.WriteDurations...)
193196
s.WriteTime += os.WriteTime
194197
s.WriteLeaves += os.WriteLeaves
198+
s.WriteBranch += os.WriteBranch
195199

196200
s.QueryDurations = append(s.QueryDurations, os.QueryDurations...)
197201
s.QueryTime += os.QueryTime

v2/migrate/go.mod

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/cosmos/iavl/v2/migrate
22

3-
go 1.18
3+
go 1.21.0
4+
5+
toolchain go1.23.6
46

57
require (
68
cosmossdk.io/api v0.7.2
@@ -53,4 +55,4 @@ require (
5355
google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c // indirect
5456
google.golang.org/grpc v1.58.3 // indirect
5557
google.golang.org/protobuf v1.31.0 // indirect
56-
)
58+
)

v2/multitree.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (mt *MultiTree) TestBuild(opts *testutil.TreeBuildOptions) (int64, error) {
345345
var (
346346
workingBytes uint64
347347
workingSize int64
348-
writeLeaves int64
348+
writeCount int64
349349
writeTime time.Duration
350350
hashCount int64
351351
)
@@ -356,11 +356,12 @@ func (mt *MultiTree) TestBuild(opts *testutil.TreeBuildOptions) (int64, error) {
356356
}
357357
workingBytes += tr.workingBytes
358358
workingSize += tr.workingSize
359-
writeLeaves += sm.WriteLeaves
360359
writeTime += sm.WriteTime
360+
writeCount += sm.WriteLeaves + sm.WriteBranch
361361
hashCount += sm.TreeHash
362362
sm.WriteDurations = nil
363363
sm.WriteLeaves = 0
364+
sm.WriteBranch = 0
364365
sm.WriteTime = 0
365366
sm.TreeHash = 0
366367
}
@@ -376,9 +377,9 @@ func (mt *MultiTree) TestBuild(opts *testutil.TreeBuildOptions) (int64, error) {
376377

377378
if writeTime > 0 {
378379
fmt.Printf("writes: cnt=%s wr/s=%s dur/wr=%s dur=%s hashes=%s\n",
379-
humanize.Comma(writeLeaves),
380-
humanize.Comma(int64(float64(writeLeaves)/writeTime.Seconds())),
381-
time.Duration(int64(writeTime)/writeLeaves),
380+
humanize.Comma(writeCount),
381+
humanize.Comma(int64(float64(writeCount)/writeTime.Seconds())),
382+
time.Duration(int64(writeTime)/writeCount),
382383
writeTime.Round(time.Millisecond),
383384
humanize.Comma(hashCount),
384385
)

v2/sqlite_batch.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import (
55
"time"
66

77
"github.com/bvinc/go-sqlite-lite/sqlite3"
8+
"github.com/cosmos/iavl/v2/metrics"
89
"github.com/dustin/go-humanize"
910
)
1011

1112
type sqliteBatch struct {
12-
tree *Tree
13-
sql *SqliteDb
14-
size int64
15-
logger Logger
13+
tree *Tree
14+
sql *SqliteDb
15+
size int64
16+
logger Logger
17+
metrics metrics.Proxy
1618

1719
treeCount int64
1820
treeSince time.Time
@@ -147,8 +149,6 @@ func (b *sqliteBatch) treeMaybeCommit(shardID int64) (err error) {
147149
}
148150

149151
func (b *sqliteBatch) saveLeaves() (int64, error) {
150-
var byteCount int64
151-
152152
err := b.newChangeLogBatch()
153153
if err != nil {
154154
return 0, err
@@ -169,7 +169,6 @@ func (b *sqliteBatch) saveLeaves() (int64, error) {
169169
if err != nil {
170170
return 0, err
171171
}
172-
byteCount += int64(len(bz))
173172
if err = b.leafInsert.Exec(leaf.nodeKey.Version(), int(leaf.nodeKey.Sequence()), bz); err != nil {
174173
return 0, err
175174
}
@@ -225,10 +224,10 @@ func (b *sqliteBatch) saveLeaves() (int64, error) {
225224

226225
err = tree.sql.leafWrite.Exec("CREATE UNIQUE INDEX IF NOT EXISTS leaf_idx ON leaf (version, sequence)")
227226
if err != nil {
228-
return byteCount, err
227+
return b.leafCount, err
229228
}
230229

231-
return byteCount, nil
230+
return b.leafCount, nil
232231
}
233232

234233
func (b *sqliteBatch) isCheckpoint() bool {

v2/sqlite_writer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ func (w *sqlWriter) saveTree(tree *Tree) error {
456456
w.leafCh <- saveSig
457457
treeResult := <-w.treeResult
458458
leafResult := <-w.leafResult
459-
tree.metrics.IncrCounter(float32(len(tree.leaves)), metricsNamespace, "db_write_leaf")
459+
tree.metrics.IncrCounter(float32(batch.leafCount), metricsNamespace, "db_write_leaf")
460+
tree.metrics.IncrCounter(float32(batch.treeCount), metricsNamespace, "db_write_branch")
460461

461462
err := errors.Join(treeResult.err, leafResult.err)
462463

0 commit comments

Comments
 (0)