Skip to content

hedera-dev/tutorial-python-tps-calculator

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Try It with GitPod

πŸ’» Quickly run this example in your browser

Hedera Transactions Per Second (TPS) Calculation

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.

1. Fetch Block Data

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)

2. Add Up Transactions

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)

3. Calculate Duration

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_timestamp

4. Calculate TPS πŸŽ‰πŸŽ‰

The 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_second

See the Results!

Simply run the script:

TPS = get_transactions_per_second()
print(f"Hedera TPS:", TPS)
image

About

Calculate the latest TPS number on Hedera using Python.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%