Skip to content

Commit e0d64e9

Browse files
yihuangcoderabbitai[bot]mmsqe
authored
Problem: testground block stats don't calculate tps directly (#1586)
* Problem: testground block stats don't calculate tps directly Solution: - add tps calculation, extract from #1575 * cleanup * Update testground/benchmark/benchmark/stats.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: yihuang <[email protected]> * skip block 1 --------- Signed-off-by: yihuang <[email protected]> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: mmsqe <[email protected]>
1 parent dad299e commit e0d64e9

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

testground/benchmark/benchmark/stateless.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from typing import List
1111

1212
import click
13-
import requests
1413
import tomlkit
1514

1615
from .cli import ChainCommand
@@ -24,16 +23,16 @@
2423
patch_configs,
2524
)
2625
from .sendtx import generate_load
26+
from .stats import dump_block_stats
2727
from .topology import connect_all
2828
from .types import PeerPacket
29-
from .utils import wait_for_block, wait_for_port, wait_for_w3
29+
from .utils import block_height, block_txs, wait_for_block, wait_for_port, wait_for_w3
3030

3131
# use cronosd on host machine
3232
LOCAL_CRONOSD_PATH = "cronosd"
3333
DEFAULT_CHAIN_ID = "cronos_777-1"
3434
# the container must be deployed with the prefixed name
3535
HOSTNAME_TEMPLATE = "testplan-{index}"
36-
LOCAL_RPC = "http://localhost:26657"
3736
ECHO_SERVER_PORT = 26659
3837

3938

@@ -309,19 +308,6 @@ def detect_idle_halted(idle_blocks: int, interval: int, chain_halt_interval=120)
309308
return
310309

311310

312-
def block_height():
313-
rsp = requests.get(f"{LOCAL_RPC}/status").json()
314-
return int(rsp["result"]["sync_info"]["latest_block_height"])
315-
316-
317-
def block(height):
318-
return requests.get(f"{LOCAL_RPC}/block?height={height}").json()
319-
320-
321-
def block_txs(height):
322-
return block(height)["result"]["block"]["data"]["txs"]
323-
324-
325311
def init_node_local(
326312
cli: ChainCommand,
327313
outdir: Path,
@@ -380,16 +366,5 @@ def wait_for_peers(home: Path):
380366
wait_for_port(ECHO_SERVER_PORT, host=host, timeout=2400)
381367

382368

383-
def dump_block_stats(fp):
384-
"""
385-
dump simple statistics for blocks for analysis
386-
"""
387-
for i in range(1, block_height() + 1):
388-
blk = block(i)
389-
timestamp = blk["result"]["block"]["header"]["time"]
390-
txs = len(blk["result"]["block"]["data"]["txs"])
391-
print("block", i, txs, timestamp, file=fp)
392-
393-
394369
if __name__ == "__main__":
395370
cli()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from datetime import datetime
2+
3+
from .utils import block, block_height
4+
5+
# the tps calculation use the average of the last 10 blocks
6+
TPS_WINDOW = 10
7+
8+
9+
def calculate_tps(blocks):
10+
if len(blocks) < 2:
11+
return 0
12+
13+
txs = sum(n for n, _ in blocks)
14+
_, t1 = blocks[0]
15+
_, t2 = blocks[-1]
16+
time_diff = (t2 - t1).total_seconds()
17+
if time_diff == 0:
18+
return 0
19+
return txs / time_diff
20+
21+
22+
def dump_block_stats(fp):
23+
"""
24+
dump simple statistics for blocks for analysis
25+
"""
26+
tps_list = []
27+
current = block_height()
28+
blocks = []
29+
# skip block 1 whose timestamp is not accurate
30+
for i in range(2, current + 1):
31+
blk = block(i)
32+
timestamp = datetime.fromisoformat(blk["result"]["block"]["header"]["time"])
33+
txs = len(blk["result"]["block"]["data"]["txs"])
34+
blocks.append((txs, timestamp))
35+
tps = calculate_tps(blocks[-TPS_WINDOW:])
36+
tps_list.append(tps)
37+
print("block", i, txs, timestamp, tps, file=fp)
38+
tps_list.sort(reverse=True)
39+
print("top_tps", tps_list[:5], file=fp)

testground/benchmark/benchmark/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
from pathlib import Path
55

66
import bech32
7+
import requests
78
import tomlkit
89
import web3
910
from eth_account import Account
1011
from hexbytes import HexBytes
1112
from web3._utils.transactions import fill_nonce, fill_transaction_defaults
1213

1314
CRONOS_ADDRESS_PREFIX = "crc"
15+
LOCAL_RPC = "http://localhost:26657"
1416

1517

1618
def patch_dict(doc, kwargs):
@@ -139,3 +141,16 @@ def gen_account(global_seq: int, index: int) -> Account:
139141
index 0 is reserved for validator account.
140142
"""
141143
return Account.from_key(((global_seq + 1) << 32 | index).to_bytes(32))
144+
145+
146+
def block_height():
147+
rsp = requests.get(f"{LOCAL_RPC}/status").json()
148+
return int(rsp["result"]["sync_info"]["latest_block_height"])
149+
150+
151+
def block(height):
152+
return requests.get(f"{LOCAL_RPC}/block?height={height}").json()
153+
154+
155+
def block_txs(height):
156+
return block(height)["result"]["block"]["data"]["txs"]

0 commit comments

Comments
 (0)