From 316fce397f74224e9eca04c7b1f49f489a148b94 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Mon, 28 Oct 2024 11:55:13 +0800 Subject: [PATCH] add sync option --- testground/benchmark/benchmark/testnet.py | 5 +++-- testground/benchmark/benchmark/transaction.py | 11 +++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/testground/benchmark/benchmark/testnet.py b/testground/benchmark/benchmark/testnet.py index b44560d552..d7d9c6d7a9 100644 --- a/testground/benchmark/benchmark/testnet.py +++ b/testground/benchmark/benchmark/testnet.py @@ -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__": diff --git a/testground/benchmark/benchmark/transaction.py b/testground/benchmark/benchmark/transaction.py index 311985d354..31853bb42d 100644 --- a/testground/benchmark/benchmark/transaction.py +++ b/testground/benchmark/benchmark/transaction.py @@ -228,8 +228,9 @@ 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"]) @@ -237,10 +238,12 @@ async def async_sendtx(session, raw, rpc): 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)