Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Improve benchmarks #25

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 37 additions & 25 deletions benchmarks/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func randBytes(length int) []byte {
return key
}

func prepareTree(db db.DB, size, keyLen, dataLen int) (*iavl.VersionedTree, [][]byte) {
func prepareTree(b *testing.B, db db.DB, size, keyLen, dataLen int) (*iavl.VersionedTree, [][]byte) {
t := iavl.NewVersionedTree(size, db)
keys := make([][]byte, size)

Expand All @@ -27,12 +27,27 @@ func prepareTree(db db.DB, size, keyLen, dataLen int) (*iavl.VersionedTree, [][]
t.Set(key, randBytes(dataLen))
keys[i] = key
}
t.Hash()
t.SaveVersion(t.LatestVersion() + 1)
commitTree(b, t)
runtime.GC()
return t, keys
}

// commit tree saves a new version and deletes and old one...
func commitTree(b *testing.B, t *iavl.VersionedTree) {
t.Hash()
version := t.LatestVersion()
_, err := t.SaveVersion(version + 1)
if err != nil {
b.Errorf("Can't save: %v", err)
}
if version > 1 {
err = t.DeleteVersion(version - 1)
if err != nil {
b.Errorf("Can't delete: %v", err)
}
}
}

func runQueries(b *testing.B, t *iavl.VersionedTree, keyLen int) {
for i := 0; i < b.N; i++ {
q := randBytes(keyLen)
Expand Down Expand Up @@ -65,8 +80,7 @@ func runUpdate(b *testing.B, t *iavl.VersionedTree, dataLen, blockSize int, keys
key := keys[rand.Int31n(l)]
t.Set(key, randBytes(dataLen))
if i%blockSize == 0 {
t.Hash()
t.SaveVersion(t.LatestVersion() + 1)
commitTree(b, t)
}
}
return t
Expand All @@ -81,8 +95,7 @@ func runDelete(b *testing.B, t *iavl.VersionedTree, blockSize int, keys [][]byte
// TODO: test if removed, use more keys (from insert)
t.Remove(key)
if i%blockSize == 0 {
t.Hash()
t.SaveVersion(t.LatestVersion() + 1)
commitTree(b, t)
}
}
return t
Expand All @@ -96,7 +109,7 @@ func runBlock(b *testing.B, t *iavl.VersionedTree, keyLen, dataLen, blockSize in

lastCommit := t
real := t
check := t
// check := t

for i := 0; i < b.N; i++ {
for j := 0; j < blockSize; j++ {
Expand All @@ -110,15 +123,14 @@ func runBlock(b *testing.B, t *iavl.VersionedTree, keyLen, dataLen, blockSize in
data := randBytes(dataLen)

// perform query and write on check and then real
check.Get(key)
check.Set(key, data)
// check.Get(key)
// check.Set(key, data)
real.Get(key)
real.Set(key, data)
}

// at the end of a block, move it all along....
real.Hash()
real.SaveVersion(real.LatestVersion() + 1)
commitTree(b, real)
lastCommit = real
}

Expand Down Expand Up @@ -214,7 +226,7 @@ func runBenchmarks(b *testing.B, benchmarks []benchmark) {
defer func() {
err := os.RemoveAll(dirName)
if err != nil {
fmt.Printf("%+v\n", err)
b.Errorf("%+v\n", err)
}
}()

Expand Down Expand Up @@ -244,7 +256,7 @@ func runSuite(b *testing.B, d db.DB, initSize, blockSize, keyLen, dataLen int) {
runtime.GC()
init := memUseMB()

t, keys := prepareTree(d, initSize, keyLen, dataLen)
t, keys := prepareTree(b, d, initSize, keyLen, dataLen)
used := memUseMB() - init
fmt.Printf("Init Tree took %0.2f MB\n", used)

Expand All @@ -265,15 +277,15 @@ func runSuite(b *testing.B, d db.DB, initSize, blockSize, keyLen, dataLen int) {

// both of these edit size of the tree too much
// need to run with their own tree
t = nil // for gc
b.Run("insert", func(sub *testing.B) {
it, _ := prepareTree(d, initSize, keyLen, dataLen)
sub.ResetTimer()
runInsert(sub, it, keyLen, dataLen, blockSize)
})
b.Run("delete", func(sub *testing.B) {
dt, dkeys := prepareTree(d, initSize+sub.N, keyLen, dataLen)
sub.ResetTimer()
runDelete(sub, dt, blockSize, dkeys)
})
// t = nil // for gc
// b.Run("insert", func(sub *testing.B) {
// it, _ := prepareTree(d, initSize, keyLen, dataLen)
// sub.ResetTimer()
// runInsert(sub, it, keyLen, dataLen, blockSize)
// })
// b.Run("delete", func(sub *testing.B) {
// dt, dkeys := prepareTree(d, initSize+sub.N, keyLen, dataLen)
// sub.ResetTimer()
// runDelete(sub, dt, blockSize, dkeys)
// })
}