def test_block_only_ops(self): bts = self.bts test_block_id = self.test_block_id block = Block(test_block_id, only_ops=True, steem_instance=bts) self.assertEqual(block.identifier, test_block_id) self.assertTrue(isinstance(block.time(), datetime)) self.assertTrue(isinstance(block, dict)) self.assertTrue(len(block.operations)) self.assertTrue(isinstance(block.ops_statistics(), dict)) block2 = Block(test_block_id + 1, steem_instance=bts) self.assertTrue(block2.time() > block.time()) with self.assertRaises(exceptions.BlockDoesNotExistsException): Block(0, steem_instance=bts)
def test_estimate_virtual_op_num(self): stm = self.bts account = Account("gtg", steem_instance=stm) block_num = 21248120 block = Block(block_num, steem_instance=stm) op_num1 = account.estimate_virtual_op_num(block.time(), stop_diff=1, max_count=100) op_num2 = account.estimate_virtual_op_num(block_num, stop_diff=1, max_count=100) op_num3 = account.estimate_virtual_op_num(block_num, stop_diff=100, max_count=100) op_num4 = account.estimate_virtual_op_num(block_num, stop_diff=0.00001, max_count=100) self.assertTrue(abs(op_num1 - op_num2) < 2) self.assertTrue(abs(op_num1 - op_num4) < 2) self.assertTrue(abs(op_num1 - op_num3) < 200) block_diff1 = 0 block_diff2 = 0 for h in account.get_account_history(op_num4 - 1, 1): block_diff1 = (block_num - h["block"]) for h in account.get_account_history(op_num4 + 1, 1): block_diff2 = (block_num - h["block"]) self.assertTrue(block_diff1 > 0) self.assertTrue(block_diff1 < 1000) self.assertTrue(block_diff2 <= 0) self.assertTrue(block_diff2 > -1000)
def test_block_only_ops(self, node_param): if node_param == "non_appbase": bts = self.bts else: bts = self.appbase block = Block(self.test_block_id, only_ops=True, steem_instance=bts) self.assertEqual(block.identifier, self.test_block_id) self.assertTrue(isinstance(block.time(), datetime)) self.assertTrue(isinstance(block, dict)) self.assertTrue(len(block.operations)) self.assertTrue(isinstance(block.ops_statistics(), dict)) block2 = Block(self.test_block_id + 1, steem_instance=bts) self.assertTrue(block2.time() > block.time()) with self.assertRaises(exceptions.BlockDoesNotExistsException): Block(0, steem_instance=bts)
def test_block(self, node_param): if node_param == "normal": bts = self.bts test_block_id = self.test_block_id else: bts = self.testnet test_block_id = self.test_block_id_testnet block = Block(test_block_id, steem_instance=bts) self.assertEqual(block.identifier, test_block_id) self.assertTrue(isinstance(block.time(), datetime)) self.assertTrue(isinstance(block, dict)) self.assertTrue(len(block.operations)) self.assertTrue(isinstance(block.ops_statistics(), dict)) block2 = Block(test_block_id + 1, steem_instance=bts) self.assertTrue(block2.time() > block.time()) with self.assertRaises(exceptions.BlockDoesNotExistsException): Block(0, steem_instance=bts)
def test_estimate_block_num(self): bts = self.bts b = Blockchain(steem_instance=bts) last_block = b.get_current_block() num = last_block.identifier old_block = Block(num - 60, steem_instance=bts) date = old_block.time() est_block_num = b.get_estimated_block_num(date, accurate=False) self.assertTrue((est_block_num - (old_block.identifier)) < 10) est_block_num = b.get_estimated_block_num(date, accurate=True) self.assertTrue((est_block_num - (old_block.identifier)) < 2) est_block_num = b.get_estimated_block_num(date, estimateForwards=True, accurate=True) self.assertTrue((est_block_num - (old_block.identifier)) < 2) est_block_num = b.get_estimated_block_num(date, estimateForwards=True, accurate=False)
]) t.align = "l" for node in nodes: print("Current node:", node) try: stm = Steem(node=node, num_retries=3) blockchain = Blockchain(steem_instance=stm) account = Account("gtg", steem_instance=stm) virtual_op_count = account.virtual_op_count() blockchain_version = stm.get_blockchain_version() last_block_id = 19273700 last_block = Block(last_block_id, steem_instance=stm) startTime = datetime.now() stopTime = last_block.time() + timedelta(seconds=how_many_minutes * 60) ltime = time.time() cnt = 0 total_transaction = 0 start_time = time.time() last_node = blockchain.steem.rpc.url for entry in blockchain.blocks(start=last_block_id, max_batch_size=max_batch_size, threading=threading, thread_num=thread_num): block_no = entry.identifier if "block" in entry: trxs = entry["block"]["transactions"]
def main(args=None): args = parse_args(args) blockchain = args.blockchain nodelist = NodeList() nodelist.update_nodes(weights={"block": 1}) if blockchain == "hive" or blockchain is None: max_batch_size = 50 threading = False thread_num = 16 block_debug = 1000 nodes = nodelist.get_hive_nodes() blk_inst = Hive(node=nodes, num_retries=3, num_retries_call=3, timeout=30) elif blockchain == "blurt": max_batch_size = None threading = False thread_num = 8 block_debug = 20 nodes = [ "https://api.blurt.blog", "https://rpc.blurtworld.com", "https://rpc.blurtworld.com" ] blk_inst = Blurt(node=nodes, num_retries=3, num_retries_call=3, timeout=30) elif blockchain == "steem": max_batch_size = 50 threading = False thread_num = 16 block_debug = 1000 nodes = nodelist.get_steem_nodes() blk_inst = Steem(node=nodes, num_retries=3, num_retries_call=3, timeout=30) else: raise Exception("Wrong parameter, can be hive, blurt or steem") print(blk_inst) block_count = 0 total_ops = 0 total_virtual_ops = 0 total_trx = 0 blocksperday = 20 * 60 * 24 blockchain = Blockchain(blockchain_instance=blk_inst, ) current_block_num = blockchain.get_current_block_num() last_block_id = current_block_num - blocksperday last_block = Block(last_block_id, blockchain_instance=blk_inst) stopTime = last_block.time() + timedelta(seconds=60 * 60 * 24) start = timer() for entry in blockchain.blocks(start=last_block_id, max_batch_size=max_batch_size, threading=threading, thread_num=thread_num): if "block" in entry: block_time = parse_time(entry["block"]["timestamp"]) else: block_time = entry["timestamp"] if block_time > stopTime: break block_count += 1 if "block" in entry: trxs = entry["block"]["transactions"] else: trxs = entry["transactions"] for tx in trxs: total_trx += 1 for op in tx["operations"]: total_ops += 1 ops_per_day = total_ops / block_count * blocksperday if block_count % (block_debug) == 0: print("%d blocks remaining... estimated ops per day: %.1f" % (blocksperday - block_count, ops_per_day)) duration = timer() - start stopTime = last_block.time() + timedelta(seconds=60 * 60 * 24) start = timer() for entry in blockchain.blocks(start=last_block_id, max_batch_size=max_batch_size, threading=threading, thread_num=thread_num, only_virtual_ops=True): block_time = entry["timestamp"] if block_time > stopTime: break for tx in entry["operations"]: for op in tx["op"]: total_virtual_ops += 1 duration = timer() - start print("Received %.2f blocks/s." % (block_count / duration)) print("Bocks: %d, duration %.3f s" % (block_count, duration)) print("Operations per day: %d" % total_ops) print("Trx per day: %d" % total_trx) print("Virtual Operations per day: %d" % total_virtual_ops)
def benchmark_node(node, how_many_minutes=10, how_many_seconds=30): block_count = 0 history_count = 0 access_time = 0 follow_time = 0 blockchain_version = u'0.0.0' successful = True error_msg = None start_total = timer() max_batch_size = None threading = False thread_num = 16 authorpermvoter = u"@gtg/steem-pressure-4-need-for-speed|gandalf" [author, permlink, voter] = resolve_authorpermvoter(authorpermvoter) authorperm = construct_authorperm(author, permlink) last_block_id = 19273700 try: stm = Steem(node=node, num_retries=3, num_retries_call=3, timeout=30) blockchain = Blockchain(steem_instance=stm) blockchain_version = stm.get_blockchain_version() last_block = Block(last_block_id, steem_instance=stm) stopTime = last_block.time() + timedelta(seconds=how_many_minutes * 60) total_transaction = 0 start = timer() for entry in blockchain.blocks(start=last_block_id, max_batch_size=max_batch_size, threading=threading, thread_num=thread_num): block_no = entry.identifier block_count += 1 if "block" in entry: trxs = entry["block"]["transactions"] else: trxs = entry["transactions"] for tx in trxs: for op in tx["operations"]: total_transaction += 1 if "block" in entry: block_time = parse_time(entry["block"]["timestamp"]) else: block_time = parse_time(entry["timestamp"]) if block_time > stopTime: last_block_id = block_no break if timer() - start > how_many_seconds or quit_thread: break except NumRetriesReached: error_msg = 'NumRetriesReached' block_count = -1 except KeyboardInterrupt: error_msg = 'KeyboardInterrupt' # quit = True except Exception as e: error_msg = str(e) block_count = -1 try: stm = Steem(node=node, num_retries=3, num_retries_call=3, timeout=30) account = Account("gtg", steem_instance=stm) blockchain_version = stm.get_blockchain_version() start = timer() for acc_op in account.history_reverse(batch_size=100): history_count += 1 if timer() - start > how_many_seconds or quit_thread: break except NumRetriesReached: error_msg = 'NumRetriesReached' history_count = -1 successful = False except KeyboardInterrupt: error_msg = 'KeyboardInterrupt' history_count = -1 successful = False # quit = True except Exception as e: error_msg = str(e) history_count = -1 successful = False try: stm = Steem(node=node, num_retries=3, num_retries_call=3, timeout=30) account = Account("gtg", steem_instance=stm) blockchain_version = stm.get_blockchain_version() start = timer() Vote(authorpermvoter, steem_instance=stm) stop = timer() vote_time = stop - start start = timer() Comment(authorperm, steem_instance=stm) stop = timer() comment_time = stop - start start = timer() Account(author, steem_instance=stm) stop = timer() account_time = stop - start start = timer() account.get_followers() stop = timer() follow_time = stop - start access_time = (vote_time + comment_time + account_time + follow_time) / 4.0 except NumRetriesReached: error_msg = 'NumRetriesReached' access_time = -1 except KeyboardInterrupt: error_msg = 'KeyboardInterrupt' # quit = True except Exception as e: error_msg = str(e) access_time = -1 return {'successful': successful, 'node': node, 'error': error_msg, 'total_duration': timer() - start_total, 'block_count': block_count, 'history_count': history_count, 'access_time': access_time, 'follow_time': follow_time, 'version': blockchain_version}
num_retries=10) max_batch_size = None threading = True thread_num = 16 elif node_setup == 2: stm = Steem(node=nodes.get_nodes(appbase=False, https=False), num_retries=10) max_batch_size = None threading = True thread_num = 16 blockchain = Blockchain(steem_instance=stm) last_block_id = 19273700 last_block = Block(last_block_id, steem_instance=stm) startTime = datetime.now() stopTime = last_block.time() + timedelta(seconds=how_many_hours * 60 * 60) ltime = time.time() cnt = 0 total_transaction = 0 start_time = time.time() last_node = blockchain.steem.rpc.url print("Current node:", last_node) for entry in blockchain.blocks(start=last_block_id, max_batch_size=max_batch_size, threading=threading, thread_num=thread_num, thread_limit=1200): block_no = entry.identifier if "block" in entry: trxs = entry["block"]["transactions"]
def main(args=None): args = parse_args(args) blockchain = args.blockchain nodelist = NodeList() nodelist.update_nodes(weights={"block": 1}) if blockchain == "hive" or blockchain is None: max_batch_size = 50 threading = False thread_num = 16 block_debug = 1000 nodes = nodelist.get_hive_nodes() blk_inst = Hive(node=nodes, num_retries=3, num_retries_call=3, timeout=30) elif blockchain == "blurt": max_batch_size = None threading = False thread_num = 8 block_debug = 20 nodes = ["https://rpc.blurt.buzz/", "https://api.blurt.blog", "https://rpc.blurtworld.com", "https://rpc.blurtworld.com"] blk_inst = Blurt(node=nodes, num_retries=3, num_retries_call=3, timeout=30) elif blockchain == "steem": max_batch_size = 50 threading = False thread_num = 16 block_debug = 1000 nodes = nodelist.get_steem_nodes() blk_inst = Steem(node=nodes, num_retries=3, num_retries_call=3, timeout=30) else: raise Exception("Wrong parameter, can be hive, blurt or steem") print(blk_inst) block_count = 0 total_ops = 0 total_trx = 0 duration_s = 60 * 60 * 1 blocksperday = int(duration_s / 3) blockchain = Blockchain(blockchain_instance=blk_inst, ) current_block_num = blockchain.get_current_block_num() last_block_id = current_block_num - blocksperday last_block = Block(last_block_id, blockchain_instance=blk_inst) stopTime = last_block.time() + timedelta(seconds=duration_s) start = timer() op_stats = {} for entry in blockchain.blocks(start=last_block_id, max_batch_size=max_batch_size, threading=threading, thread_num=thread_num): if "block" in entry: block_time = parse_time(entry["block"]["timestamp"]) else: block_time = entry["timestamp"] if block_time > stopTime: break block_count += 1 if "block" in entry: trxs = entry["block"]["transactions"] else: trxs = entry["transactions"] for tx in trxs: total_trx += 1 for op in tx["operations"]: if "_operation" in op["type"]: op_type = op["type"][:-10] else: op_type = op["type"] if op_type in op_stats: op_stats[op_type] += 1 else: op_stats[op_type] = 1 total_ops += 1 ops_per_day = total_ops / block_count * blocksperday if block_count % (block_debug) == 0: print("%d blocks remaining... estimated ops per day: %.1f" % (blocksperday - block_count, ops_per_day)) duration = timer() - start t = PrettyTable(["Type", "Count", "percentage"]) t.align = "l" op_list = [] for o in op_stats: op_list.append({"type": o, "n": op_stats[o], "perc": op_stats[o] / total_ops * 100}) op_list_sorted = sorted(op_list, key=lambda x: x['n'], reverse=True) for op in op_list_sorted: t.add_row([op["type"], op["n"], "%.2f %%" % op["perc"]]) print(t)