Skip to content

Commit

Permalink
add benches
Browse files Browse the repository at this point in the history
  • Loading branch information
testinginprod committed Oct 29, 2024
1 parent 6d64e8c commit d4c27a2
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 1 deletion.
172 changes: 172 additions & 0 deletions server/v2/stf/branch/bench_test.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 6 additions & 1 deletion server/v2/stf/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
2 changes: 2 additions & 0 deletions server/v2/stf/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down

0 comments on commit d4c27a2

Please sign in to comment.