github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/store/cache/store_bench_test.go (about)

     1  package cache_test
     2  
     3  import (
     4  	"crypto/rand"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/gnolang/gno/tm2/pkg/db/memdb"
     9  	"github.com/gnolang/gno/tm2/pkg/store/cache"
    10  	"github.com/gnolang/gno/tm2/pkg/store/dbadapter"
    11  )
    12  
    13  func benchmarkCacheStoreIterator(b *testing.B, numKVs int) {
    14  	b.Helper()
    15  
    16  	mem := dbadapter.Store{DB: memdb.NewMemDB()}
    17  	cstore := cache.New(mem)
    18  	keys := make([]string, numKVs, numKVs)
    19  
    20  	for i := 0; i < numKVs; i++ {
    21  		key := make([]byte, 32)
    22  		value := make([]byte, 32)
    23  
    24  		_, _ = rand.Read(key)
    25  		_, _ = rand.Read(value)
    26  
    27  		keys[i] = string(key)
    28  		cstore.Set(key, value)
    29  	}
    30  
    31  	sort.Strings(keys)
    32  
    33  	for n := 0; n < b.N; n++ {
    34  		iter := cstore.Iterator([]byte(keys[0]), []byte(keys[numKVs-1]))
    35  
    36  		for _ = iter.Key(); iter.Valid(); iter.Next() {
    37  		}
    38  
    39  		iter.Close()
    40  	}
    41  }
    42  
    43  func BenchmarkCacheStoreIterator500(b *testing.B)    { benchmarkCacheStoreIterator(b, 500) }
    44  func BenchmarkCacheStoreIterator1000(b *testing.B)   { benchmarkCacheStoreIterator(b, 1000) }
    45  func BenchmarkCacheStoreIterator10000(b *testing.B)  { benchmarkCacheStoreIterator(b, 10000) }
    46  func BenchmarkCacheStoreIterator50000(b *testing.B)  { benchmarkCacheStoreIterator(b, 50000) }
    47  func BenchmarkCacheStoreIterator100000(b *testing.B) { benchmarkCacheStoreIterator(b, 100000) }