Skip to content

Instantly share code, notes, and snippets.

@tomih
Created September 12, 2023 09:13
Show Gist options
  • Save tomih/177c40722b06a4de94aff9fa27376b70 to your computer and use it in GitHub Desktop.
Save tomih/177c40722b06a4de94aff9fa27376b70 to your computer and use it in GitHub Desktop.
Simple geth benchmark
from web3 import Web3, HTTPProvider, WebsocketProvider
import time
import sys
from web3.middleware import geth_poa_middleware
def benchmark_geth_node(num_of_blocks, node_address, wss_address):
start_total_time = time.time()
total_block_req = 0
total_balance_req = 0
total_log_req = 0
total_latest_req = 0
total_block_time = 0
total_balance_time = 0
total_log_time = 0
total_latest_time = 0
log_time = 0
counter = 0
w3 = Web3(HTTPProvider(f"{node_address}"))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
if wss_address == "":
websocket_provider = None
else:
websocket_provider = Web3(WebsocketProvider(wss_address))
websocket_provider.middleware_onion.inject(geth_poa_middleware, layer=0)
start_time = time.time()
latest_block_number = w3.eth.block_number
total_latest_time = total_latest_time + (time.time() - start_time)
total_latest_req += 1
block_hashes = []
block_data = []
print(f"Start benchmarking: @ block no. {latest_block_number}")
print(f"http:")
for i in range(num_of_blocks):
block_number = latest_block_number - i
start_time = time.time()
block = w3.eth.get_block(block_number, True)
block_hashes.append(block.hash)
block_time = time.time() - start_time
total_block_time = total_block_time + block_time
total_block_req += 1
counter += 1
block_data.append({'hash': block.hash, 'block_number': block.number, 'http_benchmark': block_time})
miner = block.miner
start_time = time.time()
balanceAtBlock = w3.eth.getBalance(Web3.toChecksumAddress(miner), block_number)
balanceBeforeBlock = w3.eth.getBalance(Web3.toChecksumAddress(miner), block_number - 1)
balance_time = (time.time() - start_time)
total_balance_time = total_balance_time + balance_time
total_balance_req += 2
if block.transactions:
count = 0
start_time = time.time()
for tx in block.transactions:
tx_receipt = w3.eth.get_transaction_receipt(tx['hash'])
total_log_req += 1
count += 1
if count >= 2:
break
log_time = (time.time() - start_time)
total_log_time = total_log_time + log_time
block_size = block.size
block_speed = block_size / block_time
block_size = f'{block.size:7}'
block_time = f'{(block_time * 1000):8.3f}'
balance_time = f'{(balance_time * 1000):8.3f}'
# log_time = f'{(log_time * 1000):8.3f}'
block_speed = f'{(block_speed / 1000):8.3f}'
print(
f"{counter}. Current block: {block_number}, block_size: {repr(block_size)} bytes, block_time: {repr(block_time)} ms, block_speed: {repr(block_speed)} bytes/ms, balance_time: {repr(balance_time)} ms, log_time: {repr(log_time)} ms")
end_total_time = time.time()
total_execution_time = end_total_time - start_total_time
print(f"ws:")
if websocket_provider == None:
print(f"Not available")
block_data = None
else:
for data in block_data:
start_time = time.time()
block = websocket_provider.eth.get_block(data['block_number'], True)
end_time = time.time()
benchmark_time = (end_time - start_time)
data['ws_benchmark'] = benchmark_time
print(f"{data['block_number']}: {benchmark_time*1000}")
result = [{"total_execution_time": total_execution_time,
"total_block_req": total_block_req,
"total_block_time": total_block_time * 1000,
"total_balance_req": total_balance_req,
"total_balance_time": total_balance_time * 1000,
"total_log_req": total_log_req,
"total_log_time": total_log_time * 1000,
"total_latest_req": total_latest_req,
"total_latest_time": total_latest_time * 1000,
"block_data": block_data
}]
return result
# how many blocks to iterate
num_of_blocks = 5
# nodes to benchmark
nodes = [
{"name": "local", "address": "http://localhost:8545/eth", "wss_address": "ws://localhost:8545/eth"},
]
def print_table(block_data):
print(f"{'Block Number':<15} {'HTTP Benchmark (ms)':<20} {'WS Benchmark (ms)'}")
print("="*120)
for data in block_data:
print(f"{'{:<14}'.format(data['block_number'])} {'{:<19}'.format(data['http_benchmark']*1000)} {data['ws_benchmark']*1000}")
results = []
for node in nodes:
print(f"Current node: {node['name']}")
result = benchmark_geth_node(
num_of_blocks, node["address"], node["wss_address"])
results.append({"node_name": node["name"],
"result": result})
print(f"\n=================================")
print(f"Total execution time:")
for result in results:
print(f"{result['node_name']}: {result['result'][0]['total_execution_time']} s")
print(f"\n=================================")
print(f"Average execution time:")
for result in results:
avg_req = result['result'][0]['total_execution_time'] / (result['result'][0]['total_block_req'] + result['result'][0]['total_balance_req'] + result['result'][0]['total_log_req'] + result['result'][0]['total_latest_req'])
print(f"{result['node_name']}: {avg_req} s")
print(f"\n=================================")
print(f"Latest block average request time:")
for result in results:
block_avg = result['result'][0]['total_latest_time'] / result['result'][0]['total_latest_req']
print(f"{result['node_name']}: {block_avg} ms")
print(f"\n=================================")
print(f"Block average request time:")
for result in results:
block_avg = result['result'][0]['total_block_time'] / result['result'][0]['total_block_req']
print(f"{result['node_name']}: {block_avg} ms")
print(f"\n=================================")
print(f"Balance average request time:")
for result in results:
balance_avg = result['result'][0]['total_balance_time'] / result['result'][0]['total_balance_req']
print(f"{result['node_name']}: {balance_avg} ms")
print(f"\n=================================")
print(f"Log average request time:")
for result in results:
log_avg = result['result'][0]['total_log_time'] / result['result'][0]['total_log_req']
print(f"{result['node_name']}: {log_avg} ms")
print(f"\n=================================")
print(f"http vs wss:")
for result in results:
print(f"{result['node_name']}:")
print_table(result['result'][0]["block_data"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment