github.com/MetalBlockchain/metalgo@v1.11.9/cache/lru_cache_benchmark_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package cache
     5  
     6  import (
     7  	"crypto/rand"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/MetalBlockchain/metalgo/ids"
    13  )
    14  
    15  func BenchmarkLRUCachePutSmall(b *testing.B) {
    16  	smallLen := 5
    17  	cache := &LRU[ids.ID, int]{Size: smallLen}
    18  	for n := 0; n < b.N; n++ {
    19  		for i := 0; i < smallLen; i++ {
    20  			var id ids.ID
    21  			_, err := rand.Read(id[:])
    22  			require.NoError(b, err)
    23  			cache.Put(id, n)
    24  		}
    25  		b.StopTimer()
    26  		cache.Flush()
    27  		b.StartTimer()
    28  	}
    29  }
    30  
    31  func BenchmarkLRUCachePutMedium(b *testing.B) {
    32  	mediumLen := 250
    33  	cache := &LRU[ids.ID, int]{Size: mediumLen}
    34  	for n := 0; n < b.N; n++ {
    35  		for i := 0; i < mediumLen; i++ {
    36  			var id ids.ID
    37  			_, err := rand.Read(id[:])
    38  			require.NoError(b, err)
    39  			cache.Put(id, n)
    40  		}
    41  		b.StopTimer()
    42  		cache.Flush()
    43  		b.StartTimer()
    44  	}
    45  }
    46  
    47  func BenchmarkLRUCachePutLarge(b *testing.B) {
    48  	largeLen := 10000
    49  	cache := &LRU[ids.ID, int]{Size: largeLen}
    50  	for n := 0; n < b.N; n++ {
    51  		for i := 0; i < largeLen; i++ {
    52  			var id ids.ID
    53  			_, err := rand.Read(id[:])
    54  			require.NoError(b, err)
    55  			cache.Put(id, n)
    56  		}
    57  		b.StopTimer()
    58  		cache.Flush()
    59  		b.StartTimer()
    60  	}
    61  }