Last active
November 15, 2020 14:58
-
-
Save Someguy123/1adf8b8660087d3b6a9dd834a7dc6ebb to your computer and use it in GitHub Desktop.
Hive/Steem RPC node get_block benchmarker // Python AsyncIO using Privex's steem-async library // block_loader.py
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
#!/usr/bin/env python3.8 | |
""" | |
# ------------------------------------------------------ | |
# | |
# Hive/Steem RPC get_block benchmarker | |
# Written by Someguy123 (github.com/Someguy123) | |
# | |
# (C) 2020 Someguy123 / Privex || License: X11 / MIT | |
# Buy a server from Privex! https://www.privex.io | |
# | |
# ------------------------------------------------------ | |
Usage: | |
# install python3.8 | |
apt update | |
apt install -y python3 python3-pip python3.8 | |
# install steem-async and privex-helpers libraries | |
python3.8 -m pip install -U steem-async privex-helpers | |
# download file to loader.py | |
wget -O loader.py https://gist.githubusercontent.com/Someguy123/1adf8b8660087d3b6a9dd834a7dc6ebb/raw/loader.py | |
# make it executable | |
chmod +x loader.py | |
# show help | |
./loader.py | |
# run the script | |
./loader.py https://anyx.io | |
./loader.py http://direct.hived.privex.io:8291 500 | |
./loader.py https://hived.deathwing.me 50 | |
""" | |
import asyncio | |
import time | |
import sys | |
import logging | |
try: | |
from privex.steem import SteemAsync | |
from privex.helpers import env_csv, env_bool | |
from privex.loghelper import LogHelper | |
except Exception as e: | |
print("Failed to import one or more modules... Error is:", type(e), str(e)) | |
print() | |
print("Please make sure steem-async and privex-helpers are installed into your python3.8:\n") | |
print(" python3.8 -m pip install -U steem-async privex-helpers\n") | |
sys.exit(1) | |
batch_size = 40 | |
block_ranges = env_csv('BLOCK_RANGES', ['1-2000', '2001-5000', '5001-10000', '10001-20000', '20001-50000']) | |
quiet = env_bool('QUIET', True) | |
if quiet: | |
_lh = LogHelper('privex.steem', handler_level=logging.ERROR) | |
_lh.add_console_handler() | |
def copyright(): | |
print("\n ------------------------------------------------------ \n") | |
print(" Hive/Steem RPC get_block benchmarker ") | |
print(" Written by Someguy123 (github.com/Someguy123) ") | |
print() | |
print(" (C) 2020 Someguy123 / Privex || License: X11 / MIT") | |
print(" Buy a server from Privex! https://www.privex.io") | |
print("\n ------------------------------------------------------ \n") | |
if len(sys.argv) < 2: | |
print(f"Usage: {sys.argv[0]} [rpc_node_url] (batch_size={batch_size})") | |
print() | |
print("Examples:\n") | |
print(f" {sys.argv[0]} https://hived.deathwing.me") | |
print(f" BLOCK_RANGES='1-500,1000-2000,3000-6000,6000-10000,10000-50000' {sys.argv[0]} https://hived.deathwing.me") | |
print(f" {sys.argv[0]} http://direct.hived.privex.io 400") | |
print(f" {sys.argv[0]} http://direct.hived.privex.io:8291 1000") | |
print() | |
print("Env vars:\n") | |
print(f" BLOCK_RANGES - default: {','.join(block_ranges)}") | |
print(f" Load these ranges of blocks from rpc_node_url\n") | |
copyright() | |
sys.exit(1) | |
rpc_node = sys.argv[1] | |
rpc_node = rpc_node.split(',') if ',' in rpc_node else [rpc_node] | |
if len(sys.argv) > 2: | |
batch_size = int(sys.argv[2]) | |
#sa = SteemAsync(['http://hived.deathwing.me:32779']) | |
sa = SteemAsync(rpc_node) | |
#sa = SteemAsync(['http://direct.hived.privex.io:8291']) | |
#sa.config_set('batch_size', 50) | |
sa.config_set('batch_size', batch_size) | |
total_blocks = 0 | |
async def bench_blocks(start: int, end: int): | |
global total_blocks | |
total_blocks += end - start | |
st = time.time() | |
print(f"\nGetting blocks {start} to {end}") | |
bk = await sa.get_blocks(start, end) | |
print("result length:", len(bk)) | |
print("item 0 content:", bk[0]) | |
print("Time taken:", time.time() - st, "seconds") | |
async def main(): | |
global total_blocks | |
print("RPC Node:", rpc_node) | |
print("Batch size:", batch_size) | |
print("Block ranges:", block_ranges) | |
print() | |
init_st = time.time() | |
for bran in block_ranges: | |
sblock, eblock = bran.split('-') | |
await bench_blocks(int(sblock), int(eblock)) | |
#await bench_blocks(1, 2000) | |
#await bench_blocks(2001, 5000) | |
#await bench_blocks(5000, 10000) | |
#await bench_blocks(10000, 20000) | |
#await bench_blocks(20000, 50000) | |
print() | |
totalt = time.time() - init_st | |
print("Total blocks:", total_blocks) | |
print("Total time to get blocks:", totalt, "seconds") | |
print("Blocks per second:", total_blocks / totalt) | |
print() | |
if __name__ == '__main__': | |
copyright() | |
time.sleep(1) | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment