def test_block_threading(self): bts = self.bts b = Blockchain(crea_instance=bts) blocks_no_threading = [] for block in b.blocks(start=self.start, stop=self.stop, threading=False, thread_num=8): blocks_no_threading.append(block) for n in range(5): blocks = [] for block in b.blocks(start=self.start, stop=self.stop, threading=True, thread_num=8): blocks.append(block) for i in range(min(len(blocks), len(blocks_no_threading))): self.assertEqual(blocks[i]["block_id"], blocks_no_threading[i]["block_id"]) self.assertEqual(len(blocks_no_threading), len(blocks))
def test_signing_appbase(self): b = Blockchain(crea_instance=self.bts) st = None for block in b.blocks(start=25304468, stop=25304468): for trx in block.transactions: st = Signed_Transaction(trx.copy()) self.assertTrue(st is not None)
def test_stream_batch(self): bts = self.bts b = Blockchain(crea_instance=bts) ops_stream = [] opNames = ["transfer", "vote"] for op in b.stream(opNames=opNames, start=self.start, stop=self.stop, max_batch_size=self.max_batch_size, threading=False): ops_stream.append(op) self.assertTrue(ops_stream[0]["block_num"] >= self.start) self.assertTrue(ops_stream[-1]["block_num"] <= self.stop) op_stat = b.ops_statistics(start=self.start, stop=self.stop) self.assertEqual(op_stat["vote"] + op_stat["transfer"], len(ops_stream)) ops_blocks = [] for op in b.blocks(start=self.start, stop=self.stop, max_batch_size=self.max_batch_size, threading=False): ops_blocks.append(op) op_stat4 = {"transfer": 0, "vote": 0} self.assertTrue(len(ops_blocks) > 0) for block in ops_blocks: for tran in block["transactions"]: for op in tran['operations']: if isinstance(op, dict) and "type" in op and "value" in op: op_type = op["type"] if len(op_type) > 10 and op_type[len(op_type) - 10:] == "_operation": op_type = op_type[:-10] if op_type in opNames: op_stat4[op_type] += 1 elif op[0] in opNames: op_stat4[op[0]] += 1 self.assertTrue(block.identifier >= self.start) self.assertTrue(block.identifier <= self.stop) self.assertEqual(op_stat["transfer"], op_stat4["transfer"]) self.assertEqual(op_stat["vote"], op_stat4["vote"])
def test_stream(self): bts = self.bts start = self.start stop = self.stop b = Blockchain(crea_instance=bts) ops_stream = [] opNames = ["transfer", "vote"] for op in b.stream(opNames=opNames, start=start, stop=stop): ops_stream.append(op) self.assertTrue(len(ops_stream) > 0) ops_raw_stream = [] opNames = ["transfer", "vote"] for op in b.stream(opNames=opNames, raw_ops=True, start=start, stop=stop): ops_raw_stream.append(op) self.assertTrue(len(ops_raw_stream) > 0) only_ops_stream = [] opNames = ["transfer", "vote"] for op in b.stream(opNames=opNames, start=start, stop=stop, only_ops=True): only_ops_stream.append(op) self.assertTrue(len(only_ops_stream) > 0) only_ops_raw_stream = [] opNames = ["transfer", "vote"] for op in b.stream(opNames=opNames, raw_ops=True, start=start, stop=stop, only_ops=True): only_ops_raw_stream.append(op) self.assertTrue(len(only_ops_raw_stream) > 0) op_stat = b.ops_statistics(start=start, stop=stop) op_stat2 = {"transfer": 0, "vote": 0} for op in ops_stream: self.assertIn(op["type"], opNames) op_stat2[op["type"]] += 1 self.assertTrue(op["block_num"] >= start) self.assertTrue(op["block_num"] <= stop) self.assertEqual(op_stat["transfer"], op_stat2["transfer"]) self.assertEqual(op_stat["vote"], op_stat2["vote"]) op_stat3 = {"transfer": 0, "vote": 0} for op in ops_raw_stream: self.assertIn(op["op"][0], opNames) op_stat3[op["op"][0]] += 1 self.assertTrue(op["block_num"] >= start) self.assertTrue(op["block_num"] <= stop) self.assertEqual(op_stat["transfer"], op_stat3["transfer"]) self.assertEqual(op_stat["vote"], op_stat3["vote"]) op_stat5 = {"transfer": 0, "vote": 0} for op in only_ops_stream: self.assertIn(op["type"], opNames) op_stat5[op["type"]] += 1 self.assertTrue(op["block_num"] >= start) self.assertTrue(op["block_num"] <= stop) self.assertEqual(op_stat["transfer"], op_stat5["transfer"]) self.assertEqual(op_stat["vote"], op_stat5["vote"]) op_stat6 = {"transfer": 0, "vote": 0} for op in only_ops_raw_stream: self.assertIn(op["op"][0], opNames) op_stat6[op["op"][0]] += 1 self.assertTrue(op["block_num"] >= start) self.assertTrue(op["block_num"] <= stop) self.assertEqual(op_stat["transfer"], op_stat6["transfer"]) self.assertEqual(op_stat["vote"], op_stat6["vote"]) ops_blocks = [] for op in b.blocks(start=start, stop=stop): ops_blocks.append(op) op_stat4 = {"transfer": 0, "vote": 0} self.assertTrue(len(ops_blocks) > 0) for block in ops_blocks: for tran in block["transactions"]: for op in tran['operations']: if isinstance(op, list) and op[0] in opNames: op_stat4[op[0]] += 1 elif isinstance(op, dict): op_type = op["type"] if len(op_type) > 10 and op_type[len(op_type) - 10:] == "_operation": op_type = op_type[:-10] if op_type in opNames: op_stat4[op_type] += 1 self.assertTrue(block.identifier >= start) self.assertTrue(block.identifier <= stop) self.assertEqual(op_stat["transfer"], op_stat4["transfer"]) self.assertEqual(op_stat["vote"], op_stat4["vote"]) ops_blocks = [] for op in b.blocks(): ops_blocks.append(op) break self.assertTrue(len(ops_blocks) == 1)
last_block_id = 19273700 last_block = Block(last_block_id, crea_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.crea.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"] 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"])
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/crea-pressure-4-need-for-speed|gandalf" [author, permlink, voter] = resolve_authorpermvoter(authorpermvoter) authorperm = construct_authorperm(author, permlink) last_block_id = 19273700 try: stm = Crea(node=node, num_retries=3, num_retries_call=3, timeout=30) blockchain = Blockchain(crea_instance=stm) blockchain_version = stm.get_blockchain_version() last_block = Block(last_block_id, crea_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 = Crea(node=node, num_retries=3, num_retries_call=3, timeout=30) account = Account("gtg", crea_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 = Crea(node=node, num_retries=3, num_retries_call=3, timeout=30) account = Account("gtg", crea_instance=stm) blockchain_version = stm.get_blockchain_version() start = timer() Vote(authorpermvoter, crea_instance=stm) stop = timer() vote_time = stop - start start = timer() Comment(authorperm, crea_instance=stm) stop = timer() comment_time = stop - start start = timer() Account(author, crea_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}