github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/iavl/nodedb_test.go (about)

     1  package iavl
     2  
     3  import (
     4  	"encoding/binary"
     5  	"math/rand"
     6  	"testing"
     7  )
     8  
     9  func BenchmarkNodeKey(b *testing.B) {
    10  	ndb := &nodeDB{}
    11  	hashes := makeHashes(b, 2432325)
    12  	for i := 0; i < b.N; i++ {
    13  		ndb.nodeKey(hashes[i])
    14  	}
    15  }
    16  
    17  func BenchmarkOrphanKey(b *testing.B) {
    18  	ndb := &nodeDB{}
    19  	hashes := makeHashes(b, 2432325)
    20  	for i := 0; i < b.N; i++ {
    21  		ndb.orphanKey(1234, 1239, hashes[i])
    22  	}
    23  }
    24  
    25  func makeHashes(b *testing.B, seed int64) [][]byte {
    26  	b.Helper()
    27  
    28  	b.StopTimer()
    29  	rnd := rand.NewSource(seed)
    30  	hashes := make([][]byte, b.N)
    31  	hashBytes := 8 * ((hashSize + 7) / 8)
    32  	for i := 0; i < b.N; i++ {
    33  		hashes[i] = make([]byte, hashBytes)
    34  		for b := 0; b < hashBytes; b += 8 {
    35  			binary.BigEndian.PutUint64(hashes[i][b:b+8], uint64(rnd.Int63()))
    36  		}
    37  		hashes[i] = hashes[i][:hashSize]
    38  	}
    39  	b.StartTimer()
    40  	return hashes
    41  }