Skip to content

Commit

Permalink
Make sure to delete old versions in benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey authored and melekes committed Feb 1, 2018
1 parent a2b6fe7 commit 908bcb7
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 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(db, size)
keys := make([][]byte, size)

Expand All @@ -27,12 +27,26 @@ func prepareTree(db db.DB, size, keyLen, dataLen int) (*iavl.VersionedTree, [][]
t.Set(key, randBytes(dataLen))
keys[i] = key
}
t.Hash()
t.SaveVersion()
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, err := t.SaveVersion()
if err != nil {
b.Errorf("Can't save: %v", err)
}
if version > 2 {
err = t.DeleteVersion(version - 2)
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 +79,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()
commitTree(b, t)
}
}
return t
Expand All @@ -81,8 +94,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()
commitTree(b, t)
}
}
return t
Expand All @@ -96,7 +108,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 +122,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()
commitTree(b, real)
lastCommit = real
}

Expand Down Expand Up @@ -214,7 +225,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 +255,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 +276,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)
// })
}

0 comments on commit 908bcb7

Please sign in to comment.