forked from crypto-org-chain/cronos
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem: testground block stats don't calculate tps directly
Solution: - add tps calculation, extract from crypto-org-chain#1575
- Loading branch information
Showing
3 changed files
with
65 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters