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 458aff0 commit 3b3b778
Show file tree
Hide file tree
Showing 3 changed files with 65 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()
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)
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 3b3b778

Please sign in to comment.