Skip to content

Commit

Permalink
adding benchmark tests scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
  • Loading branch information
Psykepro committed Feb 23, 2023
1 parent b788401 commit 92a702d
Show file tree
Hide file tree
Showing 12 changed files with 331 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
**/.DS_Store
.vscode
.idea/

.env
out.dat

cmd/__debug_bin
34 changes: 22 additions & 12 deletions test/benchmarks/sequencer/common/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,32 @@ const (

// CalculateAndPrint calculates and prints the results
func CalculateAndPrint(prometheusResp *http.Response, profilingResult string, elapsed time.Duration, sequencerTimeSub, executorTimeSub float64, nTxs int) {
sequencerTime, executorTime, workerTime, err := GetValues(prometheusResp)
if err != nil {
log.Fatalf("error getting prometheus metrics: %v", err)
var (
sequencerTime, executorTime, workerTime float64
err error
)
if prometheusResp != nil {
sequencerTime, executorTime, workerTime, err = GetValues(prometheusResp)
if err != nil {
log.Fatalf("error getting prometheus metrics: %v", err)
}
}

log.Info("##########")
log.Info("# Result #")
log.Info("##########")
log.Infof("Total time took for the sequencer To select all txs from the pool: %v", elapsed)
log.Info("######################")
log.Info("# Prometheus Metrics #")
log.Info("######################")
actualTotalTime := sequencerTime - sequencerTimeSub
actualExecutorTime := executorTime - executorTimeSub
PrintPrometheus(actualTotalTime, actualExecutorTime, workerTime)
log.Infof("[Transactions per second]: %v", float64(nTxs)/actualTotalTime)
log.Infof("Total time took for the sequencer to select all txs from the pool: %v", elapsed)

if prometheusResp != nil {
log.Info("######################")
log.Info("# Prometheus Metrics #")
log.Info("######################")
actualTotalTime := sequencerTime - sequencerTimeSub
actualExecutorTime := executorTime - executorTimeSub
PrintPrometheus(actualTotalTime, actualExecutorTime, workerTime)
log.Infof("[Transactions per second]: %v", float64(nTxs)/actualTotalTime)

}
if profilingResult != "" {
log.Info("#####################")
log.Info("# Profiling Metrics #")
Expand Down Expand Up @@ -90,7 +100,7 @@ func FetchPrometheus() (*http.Response, error) {
func FetchProfiling() (string, error) {
fullUrl := fmt.Sprintf("http://localhost:%d%s", profilingPort, metricsLib.ProfileEndpoint)
log.Infof("Fetching profiling metrics from: %s ...", fullUrl)
cmd := exec.Command("go", "tool", "pprof", "-top", fullUrl)
cmd := exec.Command("go", "tool", "pprof", "-show=sequencer", "-top", fullUrl)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Error running pprof: %v\n%s", err, out)
Expand Down
2 changes: 2 additions & 0 deletions test/benchmarks/sequencer/common/shared/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +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
)
48 changes: 35 additions & 13 deletions test/benchmarks/sequencer/common/transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@ package transactions

import (
"context"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"math/big"
"testing"
"time"

"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/shared"
"github.com/0xPolygonHermez/zkevm-node/test/operations"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
)

// SendAndWait sends a number of transactions and waits for them to be marked as pending in the pool
func SendAndWait(
b *testing.B,
ctx context.Context,
auth *bind.TransactOpts,
senderNonce uint64,
client *ethclient.Client,
gasPrice *big.Int,
pl *pool.Pool,
ctx context.Context,
countByStatusFunc func(ctx context.Context, status pool.TxStatus) (uint64, error),
nTxs int,
txSenderFunc func(b *testing.B, l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64),
) {
shared.Auth.GasPrice = gasPrice
shared.Auth.GasLimit = 2100000
txSenderFunc func(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64) error,
) error {
auth.GasLimit = 2100000
log.Debugf("Sending %d txs ...", nTxs)
maxNonce := uint64(nTxs) + senderNonce

for nonce := senderNonce; nonce < maxNonce; nonce++ {
txSenderFunc(b, client, gasPrice, nonce)
err := txSenderFunc(client, auth.GasPrice, nonce)
if err != nil {
return err
}
}
log.Debug("All txs were sent!")

log.Debug("Waiting pending transactions To be added in the pool ...")
err := operations.Poll(1*time.Second, shared.DefaultDeadline, func() (bool, error) {
// using a closure here To capture st and currentBatchNumber
count, err := pl.CountPendingTransactions(ctx)
count, err := countByStatusFunc(ctx, pool.TxStatusPending)
if err != nil {
return false, err
}
Expand All @@ -47,6 +47,28 @@ func SendAndWait(
done := count == uint64(nTxs)
return done, nil
})
require.NoError(b, err)
if err != nil {
return err
}

log.Debug("All pending txs are added in the pool!")

return nil
}

func WaitStatusSelected(countByStatusFunc func(ctx context.Context, status pool.TxStatus) (uint64, error), nTxs uint64) (error, time.Duration) {
start := time.Now()
log.Debug("Wait for sequencer to select all txs from the pool")
err := operations.Poll(200*time.Millisecond, shared.DefaultDeadline, func() (bool, error) {
selectedCount, err := countByStatusFunc(shared.Ctx, pool.TxStatusSelected)
if err != nil {
return false, err
}

log.Debugf("amount of selected txs: %d", selectedCount)
done := selectedCount >= nTxs
return done, nil
})
elapsed := time.Since(start)
return err, elapsed
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package erc20_transfers
import (
"context"
"fmt"
"math/big"
"net/http"
"testing"
"time"

"github.com/0xPolygonHermez/zkevm-node/encoding"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/metrics"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/setup"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/shared"
Expand All @@ -23,15 +20,8 @@ import (
)

const (
nTxs = 100
txTimeout = 60 * time.Second
profilingEnabled = true
)

var (
mintAmount, _ = big.NewInt(0).SetString("1000000000000000000000", encoding.Base10)
transferAmount = big.NewInt(0).Div(big.NewInt(0).Mul(big.NewInt(0).Div(mintAmount, big.NewInt(nTxs)), big.NewInt(90)), big.NewInt(100))
erc20SC *ERC20.ERC20
profilingEnabled = false
)

func BenchmarkSequencerERC20TransfersPoolProcess(b *testing.B) {
Expand All @@ -47,31 +37,19 @@ func BenchmarkSequencerERC20TransfersPoolProcess(b *testing.B) {
if err != nil {
return
}

transactions.SendAndWait(b, senderNonce, client, gasPrice, pl, shared.Ctx, nTxs, runERC20TxSender)
shared.Auth.GasPrice = gasPrice
err = transactions.SendAndWait(shared.Ctx, shared.Auth, senderNonce, client, pl.CountTransactionsByStatus, shared.NumberOfTxs, TxSender)
require.NoError(b, err)

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

b.Run(fmt.Sprintf("sequencer_selecting_%d_txs", nTxs), func(b *testing.B) {
b.Run(fmt.Sprintf("sequencer_selecting_%d_txs", shared.NumberOfTxs), func(b *testing.B) {
// Wait all txs to be selected by the sequencer
start := time.Now()
log.Debug("Wait for sequencer to select all txs from the pool")
err := operations.Poll(1*time.Second, shared.DefaultDeadline, func() (bool, error) {
selectedCount, err := pl.CountTransactionsByStatus(shared.Ctx, pool.TxStatusSelected)
if err != nil {
return false, err
}

log.Debugf("amount of selected txs: %d", selectedCount)
done := selectedCount >= nTxs
return done, nil
})
err, _ := transactions.WaitStatusSelected(pl.CountTransactionsByStatus, shared.NumberOfTxs)
require.NoError(b, err)
elapsed = time.Since(start)
response, err = metrics.FetchPrometheus()
require.NoError(b, err)
})
Expand All @@ -87,7 +65,7 @@ func BenchmarkSequencerERC20TransfersPoolProcess(b *testing.B) {
log.Errorf("failed to teardown: %s", err)
}

metrics.CalculateAndPrint(response, profilingResult, elapsed-deploySCElapsed, deploySCSequencerTime, deploySCExecutorOnlyTime, nTxs)
metrics.CalculateAndPrint(response, profilingResult, elapsed-deploySCElapsed, deploySCSequencerTime, deploySCExecutorOnlyTime, shared.NumberOfTxs)
log.Infof("########################################")
log.Infof("# Deploying ERC20 SC and Mint Tx took: #")
log.Infof("########################################")
Expand All @@ -111,15 +89,3 @@ func deployERC20Contract(b *testing.B, client *ethclient.Client, ctx context.Con
require.NoError(b, err)
return err
}

func runERC20TxSender(b *testing.B, l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64) {
log.Debugf("sending nonce: %d", nonce)
var actualTransferAmount *big.Int
if nonce%2 == 0 {
actualTransferAmount = big.NewInt(0).Sub(transferAmount, big.NewInt(int64(nonce)))
} else {
actualTransferAmount = big.NewInt(0).Add(transferAmount, big.NewInt(int64(nonce)))
}
_, err := erc20SC.Transfer(shared.Auth, shared.To, actualTransferAmount)
require.NoError(b, err)
}
30 changes: 30 additions & 0 deletions test/benchmarks/sequencer/erc20-transfers/tx_sender.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package erc20_transfers

import (
"math/big"

"github.com/0xPolygonHermez/zkevm-node/encoding"
"github.com/0xPolygonHermez/zkevm-node/test/contracts/bin/ERC20"

"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/shared"
"github.com/ethereum/go-ethereum/ethclient"
)

var (
mintAmount, _ = big.NewInt(0).SetString("1000000000000000000000", encoding.Base10)
transferAmount = big.NewInt(0).Div(big.NewInt(0).Mul(big.NewInt(0).Div(mintAmount, big.NewInt(shared.NumberOfTxs)), big.NewInt(90)), big.NewInt(100))
erc20SC *ERC20.ERC20
)

func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64) error {
log.Debugf("sending nonce: %d", nonce)
var actualTransferAmount *big.Int
if nonce%2 == 0 {
actualTransferAmount = big.NewInt(0).Sub(transferAmount, big.NewInt(int64(nonce)))
} else {
actualTransferAmount = big.NewInt(0).Add(transferAmount, big.NewInt(int64(nonce)))
}
_, err := erc20SC.Transfer(shared.Auth, shared.To, actualTransferAmount)
return err
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,41 @@ package eth_transfers

import (
"context"
"errors"
"fmt"
"math/big"
"net/http"
"testing"
"time"

"github.com/0xPolygonHermez/zkevm-node/encoding"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/shared"

"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/metrics"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/setup"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/shared"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/transactions"
"github.com/0xPolygonHermez/zkevm-node/test/operations"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
)

var (
ethAmount, _ = big.NewInt(0).SetString("100000000000", encoding.Base10)
)

const (
nTxs = 10
gasLimit = 21000
profilingEnabled = true
profilingEnabled = false
)

func BenchmarkSequencerEthTransfersPoolProcess(b *testing.B) {
ctx := context.Background()
//defer func() { require.NoError(b, operations.Teardown()) }()
opsman, client, pl, senderNonce, gasPrice := setup.Environment(ctx, b)
transactions.SendAndWait(b, senderNonce, client, gasPrice, pl, ctx, nTxs, runTxSender)
shared.Auth.GasPrice = gasPrice
err := transactions.SendAndWait(shared.Ctx, shared.Auth, senderNonce, client, pl.CountTransactionsByStatus, shared.NumberOfTxs, TxSender)
require.NoError(b, err)
setup.BootstrapSequencer(b, opsman)

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

b.Run(fmt.Sprintf("sequencer_selecting_%d_txs", nTxs), func(b *testing.B) {
// Wait all txs to be selected by the sequencer
start := time.Now()
log.Debug("Wait for sequencer to select all txs from the pool")
err := operations.Poll(1*time.Second, shared.DefaultDeadline, func() (bool, error) {
selectedCount, err := pl.CountTransactionsByStatus(ctx, pool.TxStatusSelected)
if err != nil {
return false, err
}

log.Debugf("amount of selected txs: %d", selectedCount)
done := selectedCount >= nTxs
return done, nil
})
b.Run(fmt.Sprintf("sequencer_selecting_%d_txs", shared.NumberOfTxs), func(b *testing.B) {
err, _ := transactions.WaitStatusSelected(pl.CountTransactionsByStatus, shared.NumberOfTxs)
require.NoError(b, err)
elapsed = time.Since(start)
prometheusResponse, err = metrics.FetchPrometheus()

require.NoError(b, err)
Expand All @@ -78,21 +53,6 @@ func BenchmarkSequencerEthTransfersPoolProcess(b *testing.B) {
log.Errorf("failed to teardown: %s", err)
}

metrics.CalculateAndPrint(prometheusResponse, profilingResult, elapsed, 0, 0, nTxs)
metrics.CalculateAndPrint(prometheusResponse, profilingResult, elapsed, 0, 0, shared.NumberOfTxs)
fmt.Printf("%s\n", profilingResult)
}

func runTxSender(b *testing.B, l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64) {
log.Debugf("sending nonce: %d", nonce)
tx := types.NewTransaction(nonce, shared.To, ethAmount, gasLimit, gasPrice, nil)
signedTx, err := shared.Auth.Signer(shared.Auth.From, tx)
require.NoError(b, err)
err = l2Client.SendTransaction(shared.Ctx, signedTx)
if errors.Is(err, state.ErrStateNotSynchronized) {
for errors.Is(err, state.ErrStateNotSynchronized) {
time.Sleep(5 * time.Second)
err = l2Client.SendTransaction(shared.Ctx, signedTx)
}
}
require.NoError(b, err)
}
Loading

0 comments on commit 92a702d

Please sign in to comment.