Skip to content

Commit

Permalink
receipts gather in separate routines
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Apr 11, 2024
1 parent 8001d83 commit 82c1ad3
Showing 1 changed file with 61 additions and 31 deletions.
92 changes: 61 additions & 31 deletions loadtest/runner/base_load_test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/big"
"os"
"sort"
"sync"
"time"

"github.com/0xPolygon/polygon-edge/crypto"
Expand Down Expand Up @@ -234,52 +235,81 @@ func (r *BaseLoadTestRunner) waitForReceipts(txHashes []types.Hash) (map[uint64]

foundErrors := make([]error, 0)

for _, txHash := range txHashes {
if blockNum, exists := txToBlockMap[txHash]; exists {
txnStats = append(txnStats, txStats{txHash, blockNum})
getTxReceipts := func(txHashes []types.Hash) {
for _, txHash := range txHashes {
if blockNum, exists := txToBlockMap[txHash]; exists {
txnStats = append(txnStats, txStats{txHash, blockNum})
_ = bar.Add(1)

continue
}

receipt, err := r.waitForReceipt(txHash)
if err != nil {
foundErrors = append(foundErrors, err)

continue
}

txnStats = append(txnStats, txStats{txHash, receipt.BlockNumber})
_ = bar.Add(1)

continue
}
block, err := r.client.GetBlockByNumber(jsonrpc.BlockNumber(receipt.BlockNumber), true)
if err != nil {
foundErrors = append(foundErrors, err)

receipt, err := r.waitForReceipt(txHash)
if err != nil {
foundErrors = append(foundErrors, err)
continue
}

continue
}
gasUsed := new(big.Int).SetUint64(block.Header.GasUsed)
gasLimit := new(big.Int).SetUint64(block.Header.GasLimit)
gasUtilization := new(big.Int).Mul(gasUsed, big.NewInt(10000))
gasUtilization = gasUtilization.Div(gasUtilization, gasLimit).Div(gasUtilization, big.NewInt(100))

txnStats = append(txnStats, txStats{txHash, receipt.BlockNumber})
_ = bar.Add(1)
gu, _ := gasUtilization.Float64()

block, err := r.client.GetBlockByNumber(jsonrpc.BlockNumber(receipt.BlockNumber), true)
if err != nil {
foundErrors = append(foundErrors, err)
blockInfoMap[receipt.BlockNumber] = &BlockInfo{
Number: receipt.BlockNumber,
CreatedAt: block.Header.Timestamp,
NumTxs: len(block.Transactions),
GasUsed: new(big.Int).SetUint64(block.Header.GasUsed),
GasLimit: new(big.Int).SetUint64(block.Header.GasLimit),
GasUtilization: gu,
}

continue
for _, txn := range block.Transactions {
txToBlockMap[txn.Hash()] = receipt.BlockNumber
}
}
}

gasUsed := new(big.Int).SetUint64(block.Header.GasUsed)
gasLimit := new(big.Int).SetUint64(block.Header.GasLimit)
gasUtilization := new(big.Int).Mul(gasUsed, big.NewInt(10000))
gasUtilization = gasUtilization.Div(gasUtilization, gasLimit).Div(gasUtilization, big.NewInt(100))
totalTxns := len(txHashes)

gu, _ := gasUtilization.Float64()
// split the txHashes into batches so we can get them in parallel routines
batchSize := totalTxns / 10
if batchSize == 0 {
batchSize = 1
}

blockInfoMap[receipt.BlockNumber] = &BlockInfo{
Number: receipt.BlockNumber,
CreatedAt: block.Header.Timestamp,
NumTxs: len(block.Transactions),
GasUsed: new(big.Int).SetUint64(block.Header.GasUsed),
GasLimit: new(big.Int).SetUint64(block.Header.GasLimit),
GasUtilization: gu,
}
var wg sync.WaitGroup

for _, txn := range block.Transactions {
txToBlockMap[txn.Hash()] = receipt.BlockNumber
for i := 0; i < totalTxns; i += batchSize {
end := i + batchSize
if end > totalTxns {
end = totalTxns
}

wg.Add(1)

go func(txHashes []types.Hash) {
defer wg.Done()

getTxReceipts(txHashes)
}(txHashes[i:end])
}

wg.Wait()

if len(foundErrors) > 0 {
fmt.Println("Errors found while waiting for receipts:")

Expand Down

0 comments on commit 82c1ad3

Please sign in to comment.