Skip to content

Commit

Permalink
test: adding benchmark test script for uniswap
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Nedkov <[email protected]>
  • Loading branch information
Psykepro committed Aug 14, 2023
1 parent d045bd5 commit 2072038
Show file tree
Hide file tree
Showing 17 changed files with 518 additions and 301 deletions.
4 changes: 2 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ benchmark-sequencer-eth-transfers: stop
$(RUNJSONRPC)
docker ps -a
docker logs $(DOCKERCOMPOSEZKPROVER)
@ cd benchmarks/sequencer/eth-transfers ; \
@ cd benchmarks/sequencer/e2e/eth-transfers ; \
mkdir -p results ; \
touch ./results/out.dat ; \
go test -bench=. -timeout=600m | tee ./results/out.dat ;
Expand All @@ -253,7 +253,7 @@ benchmark-sequencer-erc20-transfers: stop
$(RUNJSONRPC)
docker ps -a
docker logs $(DOCKERCOMPOSEZKPROVER)
@ cd benchmarks/sequencer/erc20-transfers ; \
@ cd benchmarks/sequencer/e2e/erc20-transfers ; \
mkdir -p results ; \
touch ./results/out.dat ; \
go test -bench=. -timeout=600m | tee ./results/out.dat ;
Expand Down
4 changes: 2 additions & 2 deletions test/benchmarks/sequencer/common/params/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ const (
MaxCumulativeGasUsed = 80000000000
// PrometheusPort is the port where prometheus is running
PrometheusPort = 9092
// NumberOfTxs is the number of transactions to send
NumberOfTxs = 1000
// NumberOfOperations is the number of transactions to send
NumberOfOperations = 10
)
58 changes: 37 additions & 21 deletions test/benchmarks/sequencer/common/transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package transactions

import (
"context"
"io/ioutil"
"math/big"
"net/http"
"strconv"
"time"

Expand All @@ -11,35 +13,28 @@ import (
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/params"
"github.com/0xPolygonHermez/zkevm-node/test/contracts/bin/ERC20"
"github.com/0xPolygonHermez/zkevm-node/test/operations"
"github.com/0xPolygonHermez/zkevm-node/test/scripts/uniswap/pkg"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
)

// SendAndWait sends a number of transactions and waits for them to be marked as pending in the pool
func SendAndWait(
ctx context.Context,
auth *bind.TransactOpts,
client *ethclient.Client,
countByStatusFunc func(ctx context.Context, status ...pool.TxStatus) (uint64, error),
nTxs int,
erc20SC *ERC20.ERC20,
txSenderFunc func(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20) error,
) error {
func SendAndWait(auth *bind.TransactOpts, client *ethclient.Client, getTxsByStatus func(ctx context.Context, status pool.TxStatus, limit uint64) ([]pool.Transaction, error), nTxs int, erc20SC *ERC20.ERC20, uniswapDeployments *pkg.Deployments, txSenderFunc func(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *pkg.Deployments) error) error {
auth.GasLimit = 2100000
log.Debugf("Sending %d txs ...", nTxs)
startingNonce := auth.Nonce.Uint64()
maxNonce := uint64(nTxs) + startingNonce
initialPendingCount, err := countByStatusFunc(params.Ctx, pool.TxStatusPending)
if err != nil {
panic(err)
startingNonce := uint64(0)
if auth.Nonce != nil {
startingNonce = auth.Nonce.Uint64()
}
maxNonce := uint64(nTxs) + startingNonce
IP := getPublicIP()

for nonce := startingNonce; nonce < maxNonce; nonce++ {
err = txSenderFunc(client, auth.GasPrice, nonce, auth, erc20SC)
err := txSenderFunc(client, auth.GasPrice, nonce, auth, erc20SC, uniswapDeployments)
if err != nil {
for err != nil && err.Error() == "nonce intrinsic error" {
log.Warnf("nonce intrinsic error, retrying with nonce %d", nonce)
err = txSenderFunc(client, auth.GasPrice, nonce, auth, erc20SC)
err = txSenderFunc(client, auth.GasPrice, nonce, auth, erc20SC, uniswapDeployments)
}
if err == nil {
continue
Expand All @@ -49,15 +44,21 @@ func SendAndWait(
}
log.Debug("All txs were sent!")
log.Debug("Waiting pending transactions To be added in the pool ...")
err = operations.Poll(1*time.Second, params.DefaultDeadline, func() (bool, error) {
err := operations.Poll(1*time.Second, params.DefaultDeadline, func() (bool, error) {
// using a closure here To capture st and currentBatchNumber
count, err := countByStatusFunc(ctx, pool.TxStatusPending)
pendingTxs, err := getTxsByStatus(params.Ctx, pool.TxStatusPending, 0)
if err != nil {
return false, err
panic(err)
}
pendingTxsCount := 0
for _, tx := range pendingTxs {
if tx.IP == IP {
pendingTxsCount++
}
}

log.Debugf("amount of pending txs: %d\n", count)
done := count-initialPendingCount <= 0
log.Debugf("amount of pending txs: %d\n", pendingTxsCount)
done := pendingTxsCount == 0
return done, nil
})
if err != nil {
Expand All @@ -69,6 +70,21 @@ func SendAndWait(
return nil
}

func getPublicIP() string {
resp, err := http.Get("https://api.ipify.org?format=text")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()

ip, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

return string(ip)
}

// WaitStatusSelected waits for a number of transactions to be marked as selected in the pool
func WaitStatusSelected(countByStatusFunc func(ctx context.Context, status ...pool.TxStatus) (uint64, error), initialCount uint64, nTxs uint64) error {
log.Debug("Wait for sequencer to select all txs from the pool")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ func BenchmarkSequencerERC20TransfersPoolProcess(b *testing.B) {
}
initialCount, err := pl.CountTransactionsByStatus(params.Ctx, pool.TxStatusSelected)
require.NoError(b, err)
err = transactions.SendAndWait(params.Ctx, auth, client, pl.CountTransactionsByStatus, params.NumberOfTxs, erc20SC, TxSender)
err = transactions.SendAndWait(auth, client, pl.GetTxsByStatus, params.NumberOfOperations, erc20SC, nil, TxSender)
require.NoError(b, err)

var (
elapsed time.Duration
prometheusResponse *http.Response
)

b.Run(fmt.Sprintf("sequencer_selecting_%d_txs", params.NumberOfTxs), func(b *testing.B) {
b.Run(fmt.Sprintf("sequencer_selecting_%d_txs", params.NumberOfOperations), func(b *testing.B) {
// Wait all txs to be selected by the sequencer
err = transactions.WaitStatusSelected(pl.CountTransactionsByStatus, initialCount, params.NumberOfTxs)
err = transactions.WaitStatusSelected(pl.CountTransactionsByStatus, initialCount, params.NumberOfOperations)
require.NoError(b, err)
elapsed = time.Since(start)
log.Infof("Total elapsed time: %s", elapsed)
Expand All @@ -77,7 +77,7 @@ func BenchmarkSequencerERC20TransfersPoolProcess(b *testing.B) {
elapsed,
deployMetricsValues.SequencerTotalProcessingTime,
deployMetricsValues.ExecutorTotalProcessingTime,
params.NumberOfTxs,
params.NumberOfOperations,
)
timeForFetchAndPrintMetrics := time.Since(startMetrics)
log.Infof("########################################")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/params"
"github.com/0xPolygonHermez/zkevm-node/test/contracts/bin/ERC20"
uniswap "github.com/0xPolygonHermez/zkevm-node/test/scripts/uniswap/pkg"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
)
Expand All @@ -22,7 +23,7 @@ var (
)

// TxSender sends ERC20 transfer to the sequencer
func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20) error {
func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *uniswap.Deployments) error {
log.Debugf("sending tx num: %d nonce: %d", countTxs, nonce)
auth.Nonce = new(big.Int).SetUint64(nonce)
var actualTransferAmount *big.Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ func BenchmarkSequencerEthTransfersPoolProcess(b *testing.B) {
require.NoError(b, err)
timeForSetup := time.Since(start)
setup.BootstrapSequencer(b, opsman)
err = transactions.SendAndWait(params.Ctx, auth, client, pl.CountTransactionsByStatus, params.NumberOfTxs, nil, TxSender)
err = transactions.SendAndWait(auth, client, pl.GetTxsByStatus, params.NumberOfOperations, nil, nil, TxSender)
require.NoError(b, err)

var (
elapsed time.Duration
prometheusResponse *http.Response
)

b.Run(fmt.Sprintf("sequencer_selecting_%d_txs", params.NumberOfTxs), func(b *testing.B) {
err = transactions.WaitStatusSelected(pl.CountTransactionsByStatus, initialCount, params.NumberOfTxs)
b.Run(fmt.Sprintf("sequencer_selecting_%d_txs", params.NumberOfOperations), func(b *testing.B) {
err = transactions.WaitStatusSelected(pl.CountTransactionsByStatus, initialCount, params.NumberOfOperations)
require.NoError(b, err)
elapsed = time.Since(start)
log.Infof("Total elapsed time: %s", elapsed)
Expand All @@ -51,7 +51,7 @@ func BenchmarkSequencerEthTransfersPoolProcess(b *testing.B) {
require.NoError(b, err)
}

metrics.CalculateAndPrint(prometheusResponse, profilingResult, elapsed, 0, 0, params.NumberOfTxs)
metrics.CalculateAndPrint(prometheusResponse, profilingResult, elapsed, 0, 0, params.NumberOfOperations)
fmt.Printf("%s\n", profilingResult)
timeForFetchAndPrintMetrics := time.Since(startMetrics)
log.Infof("Time for setup: %s", timeForSetup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/params"
"github.com/0xPolygonHermez/zkevm-node/test/contracts/bin/ERC20"
uniswap "github.com/0xPolygonHermez/zkevm-node/test/scripts/uniswap/pkg"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
Expand All @@ -22,7 +23,7 @@ var (
)

// TxSender sends eth transfer to the sequencer
func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20) error {
func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *uniswap.Deployments) error {
log.Debugf("sending tx num: %d nonce: %d", countTxs, nonce)
auth.Nonce = big.NewInt(int64(nonce))
tx := types.NewTransaction(nonce, params.To, ethAmount, uint64(gasLimit), gasPrice, nil)
Expand Down
41 changes: 41 additions & 0 deletions test/benchmarks/sequencer/e2e/uniswap-transfers/tx_sender.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package uniswap_transfers

import (
"errors"
"math/big"
"time"

"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/test/contracts/bin/ERC20"
uniswap "github.com/0xPolygonHermez/zkevm-node/test/scripts/uniswap/pkg"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
)

var (
gasLimit = 21000
sleepTime = 5 * time.Second
countTxs = 0
txTimeout = 60 * time.Second
)

// TxSender sends eth transfer to the sequencer
func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *uniswap.Deployments) error {
log.Debugf("swap number: %d", countTxs, nonce)
var err error

uniswap.SwapTokens(l2Client, auth, *uniswapDeployments)
if errors.Is(err, state.ErrStateNotSynchronized) || errors.Is(err, state.ErrInsufficientFunds) {
for errors.Is(err, state.ErrStateNotSynchronized) || errors.Is(err, state.ErrInsufficientFunds) {
time.Sleep(sleepTime)
uniswap.SwapTokens(l2Client, auth, *uniswapDeployments)
}
}

if err == nil {
countTxs += 1
}

return err
}
4 changes: 2 additions & 2 deletions test/benchmarks/sequencer/scripts/common/environment/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
)

// Init sets up the environment for the benchmark
func Init() (context.Context, *pgpoolstorage.PostgresPoolStorage, *state.PostgresStorage, *ethclient.Client, *bind.TransactOpts) {
func Init() (*pgpoolstorage.PostgresPoolStorage, *state.PostgresStorage, *ethclient.Client, *bind.TransactOpts) {
ctx := context.Background()
pl, err := pgpoolstorage.NewPostgresPoolStorage(db.Config{
Name: poolDbName,
Expand Down Expand Up @@ -93,5 +93,5 @@ func Init() (context.Context, *pgpoolstorage.PostgresPoolStorage, *state.Postgre
stateStorage := state.NewPostgresStorage(stateDb)
auth.Nonce = new(big.Int).SetUint64(senderNonce)

return ctx, pl, stateStorage, l2Client, auth
return pl, stateStorage, l2Client, auth
}
13 changes: 11 additions & 2 deletions test/benchmarks/sequencer/scripts/common/results/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ func Print(elapsed time.Duration) {
log.Info("# Result #")
log.Info("##########")
log.Infof("Total time took for the sequencer to select all txs from the pool: %v", elapsed)
log.Infof("Number of txs sent: %d", params.NumberOfTxs)
log.Infof("Txs per second: %f", float64(params.NumberOfTxs)/elapsed.Seconds())
log.Infof("Number of txs sent: %d", params.NumberOfOperations)
log.Infof("Txs per second: %f", float64(params.NumberOfOperations)/elapsed.Seconds())
}

func PrintUniswapDeployments(deployments time.Duration, count uint64) {
log.Info("#######################")
log.Info("# Uniswap Deployments #")
log.Info("#######################")
log.Infof("Total time took for the sequencer to deploy all contracts: %v", deployments)
log.Infof("Number of txs sent: %d", count)
log.Infof("Txs per second: %f", float64(count)/deployments.Seconds())
}
12 changes: 6 additions & 6 deletions test/benchmarks/sequencer/scripts/erc20-transfers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/params"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/transactions"
erc20transfers "github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/erc20-transfers"
erc20transfers "github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/e2e/erc20-transfers"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/scripts/common/environment"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/scripts/common/results"
"github.com/0xPolygonHermez/zkevm-node/test/contracts/bin/ERC20"
Expand All @@ -17,7 +17,7 @@ func main() {
var (
err error
)
ctx, pl, state, l2Client, auth := environment.Init()
pl, state, l2Client, auth := environment.Init()
initialCount, err := pl.CountTransactionsByStatus(params.Ctx, pool.TxStatusSelected)
if err != nil {
panic(err)
Expand All @@ -30,20 +30,20 @@ func main() {
}
// Send Txs
err = transactions.SendAndWait(
ctx,
auth,
l2Client,
pl.CountTransactionsByStatus,
params.NumberOfTxs,
pl.GetTxsByStatus,
params.NumberOfOperations,
erc20SC,
nil,
erc20transfers.TxSender,
)
if err != nil {
panic(err)
}

// Wait for Txs to be selected
err = transactions.WaitStatusSelected(pl.CountTransactionsByStatus, initialCount, params.NumberOfTxs)
err = transactions.WaitStatusSelected(pl.CountTransactionsByStatus, initialCount, params.NumberOfOperations)
if err != nil {
panic(err)
}
Expand Down
12 changes: 6 additions & 6 deletions test/benchmarks/sequencer/scripts/eth-transfers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/params"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/transactions"
ethtransfers "github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/eth-transfers"
ethtransfers "github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/e2e/eth-transfers"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/scripts/common/environment"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/scripts/common/results"
)
Expand All @@ -15,7 +15,7 @@ func main() {
var (
err error
)
ctx, pl, state, l2Client, auth := environment.Init()
pl, state, l2Client, auth := environment.Init()
initialCount, err := pl.CountTransactionsByStatus(params.Ctx, pool.TxStatusSelected)
if err != nil {
panic(err)
Expand All @@ -24,11 +24,11 @@ func main() {
start := time.Now()
// Send Txs
err = transactions.SendAndWait(
ctx,
auth,
l2Client,
pl.CountTransactionsByStatus,
params.NumberOfTxs,
pl.GetTxsByStatus,
params.NumberOfOperations,
nil,
nil,
ethtransfers.TxSender,
)
Expand All @@ -37,7 +37,7 @@ func main() {
}

// Wait for Txs to be selected
err = transactions.WaitStatusSelected(pl.CountTransactionsByStatus, initialCount, params.NumberOfTxs)
err = transactions.WaitStatusSelected(pl.CountTransactionsByStatus, initialCount, params.NumberOfOperations)
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit 2072038

Please sign in to comment.