Skip to content

Commit

Permalink
add sync option
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Oct 28, 2024
1 parent 26ae49e commit 316fce3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions testground/benchmark/benchmark/testnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ def gen_txs(start, end, num_txs, nonce, msg_version):
@cli.command()
@click.argument("path", type=str)
@click.option("--rpc", default=TESTNET_RPC)
def send_txs(path, rpc):
@click.option("--sync", default=False)
def send_txs(path, rpc, sync):
txs = json.loads(Path(path).read_text())
asyncio.run(send(txs, rpc))
asyncio.run(send(txs, rpc, sync))


if __name__ == "__main__":
Expand Down
11 changes: 7 additions & 4 deletions testground/benchmark/benchmark/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,22 @@ def json_rpc_send_body(raw, method="broadcast_tx_async"):

@backoff.on_predicate(backoff.expo, max_time=60, max_value=5)
@backoff.on_exception(backoff.expo, aiohttp.ClientError, max_time=60, max_value=5)
async def async_sendtx(session, raw, rpc):
async with session.post(rpc, json=json_rpc_send_body(raw)) as rsp:
async def async_sendtx(session, raw, rpc, sync=False):
method = "broadcast_tx_sync" if sync else "broadcast_tx_async"
async with session.post(rpc, json=json_rpc_send_body(raw, method)) as rsp:
data = await rsp.json()
if "error" in data:
print("send tx error, will retry,", data["error"])
return False
return True


async def send(txs, rpc=LOCAL_RPC):
async def send(txs, rpc=LOCAL_RPC, sync=False):
connector = aiohttp.TCPConnector(limit=CONNECTION_POOL_SIZE)
async with aiohttp.ClientSession(
connector=connector, json_serialize=ujson.dumps
) as session:
tasks = [asyncio.ensure_future(async_sendtx(session, raw, rpc)) for raw in txs]
tasks = [
asyncio.ensure_future(async_sendtx(session, raw, rpc, sync)) for raw in txs
]
await asyncio.gather(*tasks)

0 comments on commit 316fce3

Please sign in to comment.