Skip to content

Commit

Permalink
Problem: single validator benchmark can't run natively (#1649)
Browse files Browse the repository at this point in the history
* Problem: single validator benchmark can't run natively

single validator benchmark should be runnable locally, since there are no
port issues

temp

* Update CHANGELOG.md

Signed-off-by: yihuang <[email protected]>

* Update testground/benchmark/benchmark/stateless.py

Signed-off-by: yihuang <[email protected]>

* Update testground/benchmark/benchmark/stateless.py

Signed-off-by: yihuang <[email protected]>

---------

Signed-off-by: yihuang <[email protected]>
Signed-off-by: mmsqe <[email protected]>
Co-authored-by: mmsqe <[email protected]>
  • Loading branch information
yihuang and mmsqe authored Oct 18, 2024
1 parent b08f6d6 commit 85ca58b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Bug Fixes

* (testground)[1649](https://github.com/crypto-org-chain/cronos/pull/1649) Fix running single validator benchmark locally.
* (cli)[#1647](https://github.com/crypto-org-chain/cronos/pull/1647) Fix node can't shutdown by signal.

### Improvements
Expand Down
61 changes: 44 additions & 17 deletions testground/benchmark/benchmark/stateless.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shutil
import socket
import subprocess
import sys
import tarfile
import tempfile
import time
Expand Down Expand Up @@ -175,7 +176,7 @@ def patchimage(toimage, src, dst, fromimage):
@click.option("--outdir", default="/outputs")
@click.option("--datadir", default="/data")
@click.option("--cronosd", default=CONTAINER_CRONOSD_PATH)
@click.option("--global-seq", default=None)
@click.option("--global-seq", default=None, type=int)
def run(outdir: str, datadir: str, cronosd, global_seq):
datadir = Path(datadir)
cfg = json.loads((datadir / "config.json").read_text())
Expand All @@ -194,14 +195,19 @@ def run(outdir: str, datadir: str, cronosd, global_seq):
finally:
# collect outputs
output = Path("/data.tar.bz2")
with tarfile.open(output, "x:bz2") as tar:
tar.add(home, arcname="data", filter=output_filter(group, group_seq))
outdir = Path(outdir)
if outdir.exists():
assert outdir.is_dir()
filename = outdir / f"{group}_{group_seq}.tar.bz2"
filename.unlink(missing_ok=True)
shutil.copy(output, filename)
try:
with tarfile.open(output, "x:bz2") as tar:
tar.add(home, arcname="data", filter=output_filter(group, group_seq))
except OSError:
# ignore if the file is not writable when running in bare metal
pass
else:
outdir = Path(outdir)
if outdir.exists():
assert outdir.is_dir()
filename = outdir / f"{group}_{group_seq}.tar.bz2"
filename.unlink(missing_ok=True)
shutil.copy(output, filename)


@cli.command()
Expand All @@ -221,6 +227,22 @@ def generic_gen_txs(options: dict):
return _gen_txs(**options)


@cli.command()
@click.option("--datadir", default="/data", type=Path)
@click.option("--global-seq", default=0)
def generate_load(datadir: Path, global_seq: int):
"""
manually generate load to an existing node
"""
cfg = json.loads((datadir / "config.json").read_text())
txs = prepare_txs(cfg, datadir, global_seq)
asyncio.run(transaction.send(txs))
print("sent", len(txs), "txs")
print("wait for 20 idle blocks")
detect_idle_halted(cfg["num_idle"], 20)
dump_block_stats(sys.stdout)


def _gen_txs(
outdir: str,
nodes: int = 10,
Expand Down Expand Up @@ -248,14 +270,7 @@ def do_run(
datadir: Path, home: Path, cronosd: str, group: str, global_seq: int, cfg: dict
):
if group == FULLNODE_GROUP or cfg.get("validator-generate-load", True):
txs = transaction.load(datadir, global_seq)
if txs:
print("loaded", len(txs), "txs")
else:
print("generating", cfg["num_accounts"] * cfg["num_txs"], "txs")
txs = transaction.gen(
global_seq, cfg["num_accounts"], cfg["num_txs"], cfg["tx_type"]
)
txs = prepare_txs(cfg, datadir, global_seq)
else:
txs = []

Expand Down Expand Up @@ -415,5 +430,17 @@ def wait_for_peers(home: Path):
wait_for_port(ECHO_SERVER_PORT, host=host, timeout=2400)


def prepare_txs(cfg, datadir, global_seq):
txs = transaction.load(datadir, global_seq)
if txs:
print("loaded", len(txs), "txs")
else:
print("generating", cfg["num_accounts"] * cfg["num_txs"], "txs")
txs = transaction.gen(
global_seq, cfg["num_accounts"], cfg["num_txs"], cfg["tx_type"]
)
return txs


if __name__ == "__main__":
cli()
5 changes: 2 additions & 3 deletions testground/benchmark/benchmark/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import ujson

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

GAS_PRICE = 1000000000
CHAIN_ID = 777
LOCAL_JSON_RPC = "http://localhost:8545"
CONNECTION_POOL_SIZE = 1024
TXS_DIR = "txs"
RECIPIENT = "0x1" + "0" * 39
Expand Down Expand Up @@ -128,7 +127,7 @@ async def async_sendtx(session, raw):


async def send(txs):
connector = aiohttp.TCPConnector(limit=1024)
connector = aiohttp.TCPConnector(limit=CONNECTION_POOL_SIZE)
async with aiohttp.ClientSession(
connector=connector, json_serialize=ujson.dumps
) as session:
Expand Down
5 changes: 3 additions & 2 deletions testground/benchmark/benchmark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from web3._utils.transactions import fill_nonce, fill_transaction_defaults

CRONOS_ADDRESS_PREFIX = "crc"
LOCAL_RPC = "http://localhost:26657"
LOCAL_RPC = "http://127.0.0.1:26657"
LOCAL_JSON_RPC = "http://127.0.0.1:8545"


def patch_toml_doc(doc, patch):
Expand Down Expand Up @@ -93,7 +94,7 @@ def wait_for_block(cli, target: int, timeout=40):
def wait_for_w3(timeout=40):
for i in range(timeout):
try:
w3 = web3.Web3(web3.providers.HTTPProvider("http://localhost:8545"))
w3 = web3.Web3(web3.providers.HTTPProvider(LOCAL_JSON_RPC))
w3.eth.get_balance("0x0000000000000000000000000000000000000001")
except: # noqa
time.sleep(1)
Expand Down

0 comments on commit 85ca58b

Please sign in to comment.