From 68d174ad8966792ed03dd2e21f3cea8b3af2b4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Mon, 2 Dec 2024 10:04:15 +0200 Subject: [PATCH 1/2] Add benchmarks for addition in worst case. --- txcache/testutils_test.go | 1 + txcache/txCache_test.go | 87 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/txcache/testutils_test.go b/txcache/testutils_test.go index 6902f1d7..49dc0e85 100644 --- a/txcache/testutils_test.go +++ b/txcache/testutils_test.go @@ -15,6 +15,7 @@ const oneMilion = 1000000 const oneBillion = oneMilion * 1000 const oneQuintillion = 1_000_000_000_000_000_000 const estimatedSizeOfBoundedTxFields = uint64(128) +const hashLength = 32 var oneQuintillionBig = big.NewInt(oneQuintillion) diff --git a/txcache/txCache_test.go b/txcache/txCache_test.go index d8984718..858ad320 100644 --- a/txcache/txCache_test.go +++ b/txcache/txCache_test.go @@ -1,6 +1,7 @@ package txcache import ( + "crypto/rand" "errors" "fmt" "math" @@ -8,6 +9,7 @@ import ( "sync" "testing" + "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/check" "github.com/multiversx/mx-chain-storage-go/common" "github.com/multiversx/mx-chain-storage-go/testscommon/txcachemocks" @@ -513,6 +515,91 @@ func TestTxCache_NoCriticalInconsistency_WhenConcurrentAdditionsAndRemovals(t *t } } +func TestBenchmarkTxCache_addManyTransactionsWithSameNonce(t *testing.T) { + config := ConfigSourceMe{ + Name: "untitled", + NumChunks: 16, + NumBytesThreshold: 419_430_400, + NumBytesPerSenderThreshold: 12_288_000, + CountThreshold: 300_000, + CountPerSenderThreshold: 20_000, + EvictionEnabled: true, + NumItemsToPreemptivelyEvict: 50_000, + } + + host := txcachemocks.NewMempoolHostMock() + randomBytes := make([]byte, math.MaxUint16*hashLength) + rand.Read(randomBytes) + sw := core.NewStopWatch() + + t.Run("numTransactions = 100 (worst case)", func(t *testing.T) { + cache, err := NewTxCache(config, host) + require.Nil(t, err) + + numTransactions := 100 + + sw.Start(t.Name()) + + for i := 0; i < numTransactions; i++ { + cache.AddTx(createTx(randomBytes[i*hashLength:(i+1)*hashLength], "alice", 42).withGasPrice(oneBillion + uint64(i))) + } + + sw.Stop(t.Name()) + + require.Equal(t, numTransactions, int(cache.CountTx())) + }) + + t.Run("numTransactions = 1000 (worst case)", func(t *testing.T) { + cache, err := NewTxCache(config, host) + require.Nil(t, err) + + numTransactions := 1000 + + sw.Start(t.Name()) + + for i := 0; i < numTransactions; i++ { + cache.AddTx(createTx(randomBytes[i*hashLength:(i+1)*hashLength], "alice", 42).withGasPrice(oneBillion + uint64(i))) + } + + sw.Stop(t.Name()) + + require.Equal(t, numTransactions, int(cache.CountTx())) + }) + + t.Run("numTransactions = 5_000 (worst case)", func(t *testing.T) { + cache, err := NewTxCache(config, host) + require.Nil(t, err) + + numTransactions := 5_000 + + sw.Start(t.Name()) + + for i := 0; i < numTransactions; i++ { + cache.AddTx(createTx(randomBytes[i*hashLength:(i+1)*hashLength], "alice", 42).withGasPrice(oneBillion + uint64(i))) + } + + sw.Stop(t.Name()) + + require.Equal(t, numTransactions, int(cache.CountTx())) + }) + + for name, measurement := range sw.GetMeasurementsMap() { + fmt.Printf("%fs (%s)\n", measurement, name) + } + + // (1) + // Vendor ID: GenuineIntel + // Model name: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz + // CPU family: 6 + // Model: 140 + // Thread(s) per core: 2 + // Core(s) per socket: 4 + // + // 0.000117s (TestBenchmarkTxCache_addManyTransactionsWithSameNonce/numTransactions_=_100) + // 0.003117s (TestBenchmarkTxCache_addManyTransactionsWithSameNonce/numTransactions_=_1000) + // 0.056481s (TestBenchmarkTxCache_addManyTransactionsWithSameNonce/numTransactions_=_5_000) +} + func newUnconstrainedCacheToTest() *TxCache { host := txcachemocks.NewMempoolHostMock() From f9a268fc6918ddd51af79eda92704e9ebf272d2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Mon, 2 Dec 2024 10:12:01 +0200 Subject: [PATCH 2/2] Fix linter issue. --- txcache/txCache_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/txcache/txCache_test.go b/txcache/txCache_test.go index 858ad320..18cec810 100644 --- a/txcache/txCache_test.go +++ b/txcache/txCache_test.go @@ -529,7 +529,9 @@ func TestBenchmarkTxCache_addManyTransactionsWithSameNonce(t *testing.T) { host := txcachemocks.NewMempoolHostMock() randomBytes := make([]byte, math.MaxUint16*hashLength) - rand.Read(randomBytes) + _, err := rand.Read(randomBytes) + require.Nil(t, err) + sw := core.NewStopWatch() t.Run("numTransactions = 100 (worst case)", func(t *testing.T) {