Skip to content

Commit

Permalink
Merge pull request cosmos#30 from tendermint/sdk2-benchmarks
Browse files Browse the repository at this point in the history
Sdk2 benchmarks
  • Loading branch information
ebuchman committed Feb 3, 2018
2 parents 4dd4783 + 65ccc26 commit 99f821b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 25 deletions.
63 changes: 38 additions & 25 deletions benchmarks/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import (
db "github.com/tendermint/tmlibs/db"
)

const historySize = 20

func randBytes(length int) []byte {
key := make([]byte, length)
// math.rand.Read always returns err=nil
rand.Read(key)
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 +29,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 > historySize {
err = t.DeleteVersion(version - historySize)
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 +81,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 +96,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 +110,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 +124,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 +227,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 +257,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 +278,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)
// })
}
47 changes: 47 additions & 0 deletions benchmarks/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package benchmarks

import (
"crypto"
"fmt"
"hash"
"testing"

_ "crypto/sha256"

_ "golang.org/x/crypto/ripemd160"
_ "golang.org/x/crypto/sha3"
)

func BenchmarkHash(b *testing.B) {
hashers := []struct {
name string
size int
hasher hash.Hash
}{
{"ripemd160", 64, crypto.RIPEMD160.New()},
{"ripemd160", 512, crypto.RIPEMD160.New()},
{"sha2-256", 64, crypto.SHA256.New()},
{"sha2-256", 512, crypto.SHA256.New()},
{"sha3-256", 64, crypto.SHA3_256.New()},
{"sha3-256", 512, crypto.SHA3_256.New()},
}

for _, h := range hashers {
prefix := fmt.Sprintf("%s-%d", h.name, h.size)
b.Run(prefix, func(sub *testing.B) {
benchHasher(sub, h.hasher, h.size)
})
}
}

func benchHasher(b *testing.B, hasher hash.Hash, size int) {
// create all random bytes before to avoid timing this
inputs := randBytes(b.N + size + 1)

for i := 0; i < b.N; i++ {
hasher.Reset()
// grab a slice of size bytes from random string
hasher.Write(inputs[i : i+size])
hasher.Sum(nil)
}
}

0 comments on commit 99f821b

Please sign in to comment.