Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Sep 13, 2024
1 parent 3473b73 commit 820eb68
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions testground/benchmark/benchmark/stateless.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,21 +321,29 @@ def truncate_fractional_seconds(timestamp):
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
def calculate_tps(start_height, end_height):
total_txs = 0
total_time = 0

for height in range(start_height + 1, end_height):
blk = block(height)
timestamp = blk["result"]["block"]["header"]["time"]
prev_blk = block(height - 1)
txs = len(prev_blk["result"]["block"]["data"]["txs"])
if txs > 0:
prev_timestamp = prev_blk["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()
total_time += diff
total_txs += txs

if total_time > 0:
average_tps = total_txs / total_time
return average_tps
else:
return 0

Expand All @@ -345,13 +353,14 @@ def dump_block_stats(fp):
dump simple statistics for blocks for analysis
"""
tps_list = []
for i in range(1, block_height() + 1):
current = block_height()
for i in range(1, current + 1):
blk = block(i)
timestamp = blk["result"]["block"]["header"]["time"]
txs = len(blk["result"]["block"]["data"]["txs"])
tps = calculate_tps(i, txs, timestamp)
print("block", i, txs, timestamp, tps, file=fp)
tps = calculate_tps(i, min(i + 2, current))
tps_list.append(tps)
print("block", i, txs, timestamp, tps, file=fp)
top_tps = sorted(tps_list, reverse=True)[:3]
print("top_tps", top_tps)

Expand Down

0 comments on commit 820eb68

Please sign in to comment.