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

Problem: gen-tx don't run in parallel for single node #1645

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## UNRELEASED

### Improvements

* [#]() Gen test tx in parallel even in single node.
yihuang marked this conversation as resolved.
Show resolved Hide resolved

*Oct 14, 2024*

## v1.4.0-rc1
Expand Down
46 changes: 37 additions & 9 deletions testground/benchmark/benchmark/transaction.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import asyncio
import itertools
import multiprocessing
import os
from collections import namedtuple
from pathlib import Path

import aiohttp
import eth_abi
import ujson

from .erc20 import CONTRACT_ADDRESS
from .utils import gen_account
from .utils import gen_account, split

GAS_PRICE = 1000000000
CHAIN_ID = 777
Expand Down Expand Up @@ -47,17 +51,41 @@ def erc20_transfer_tx(nonce: int):
}


Job = namedtuple(
"Job", ["chunk", "global_seq", "num_accounts", "num_txs", "tx_type", "create_tx"]
)


def _do_job(job: Job):
accounts = [gen_account(job.global_seq, i + 1) for i in range(*job.chunk)]
acct_txs = [[] for acct in accounts]
total = 0
for i in range(job.num_txs):
for acct, txs in zip(accounts, acct_txs):
txs.append(acct.sign_transaction(job.create_tx(i)).rawTransaction.hex())
total += 1
if total % 1000 == 0:
print("generated", total, "txs for node", job.global_seq)
return acct_txs
yihuang marked this conversation as resolved.
Show resolved Hide resolved
yihuang marked this conversation as resolved.
Show resolved Hide resolved


def gen(global_seq, num_accounts, num_txs, tx_type: str) -> [str]:
accounts = [gen_account(global_seq, i + 1) for i in range(num_accounts)]
txs = []
chunks = split(num_accounts, os.cpu_count())
create_tx = TX_TYPES[tx_type]
for i in range(num_txs):
for acct in accounts:
txs.append(acct.sign_transaction(create_tx(i)).rawTransaction.hex())
if len(txs) % 1000 == 0:
print("generated", len(txs), "txs for node", global_seq)
jobs = [
Job(chunk, global_seq, num_accounts, num_txs, tx_type, create_tx)
for chunk in chunks
]

with multiprocessing.Pool() as pool:
acct_txs = pool.map(_do_job, jobs)

# mix the account txs together, ordered by nonce.
all_txs = []
for txs in itertools.zip_longest(*itertools.chain(*acct_txs)):
all_txs += txs
yihuang marked this conversation as resolved.
Show resolved Hide resolved

return txs
return all_txs


def save(txs: [str], datadir: Path, global_seq: int):
Expand Down
8 changes: 8 additions & 0 deletions testground/benchmark/benchmark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,11 @@ def block(height):

def block_txs(height):
return block(height)["result"]["block"]["data"]["txs"]


def split(a: int, n: int):
"""
Split range(0, a) into n parts
"""
k, m = divmod(a, n)
return [(i * k + min(i, m), (i + 1) * k + min(i + 1, m)) for i in range(n)]
Loading