π» Quickly run this example in your browser
This Python script (also provided in JavaScript - see repo) calculates the Transactions Per Second (TPS) for Hedera by analyzing data from the last few blocks.
Every block on Hedera takes approximately 2 seconds. In our script, we're focusing on the last 5 blocks: Data for the last 5 blocks is retrieved using Hedera's mirror node REST API:
import requests
def get_transactions_per_second():
numBlocks = 5
blocks_url = f"https://mainnet-public.mirrornode.hedera.com/api/v1/blocks?limit={numBlocks}"
response = requests.get(blocks_url)For these 5 blocks, the total number of transactions is calculated by summing up the transaction counts of each block:
blocks = response.json()['blocks']
sum_of_transactions = sum(block['count'] for block in blocks)We determine the total duration of these 5 blocks by calculating the difference between the timestamps of the newest and the oldest block:
newest_block_to_timestamp = float(blocks[0]['timestamp']['to'])
oldest_block_from_timestamp = float(blocks[-1]['timestamp']['from'])
duration = newest_block_to_timestamp - oldest_block_from_timestampThe TPS is simply the total number of transactions in the last 5 blocks (approximately 10 seconds) divided by the actual duration of those blocks.
transactions_per_second = sum_of_transactions / duration
return transactions_per_secondSimply run the script:
TPS = get_transactions_per_second()
print(f"Hedera TPS:", TPS)