Skip to content

Commit

Permalink
Problem: testground block stats don't calculate tps directly
Browse files Browse the repository at this point in the history
Solution:
- add tps calculation, extract from crypto-org-chain#1575
  • Loading branch information
yihuang committed Sep 18, 2024
1 parent e2b0a65 commit db8f676
Show file tree
Hide file tree
Showing 3 changed files with 66 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,17 +23,17 @@
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"
DEFAULT_DENOM = "basecro"
# 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 @@ -291,19 +290,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, group: str, group_seq: int, ip: str
) -> PeerPacket:
Expand Down Expand Up @@ -354,16 +340,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()
48 changes: 48 additions & 0 deletions testground/benchmark/benchmark/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 truncate_fractional_seconds(timestamp):
(
date_time_part,
_,
_,
) = timestamp.partition("Z")
if "." in date_time_part:
date_time_part, fractional_part = date_time_part.split(".")
fractional_part = fractional_part[:6]
return f"{date_time_part}.{fractional_part}Z"
return timestamp


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

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


def dump_block_stats(fp):
"""
dump simple statistics for blocks for analysis
"""
tps_list = []
current = block_height()
blocks = []
for i in range(1, 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)
16 changes: 16 additions & 0 deletions testground/benchmark/benchmark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +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

LOCAL_RPC = "http://localhost:26657"


def patch_dict(doc, kwargs):
for k, v in kwargs.items():
Expand Down Expand Up @@ -124,3 +127,16 @@ def send_transactions(w3, txs, acct, wait=True):
def export_eth_account(cli, name: str, **kwargs) -> Account:
kwargs.setdefault("keyring_backend", "test")
return Account.from_key(cli("keys", "unsafe-export-eth-key", name, **kwargs))


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 db8f676

Please sign in to comment.