From a699f19010fe58b5d59d2a4c11ad9ef139ac7614 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Tue, 17 Sep 2024 09:24:51 +0800 Subject: [PATCH] log tps --- testground/benchmark/benchmark/stateless.py | 40 ++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/testground/benchmark/benchmark/stateless.py b/testground/benchmark/benchmark/stateless.py index 84a8ec58f2..2f63552dc3 100644 --- a/testground/benchmark/benchmark/stateless.py +++ b/testground/benchmark/benchmark/stateless.py @@ -6,6 +6,7 @@ import tarfile import tempfile import time +from datetime import datetime from pathlib import Path from typing import List @@ -410,15 +411,52 @@ def wait_for_peers(home: Path): wait_for_port(ECHO_SERVER_PORT, host=host, timeout=2400) +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(height, num_txs, timestamp): + if height > 1: + prev_block = block(height - 1) + prev_timestamp = prev_block["result"]["block"]["header"]["time"] + time_format = "%Y-%m-%dT%H:%M:%S.%fZ" + current_time, previous_time = ( + datetime.strptime(truncate_fractional_seconds(ts), time_format) + for ts in (timestamp, prev_timestamp) + ) + diff = (current_time - previous_time).total_seconds() + if diff > 0: + tps = num_txs / diff + return tps + else: + return 0 + else: + return 0 + + def dump_block_stats(fp): """ dump simple statistics for blocks for analysis """ + tps_list = [] 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) + tps = calculate_tps(i, txs, timestamp) + print("block", i, txs, timestamp, tps, file=fp) + tps_list.append(tps) + top_tps = sorted(tps_list, reverse=True)[:3] + print("top_tps", top_tps) if __name__ == "__main__":