Skip to content

Commit

Permalink
Problem: testground block stats don't calculate tps directly (#1586)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Sep 18, 2024
1 parent dad299e commit e0d64e9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
29 changes: 2 additions & 27 deletions testground/benchmark/benchmark/stateless.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from typing import List

import click
import requests
import tomlkit

from .cli import ChainCommand
Expand All @@ -24,16 +23,16 @@
patch_configs,
)
from .sendtx import generate_load
from .stats import dump_block_stats
from .topology import connect_all
from .types import PeerPacket
from .utils import wait_for_block, wait_for_port, wait_for_w3
from .utils import block_height, block_txs, wait_for_block, wait_for_port, wait_for_w3

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


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


def block_height():
rsp = requests.get(f"{LOCAL_RPC}/status").json()
return int(rsp["result"]["sync_info"]["latest_block_height"])


def block(height):
return requests.get(f"{LOCAL_RPC}/block?height={height}").json()


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


def init_node_local(
cli: ChainCommand,
outdir: Path,
Expand Down Expand Up @@ -380,16 +366,5 @@ def wait_for_peers(home: Path):
wait_for_port(ECHO_SERVER_PORT, host=host, timeout=2400)


def dump_block_stats(fp):
"""
dump simple statistics for blocks for analysis
"""
for i in range(1, block_height() + 1):
blk = block(i)
timestamp = blk["result"]["block"]["header"]["time"]
txs = len(blk["result"]["block"]["data"]["txs"])
print("block", i, txs, timestamp, file=fp)


if __name__ == "__main__":
cli()
39 changes: 39 additions & 0 deletions testground/benchmark/benchmark/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from datetime import datetime

from .utils import block, block_height

# the tps calculation use the average of the last 10 blocks
TPS_WINDOW = 10


def calculate_tps(blocks):
if len(blocks) < 2:
return 0

txs = sum(n for n, _ in blocks)
_, t1 = blocks[0]
_, t2 = blocks[-1]
time_diff = (t2 - t1).total_seconds()
if time_diff == 0:
return 0
return txs / time_diff


def dump_block_stats(fp):
"""
dump simple statistics for blocks for analysis
"""
tps_list = []
current = block_height()
blocks = []
# skip block 1 whose timestamp is not accurate
for i in range(2, current + 1):
blk = block(i)
timestamp = datetime.fromisoformat(blk["result"]["block"]["header"]["time"])
txs = len(blk["result"]["block"]["data"]["txs"])
blocks.append((txs, timestamp))
tps = calculate_tps(blocks[-TPS_WINDOW:])
tps_list.append(tps)
print("block", i, txs, timestamp, tps, file=fp)
tps_list.sort(reverse=True)
print("top_tps", tps_list[:5], file=fp)
15 changes: 15 additions & 0 deletions testground/benchmark/benchmark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
from pathlib import Path

import bech32
import requests
import tomlkit
import web3
from eth_account import Account
from hexbytes import HexBytes
from web3._utils.transactions import fill_nonce, fill_transaction_defaults

CRONOS_ADDRESS_PREFIX = "crc"
LOCAL_RPC = "http://localhost:26657"


def patch_dict(doc, kwargs):
Expand Down Expand Up @@ -139,3 +141,16 @@ def gen_account(global_seq: int, index: int) -> Account:
index 0 is reserved for validator account.
"""
return Account.from_key(((global_seq + 1) << 32 | index).to_bytes(32))


def block_height():
rsp = requests.get(f"{LOCAL_RPC}/status").json()
return int(rsp["result"]["sync_info"]["latest_block_height"])


def block(height):
return requests.get(f"{LOCAL_RPC}/block?height={height}").json()


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

0 comments on commit e0d64e9

Please sign in to comment.