From ef4c4a569eec4f5835b912ada32787611cdd61e8 Mon Sep 17 00:00:00 2001 From: David Boehm Date: Fri, 30 Aug 2024 08:02:09 -0700 Subject: [PATCH 1/3] tests --- x/merkledb/db_test.go | 91 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) diff --git a/x/merkledb/db_test.go b/x/merkledb/db_test.go index 1ef015f8db1b..2be02b9b8fbd 100644 --- a/x/merkledb/db_test.go +++ b/x/merkledb/db_test.go @@ -8,6 +8,8 @@ import ( "context" "encoding/binary" "fmt" + "github.com/ava-labs/avalanchego/database/leveldb" + "github.com/ava-labs/avalanchego/utils/logging" "math/rand" "slices" "strconv" @@ -45,8 +47,8 @@ func newDefaultConfig() Config { Hasher: DefaultHasher, RootGenConcurrency: 0, HistoryLength: defaultHistoryLength, - ValueNodeCacheSize: units.MiB, - IntermediateNodeCacheSize: units.MiB, + ValueNodeCacheSize: 10 * units.MiB, + IntermediateNodeCacheSize: 10 * units.MiB, IntermediateWriteBufferSize: units.KiB, IntermediateWriteBatchSize: 256 * units.KiB, Reg: prometheus.NewRegistry(), @@ -1337,6 +1339,91 @@ func TestCrashRecovery(t *testing.T) { require.Equal(expectedRoot, rootAfterRecovery) } +func BenchmarkLevelDBCommitView(b *testing.B) { + b.Run("100k in 1", func(b *testing.B) { + for i := 0; i < b.N; i++ { + CommitToLevelDB(b, 100_000, 1) + } + }) + b.Run("500k in 5", func(b *testing.B) { + for i := 0; i < b.N; i++ { + CommitToLevelDB(b, 100_000, 5) + } + }) + b.Run("1m in 10", func(b *testing.B) { + for i := 0; i < b.N; i++ { + CommitToLevelDB(b, 100_000, 10) + } + }) +} + +func BenchmarkMemDBCommitView(b *testing.B) { + b.Run("100k in 1", func(b *testing.B) { + for i := 0; i < b.N; i++ { + CommitToMemDB(b, 100_000, 1) + } + }) + b.Run("500k in 5", func(b *testing.B) { + for i := 0; i < b.N; i++ { + CommitToMemDB(b, 100_000, 5) + } + }) + b.Run("1m in 10", func(b *testing.B) { + for i := 0; i < b.N; i++ { + CommitToMemDB(b, 100_000, 10) + } + }) +} + +func CommitToLevelDB(b *testing.B, batchSize int, batchCount int) { + baseDB, err := leveldb.New( + b.TempDir()+fmt.Sprintf("/%d/%d", batchSize, batchCount), + nil, + logging.NoLog{}, + prometheus.NewRegistry(), + ) + require.NoError(b, err) + db, err := newDatabase( + context.Background(), + baseDB, + newDefaultConfig(), + &mockMetrics{}, + ) + require.NoError(b, err) + for batchNum := 0; batchNum < batchCount; batchNum++ { + CommitBatch(b, db, batchSize, int64(batchNum)) + } + require.NoError(b, db.Close()) +} + +func CommitToMemDB(b *testing.B, batchSize int, batchCount int) { + db, err := getBasicDB() + require.NoError(b, err) + for batchNum := 0; batchNum < batchCount; batchNum++ { + CommitBatch(b, db, batchSize, int64(batchNum)) + } + require.NoError(b, db.Close()) +} + +func CommitBatch(b *testing.B, db *merkleDB, batchSize int, seed int64) { + ops := make([]database.BatchOp, batchSize) + r := rand.New(rand.NewSource(seed)) + for i := range ops { + size := r.Intn(10) + ops[i] = database.BatchOp{ + Key: make([]byte, size), + Value: make([]byte, size), + } + r.Read(ops[i].Key) + r.Read(ops[i].Value) + } + + ctx := context.Background() + view, err := db.NewView(ctx, ViewChanges{BatchOps: ops}) + require.NoError(b, err) + require.NoError(b, view.CommitToDB(ctx)) +} + func BenchmarkCommitView(b *testing.B) { db, err := getBasicDB() require.NoError(b, err) From 3668619f338f1df86c41b9f83e3f088cd86d56d8 Mon Sep 17 00:00:00 2001 From: David Boehm Date: Fri, 30 Aug 2024 10:02:25 -0700 Subject: [PATCH 2/3] Update db_test.go --- x/merkledb/db_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/x/merkledb/db_test.go b/x/merkledb/db_test.go index 2be02b9b8fbd..74d6bb873f1c 100644 --- a/x/merkledb/db_test.go +++ b/x/merkledb/db_test.go @@ -8,14 +8,15 @@ import ( "context" "encoding/binary" "fmt" - "github.com/ava-labs/avalanchego/database/leveldb" - "github.com/ava-labs/avalanchego/utils/logging" "math/rand" "slices" "strconv" "testing" "time" + "github.com/ava-labs/avalanchego/database/leveldb" + "github.com/ava-labs/avalanchego/utils/logging" + "github.com/prometheus/client_golang/prometheus" "github.com/stretchr/testify/require" @@ -1409,10 +1410,10 @@ func CommitBatch(b *testing.B, db *merkleDB, batchSize int, seed int64) { ops := make([]database.BatchOp, batchSize) r := rand.New(rand.NewSource(seed)) for i := range ops { - size := r.Intn(10) + size := r.Intn(64) + 1 ops[i] = database.BatchOp{ Key: make([]byte, size), - Value: make([]byte, size), + Value: make([]byte, 32), } r.Read(ops[i].Key) r.Read(ops[i].Value) From 99371323d83537ec7199d14bc82ab20313fffdb8 Mon Sep 17 00:00:00 2001 From: David Boehm Date: Fri, 30 Aug 2024 10:06:03 -0700 Subject: [PATCH 3/3] Update db_test.go --- x/merkledb/db_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/merkledb/db_test.go b/x/merkledb/db_test.go index 74d6bb873f1c..adb5855ffb4c 100644 --- a/x/merkledb/db_test.go +++ b/x/merkledb/db_test.go @@ -1408,7 +1408,7 @@ func CommitToMemDB(b *testing.B, batchSize int, batchCount int) { func CommitBatch(b *testing.B, db *merkleDB, batchSize int, seed int64) { ops := make([]database.BatchOp, batchSize) - r := rand.New(rand.NewSource(seed)) + r := rand.New(rand.NewSource(seed)) // #nosec G404 for i := range ops { size := r.Intn(64) + 1 ops[i] = database.BatchOp{