Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mempool: add benchmarks for addition in worst case #64

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions txcache/testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
89 changes: 89 additions & 0 deletions txcache/txCache_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package txcache

import (
"crypto/rand"
"errors"
"fmt"
"math"
"sort"
"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"
Expand Down Expand Up @@ -513,6 +515,93 @@ 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)
_, err := rand.Read(randomBytes)
require.Nil(t, err)

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()

Expand Down