From d6eee10dfb547d6e19240bd4630ac83b0b938e65 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Mon, 6 Jun 2022 12:10:41 -0400 Subject: [PATCH] updates --- mempool/bench_test.go | 10 ++++++++-- mempool/cache_test.go | 6 +++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/mempool/bench_test.go b/mempool/bench_test.go index 779110b62..476550548 100644 --- a/mempool/bench_test.go +++ b/mempool/bench_test.go @@ -44,13 +44,16 @@ func BenchmarkCheckTx(b *testing.B) { } func BenchmarkCacheInsertTime(b *testing.B) { - cache := newMapTxCache(b.N) + cache := NewLRUTxCache(b.N) + txs := make([][]byte, b.N) for i := 0; i < b.N; i++ { txs[i] = make([]byte, 8) binary.BigEndian.PutUint64(txs[i], uint64(i)) } + b.ResetTimer() + for i := 0; i < b.N; i++ { cache.Push(txs[i]) } @@ -59,14 +62,17 @@ func BenchmarkCacheInsertTime(b *testing.B) { // This benchmark is probably skewed, since we actually will be removing // txs in parallel, which may cause some overhead due to mutex locking. func BenchmarkCacheRemoveTime(b *testing.B) { - cache := newMapTxCache(b.N) + cache := NewLRUTxCache(b.N) + txs := make([][]byte, b.N) for i := 0; i < b.N; i++ { txs[i] = make([]byte, 8) binary.BigEndian.PutUint64(txs[i], uint64(i)) cache.Push(txs[i]) } + b.ResetTimer() + for i := 0; i < b.N; i++ { cache.Remove(txs[i]) } diff --git a/mempool/cache_test.go b/mempool/cache_test.go index d9a53f475..f139d1778 100644 --- a/mempool/cache_test.go +++ b/mempool/cache_test.go @@ -14,20 +14,24 @@ import ( ) func TestCacheRemove(t *testing.T) { - cache := newMapTxCache(100) + cache := NewLRUTxCache(100) numTxs := 10 + txs := make([][]byte, numTxs) for i := 0; i < numTxs; i++ { // probability of collision is 2**-256 txBytes := make([]byte, 32) _, err := rand.Read(txBytes) require.NoError(t, err) + txs[i] = txBytes cache.Push(txBytes) + // make sure its added to both the linked list and the map require.Equal(t, i+1, len(cache.cacheMap)) require.Equal(t, i+1, cache.list.Len()) } + for i := 0; i < numTxs; i++ { cache.Remove(txs[i]) // make sure its removed from both the map and the linked list