Skip to content

Commit

Permalink
testing(lib/trie): change lib/trie slow and intensive benchmark-ori…
Browse files Browse the repository at this point in the history
…ented tests to benchmarks (#2031)

- Change unit tests to a merged Go benchmark (you can run them with `go test -bench`)
- Use deepcopy to copy the trie instead of re-generating one, which takes time
- Use math/rand instead of crypto/rand to generate trie values faster
  • Loading branch information
qdm12 authored Nov 16, 2021
1 parent 18ef651 commit c5397c4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
25 changes: 13 additions & 12 deletions lib/trie/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package trie
import (
"crypto/rand"
"encoding/binary"
"math/big"
prand "math/rand"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -35,37 +35,38 @@ func GenerateRandomTests(t testing.TB, size int) []Test {
rt := make([]Test, size)
kv := make(map[string][]byte)

const seed = 912378
generator := prand.New(prand.NewSource(seed)) //nolint:gosec

for i := range rt {
test := generateRandomTest(t, kv)
test := generateRandomTest(t, kv, generator)
rt[i] = test
kv[string(test.key)] = rt[i].value
}

return rt
}

func generateRandomTest(t testing.TB, kv map[string][]byte) Test {
func generateRandomTest(t testing.TB, kv map[string][]byte, generator *prand.Rand) Test {
test := Test{}

for {
n := 2 // arbitrary positive number
size, err := rand.Int(rand.Reader, big.NewInt(510))
require.NoError(t, err)
var n int64 = 2 // arbitrary positive number
size := int64(generator.Intn(510))

buf := make([]byte, size.Int64()+int64(n))
_, err = rand.Read(buf)
buf := make([]byte, size+n)
_, err := generator.Read(buf)
require.NoError(t, err)

key := binary.LittleEndian.Uint16(buf[:2])

if kv[string(buf)] == nil || key < 256 {
test.key = buf

size, err := rand.Int(rand.Reader, big.NewInt(128))
require.NoError(t, err)
size := int64(generator.Intn(128))

buf = make([]byte, size.Int64()+int64(n))
_, err = rand.Read(buf)
buf = make([]byte, size+n)
_, err = generator.Read(buf)
require.NoError(t, err)

test.value = buf
Expand Down
42 changes: 23 additions & 19 deletions lib/trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1116,39 +1116,43 @@ func TestNextKey_Random(t *testing.T) {
}
}

func TestRootHashNonParallel(t *testing.T) {
rt := GenerateRandomTests(t, 1000000)
func Benchmark_Trie_Hash(b *testing.B) {
rt := GenerateRandomTests(b, 1000000)
trie := NewEmptyTrie()
for i := range rt {
test := &rt[i]
trie.Put(test.key, test.value)
}

t.Run("Non Parallel Hash", func(t *testing.T) {
trieTwo, err := trie.DeepCopy()
require.NoError(b, err)

b.Run("Sequential hash", func(b *testing.B) {
trie.parallel = false

b.StartTimer()
_, err := trie.Hash()
require.NoError(t, err)
PrintMemUsage()
b.StopTimer()

require.NoError(b, err)

printMemUsage()
})
}

func TestRootHashParallel(t *testing.T) {
rt := GenerateRandomTests(t, 1000000)
trie := NewEmptyTrie()
for i := range rt {
test := &rt[i]
trie.Put(test.key, test.value)
}
b.Run("Parallel hash", func(b *testing.B) {
trieTwo.parallel = true

t.Run("Parallel Hash", func(t *testing.T) {
trie.parallel = true
_, err := trie.Hash()
require.NoError(t, err)
PrintMemUsage()
b.StartTimer()
_, err := trieTwo.Hash()
b.StopTimer()

require.NoError(b, err)

printMemUsage()
})
}

func PrintMemUsage() {
func printMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
Expand Down

0 comments on commit c5397c4

Please sign in to comment.