diff --git a/server/v2/stf/branch/bench_test.go b/server/v2/stf/branch/bench_test.go new file mode 100644 index 000000000000..2e0097ba6535 --- /dev/null +++ b/server/v2/stf/branch/bench_test.go @@ -0,0 +1,172 @@ +package branch + +import ( + "testing" + + "cosmossdk.io/core/store" + coretesting "cosmossdk.io/core/testing" +) + +func Benchmark_CacheStack1_Set(b *testing.B) { + bs := makeBranchStack(b, 1) + b.ResetTimer() + b.ReportAllocs() + // test sets + for i := 0; i < b.N; i++ { + err := bs.Set([]byte{0}, []byte{0}) + if err != nil { + b.Fatal(err) + } + } +} + +func Benchmark_CacheStack10_Set(b *testing.B) { + bs := makeBranchStack(b, 10) + b.ResetTimer() + b.ReportAllocs() + // test sets + for i := 0; i < b.N; i++ { + err := bs.Set([]byte{0}, []byte{0}) + if err != nil { + b.Fatal(err) + } + } +} + +func Benchmark_CacheStack100_Set(b *testing.B) { + bs := makeBranchStack(b, 100) + b.ResetTimer() + b.ReportAllocs() + // test sets + for i := 0; i < b.N; i++ { + err := bs.Set([]byte{0}, []byte{0}) + if err != nil { + b.Fatal(err) + } + } +} + +func Benchmark_CacheStack1_Get(b *testing.B) { + bs := makeBranchStack(b, 1) + b.ResetTimer() + b.ReportAllocs() + // test sets + for i := 0; i < b.N; i++ { + _, err := bs.Get([]byte{0}) + if err != nil { + b.Fatal(err) + } + } +} + +func Benchmark_CacheStack10_Get(b *testing.B) { + bs := makeBranchStack(b, 10) + b.ResetTimer() + b.ReportAllocs() + // test sets + for i := 0; i < b.N; i++ { + _, err := bs.Get([]byte{0}) + if err != nil { + b.Fatal(err) + } + } +} + +func Benchmark_CacheStack100_Get(b *testing.B) { + bs := makeBranchStack(b, 100) + b.ResetTimer() + b.ReportAllocs() + // test sets + for i := 0; i < b.N; i++ { + _, err := bs.Get([]byte{0}) + if err != nil { + b.Fatal(err) + } + } +} + +func Benchmark_CacheStack1_Iterate(b *testing.B) { + bs := makeBranchStack(b, 1) + var keySink, valueSink any + + b.ResetTimer() + b.ReportAllocs() + // test sets + for i := 0; i < b.N; i++ { + iter, err := bs.Iterator([]byte{0}, []byte{0}) + if err != nil { + b.Fatal(err) + } + for iter.Valid() { + iter.Next() + keySink = iter.Key() + valueSink = iter.Value() + } + } + + _ = keySink + _ = valueSink +} + +func Benchmark_CacheStack10_Iterate(b *testing.B) { + bs := makeBranchStack(b, 10) + var keySink, valueSink any + + b.ResetTimer() + b.ReportAllocs() + // test sets + for i := 0; i < b.N; i++ { + iter, err := bs.Iterator([]byte{0}, []byte{0}) + if err != nil { + b.Fatal(err) + } + for iter.Valid() { + iter.Next() + keySink = iter.Key() + valueSink = iter.Value() + } + } + + _ = keySink + _ = valueSink +} + +func Benchmark_CacheStack100_Iterate(b *testing.B) { + bs := makeBranchStack(b, 100) + var keySink, valueSink any + + b.ResetTimer() + b.ReportAllocs() + // test sets + for i := 0; i < b.N; i++ { + iter, err := bs.Iterator([]byte{0}, []byte{0}) + if err != nil { + b.Fatal(err) + } + for iter.Valid() { + iter.Next() + keySink = iter.Key() + valueSink = iter.Value() + } + } + + _ = keySink + _ = valueSink +} + +func makeBranchStack(b *testing.B, stackSize int) Store[store.KVStore] { + const elems = 10 + parent := coretesting.NewMemKV() + branch := NewStore[store.KVStore](parent) + for i := 1; i < stackSize; i++ { + branch = NewStore[store.KVStore](branch) + for j := 0; j < elems; j++ { + key, value := []byte{byte(j)}, []byte{byte(j)} + err := branch.Set(key, value) + if err != nil { + b.Fatal(err) + } + } + } + return branch +} diff --git a/server/v2/stf/go.mod b/server/v2/stf/go.mod index 7f55feb3f3f2..c8f18d0bbe04 100644 --- a/server/v2/stf/go.mod +++ b/server/v2/stf/go.mod @@ -3,7 +3,8 @@ module cosmossdk.io/server/v2/stf go 1.23 require ( - cosmossdk.io/core v1.0.0-alpha.4 + cosmossdk.io/core v1.0.0-alpha.5 + cosmossdk.io/core/testing v0.0.0 cosmossdk.io/schema v0.3.0 github.com/cosmos/gogoproto v1.7.0 github.com/tidwall/btree v1.7.0 @@ -13,3 +14,7 @@ require ( github.com/google/go-cmp v0.6.0 // indirect google.golang.org/protobuf v1.35.1 // indirect ) + +replace ( + cosmossdk.io/core/testing => ../../../core/testing +) diff --git a/server/v2/stf/go.sum b/server/v2/stf/go.sum index 29d26cc868dc..3813c60a7c9d 100644 --- a/server/v2/stf/go.sum +++ b/server/v2/stf/go.sum @@ -1,5 +1,7 @@ cosmossdk.io/core v1.0.0-alpha.4 h1:9iuroT9ejDYETCsGkzkvs/wAY/5UFl7nCIINFRxyMJY= cosmossdk.io/core v1.0.0-alpha.4/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY= +cosmossdk.io/core v1.0.0-alpha.5 h1:McjYXAQ6XcT20v2uHyH7PhoWH8V+mebzfVFqT3GinsI= +cosmossdk.io/core v1.0.0-alpha.5/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY= cosmossdk.io/schema v0.3.0 h1:01lcaM4trhzZ1HQTfTV8z6Ma1GziOZ/YmdzBN3F720c= cosmossdk.io/schema v0.3.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ= github.com/cosmos/gogoproto v1.7.0 h1:79USr0oyXAbxg3rspGh/m4SWNyoz/GLaAh0QlCe2fro=