Skip to content

Commit

Permalink
Problem: memiavl get benchmark not compare with unordered containers (c…
Browse files Browse the repository at this point in the history
…rypto-org-chain#939)

Solution:
- add lru-cache and go-map to the benchmark game
  • Loading branch information
yihuang authored Mar 20, 2023
1 parent f7c6f73 commit bd7251b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
75 changes: 53 additions & 22 deletions memiavl/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"
"testing"

lru "github.com/hashicorp/golang-lru"
"github.com/stretchr/testify/require"
"github.com/tidwall/btree"
)
Expand All @@ -18,7 +19,8 @@ func BenchmarkByteCompare(b *testing.B) {
}

func BenchmarkRandomGet(b *testing.B) {
items := genRandItems(1000000)
amount := 1000000
items := genRandItems(amount)
targetKey := items[500].key
targetValue := items[500].value
targetItem := itemT{key: targetKey}
Expand All @@ -28,24 +30,8 @@ func BenchmarkRandomGet(b *testing.B) {
tree.Set(item.key, item.value)
}

bt2 := btree.NewBTreeGOptions(lessG, btree.Options{
NoLocks: true,
Degree: 2,
})
for _, item := range items {
bt2.Set(item)
}

bt32 := btree.NewBTreeGOptions(lessG, btree.Options{
NoLocks: true,
Degree: 32,
})
for _, item := range items {
bt32.Set(item)
}

snapshotDir := b.TempDir()
err := tree.WriteSnapshot(snapshotDir, false)
err := tree.WriteSnapshot(snapshotDir, true)
require.NoError(b, err)
snapshot, err := OpenSnapshot(snapshotDir)
require.NoError(b, err)
Expand All @@ -55,10 +41,6 @@ func BenchmarkRandomGet(b *testing.B) {
require.Equal(b, targetValue, tree.Get(targetKey))
require.Equal(b, targetValue, diskTree.Get(targetKey))
require.Equal(b, targetValue, snapshot.Get(targetKey))
v, _ := bt2.Get(targetItem)
require.Equal(b, targetValue, v.value)
v, _ = bt32.Get(targetItem)
require.Equal(b, targetValue, v.value)

b.ResetTimer()
b.Run("memiavl", func(b *testing.B) {
Expand All @@ -77,15 +59,64 @@ func BenchmarkRandomGet(b *testing.B) {
}
})
b.Run("btree-degree-2", func(b *testing.B) {
bt2 := btree.NewBTreeGOptions(lessG, btree.Options{
NoLocks: true,
Degree: 2,
})
for _, item := range items {
bt2.Set(item)
}
v, _ := bt2.Get(targetItem)
require.Equal(b, targetValue, v.value)

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = bt2.Get(targetItem)
}
})
b.Run("btree-degree-32", func(b *testing.B) {
bt32 := btree.NewBTreeGOptions(lessG, btree.Options{
NoLocks: true,
Degree: 32,
})
for _, item := range items {
bt32.Set(item)
}
v, _ := bt32.Get(targetItem)
require.Equal(b, targetValue, v.value)

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = bt32.Get(targetItem)
}
})
b.Run("lru-cache", func(b *testing.B) {
cache, err := lru.NewARC(amount)
require.NoError(b, err)
for _, item := range items {
cache.Add(string(item.key), item.value)
}
v, _ := cache.Get(string(targetItem.key))
require.Equal(b, targetValue, v.([]byte))

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = cache.Get(string(targetKey))
}
})
b.Run("go-map", func(b *testing.B) {
m := make(map[string][]byte, amount)
for _, item := range items {
m[string(item.key)] = item.value
}
v, _ := m[string(targetItem.key)]
require.Equal(b, targetValue, v)

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = m[string(targetKey)]
}
})
}

func BenchmarkRandomSet(b *testing.B) {
Expand Down
1 change: 1 addition & 0 deletions memiavl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/cosmos/iavl v0.19.5
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
github.com/ledgerwatch/erigon-lib v0.0.0-20230210071639-db0e7ed11263
github.com/stretchr/testify v1.8.1
github.com/tendermint/tm-db v0.6.7
Expand Down
2 changes: 2 additions & 0 deletions memiavl/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs=
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down

0 comments on commit bd7251b

Please sign in to comment.