This commit is contained in:
Aleksandr Bezobchuk
2022-06-06 12:10:41 -04:00
parent 645b42c3dc
commit d6eee10dfb
2 changed files with 13 additions and 3 deletions
+8 -2
View File
@@ -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])
}
+5 -1
View File
@@ -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