async def main(): with profiler.profile("Main"): g1 = asyncio.gather(*[hello1()]) g2 = asyncio.gather(*[hello2()]) groups = asyncio.gather(g2, g1) with profiler.profile("Groups"): await groups print(await g1) print(await g2) print(await groups)
async def hello1(): with profiler.profile("Hello1"): await asyncio.sleep(2) # Async Sleep for 5 seconds print("hello 1") return "hello11"
def test_method_trace(new_profiler): @profiler.profile_func def inner1(): time.sleep(0.1) @profiler.profile_func def inner2(): time.sleep(0.05) with profiler.profile("binder"): for x in range(5): inner1() inner2() f = io.StringIO() profiler.print_run_stats(file=f) stats_str = f.getvalue() splits = stats_str.split(":\n") binder = splits[1] assert_stats_list(binder, 1, [0.75, 0.76,], [0.75, 0.76,]) inner1 = splits[2] assert_stats(inner1, 5, 0.5, 0.1, 0.1, 0.1, 0.1) inner2 = splits[3] assert_stats_list(inner2, 5, [0.25, 0.26, 0.27],[0.05])
def test_trace_hierachy(new_profiler): with profiler.profile("top"): for x in range(5): with profiler.profile("in1"): time.sleep(0.1) with profiler.profile(("in2")): time.sleep(0.15) f = io.StringIO() profiler.print_run_stats(file=f) stats_str = f.getvalue() splits = stats_str.split(":\n") in1 = splits[1] assert_stats(in1, 5, 0.5, 0.1, 0.1, 0.1, 0.1) in2 = splits[2] assert_stats(in2, 5, 0.75, 0.15, 0.15, 0.15, 0.15) top = splits[3] assert_stats_list(top, 1, [1.25, 1.26], [1.25, 1.26])
def insert_transaction(c: MySQLCursor, t: Transaction, coin_type): with profiler.profile("sql: insert tx"): if coin_type == 'xmr': c.execute(INSERT_TRANSACTION_XMR, (t.version, t.hash_hex, t.fee, t.block_height, t.unlock_time, t.tx_extra)) elif coin_type == 'xhv': c.execute( INSERT_TRANSACTION_XHV, (t.version, t.hash_hex, t.fee, t.block_height, t.unlock_time, t.tx_extra, t.amount_burnt, t.amount_minted)) else: raise NotImplementedError(f"Uknown coint type: {coin_type}") t.tx_id = c.lastrowid #print(f"inserted transaction, rows updated{c.rowcount}") for tx_in in t.tx_ins: with profiler.profile("sql: insert txin"): c.execute(INSERT_TXIN, (t.tx_id, tx_in.amount, tx_in.key_image, tx_in.coinbase)) tx_in.txin_id = c.lastrowid for keyoffset in tx_in.key_offsets: with profiler.profile("sql: insert key offset"): c.execute(INSERT_KEY_OFFSET, (tx_in.txin_id, keyoffset)) for od in tx_in.out_details: od.tx_id = t.tx_id with profiler.profile("sql: insert output details"): c.execute(INSERT_OUTPUT_DETAILS, (tx_in.txin_id, od.tx_id, od.height, od.key_hex, od.mask_hex, od.unlocked)) for tx_out in t.tx_outs: with profiler.profile("sql: insert txout"): if coin_type == 'xmr': c.execute(INSERT_TXOUT_XMR, (t.tx_id, tx_out.amount, tx_out.target_key)) elif coin_type == 'xhv': c.execute(INSERT_TXOUT_XHV, (t.tx_id, tx_out.amount, tx_out.target_key, tx_out.tx_type, tx_out.asset_type)) else: raise NotImplementedError(f"Uknown coint type: {coin_type}") pass
def test_different_ranges(new_profiler): for x in range(20): with profiler.profile("sleep-one"): time.sleep(0.1) for x in range(10): with profiler.profile("sleep-two"): sleeptime = 0.1*(1+x%2) time.sleep(sleeptime) f = io.StringIO() profiler.print_run_stats(file=f) stats_str = f.getvalue() splits = stats_str.split("sleep-two") assert len(splits) == 2, "Should have two profiled elements: sleep-one and sleep-two" sleep_one = splits[0] assert_stats(sleep_one, 20, 2.0, 0.10, 0.10, 0.10, 0.10) sleep_two = splits[1] assert_stats(sleep_two, 10, 1.50, 0.10, 0.20, 0.15, 0.15) f = io.StringIO() profiler.print_csv(file=f) stats_str = f.getvalue() split = stats_str.split("\n") assert len(split) == 22 df = pd.read_csv(io.StringIO(stats_str)) print(df.describe()) df_s1 = df["sleep-one"] nans = df_s1.isna().sum() assert nans == 0 mean = df_s1.mean() assert mean == pytest.approx(0.1, 0.05) df_s2 = df["sleep-two"] nans = df_s2.isna().sum() assert nans == 10 mean = df_s2.mean() print(mean) assert mean == pytest.approx(0.15, 0.05)
def insert_block(conn: MySQLConnection, block: Block, coin_type: str): c = conn.cursor() try: b = block with profiler.profile("sql: insert block"): c.execute( INSERT_BLOCK, (b.height, b.block_size, b.block_weight, b.difficulty, b.cumulative_difficulty, b.hash, b.long_term_weight, b.major_version, b.minor_version, b.nonce, b.reward, b.timestamp, b.wide_cumulative_difficulty, b.wide_difficulty)) bt = b.miner_tx with profiler.profile("insert cb transaction"): insert_transaction(c, bt, coin_type) # c.execute(INSERT_TRANSACTION, (bt.version, bt.hash_hex, bt.fee, bt.block_height, bt.unlock_time)) bt.tx_id = c.lastrowid for t in b.txs: # hash_value = int(t.hash_hex, 16) with profiler.profile("insert transaction"): insert_transaction(c, t, coin_type) # c.execute(INSERT_TRANSACTION, (t.version, t.hash_hex, t.fee, t.block_height, t.unlock_time)) # t.tx_id = c.lastrowid # print(f"inserted transaction, rows updated{c.rowcount}") # for tx_in in t.tx_ins: # c.execute(INSERT_TXIN, (t.tx_id, tx_in.amount, tx_in.key_image, tx_in.coinbase)) # tx_in.txin_id = c.lastrowid # for keyoffset in tx_in.key_offsets: # c.execute(INSERT_KEY_OFFSET, (tx_in.txin_id, keyoffset)) # for od in tx_in.out_details: # od.tx_id = t.tx_id # c.execute(INSERT_OUTPUT_DETAILS, (od.tx_id, od.height, od.key_hex, od.mask_hex, od.unlocked)) # for tx_out in t.tx_outs: # c.execute(INSERT_TXOUT, (t.tx_id, tx_out.amount, tx_out.target_key)) conn.commit() c.close() except Exception as e: logger.exception(f"ERROR AT HEIGHT:{block.height}" ) #should automatically dump the stack conn.rollback() c.close()
def test_throw_error(new_profiler): for x in range(20): with profiler.profile("sleep-one"): time.sleep(0.1) try: with profiler.profile("throw error"): time.sleep(0.1) if (x > 1): raise Exception("oops") except: pass f = io.StringIO() profiler.print_run_stats(file=f) stats_str = f.getvalue() splits = stats_str.split(":\n") in1 = splits[1] assert_stats(in1, 20, 2.0, 0.1, 0.1, 0.1, 0.1) in2 = splits[2] assert_stats(in2, 2, 0.2, 0.1, 0.1, 0.1, 0.1)
def test_range(new_profiler): for x in range(20): with profiler.profile("sleep-one"): time.sleep(0.1) with profiler.profile("sleep-two"): time.sleep(0.2) f = io.StringIO() profiler.print_run_stats(file=f) stats_str = f.getvalue() splits = stats_str.split("sleep-two") assert len(splits) == 2, "Should have two profiled elements: sleep-one and sleep-two" sleep_one = splits[0] assert_stats(sleep_one, 20, 2.0, 0.10, 0.10, 0.10, 0.10) sleep_two = splits[1] assert_stats(sleep_two, 20, 4.0, 0.20, 0.20, 0.20, 0.20) f = io.StringIO() profiler.print_csv(file=f) stats_str = f.getvalue() split = stats_str.split("\n") assert len(split) == 22 df = pd.read_csv(io.StringIO(stats_str)) print(df.describe()) df_s1 = df["sleep-one"] nans = df_s1.isna().sum() assert nans == 0 mean = df_s1.mean() #https://stackoverflow.com/questions/8560131/pytest-assert-almost-equal assert mean == pytest.approx(0.1, 0.05) df_s2 = df["sleep-two"] nans = df_s2.isna().sum() assert nans == 0 mean = df_s2.mean() assert mean == pytest.approx(0.2, 0.05)
def test_count_zero(new_profiler): for x in range(2): with profiler.profile("sleep-one"): time.sleep(0.1) profiler.counts["zero_count"] = 0 profiler.cumulative_times["zero_count"] = 0 f = io.StringIO() profiler.print_run_stats(file=f) stats_str = f.getvalue() splits = stats_str.split(":\n") in1 = splits[1] assert_stats(in1, 2, 0.2, 0.1, 0.1, 0.1, 0.1) in2 = splits[2] assert_stats(in2, 0, 0, 0, 0, "NA", "NA")
__author__ = 'teemu kanstren' import time import codeprofile.profiler as profiler def foo(): time.sleep(0.1) @profiler.profile_func def bar(): time.sleep(0.2) bar() profiler.print_run_stats() for x in range(20): with profiler.profile("foo-perf"): foo() with profiler.profile("bar-perf"): bar() #print(times) profiler.print_run_stats() profiler.print_csv()
#genesis_block = daemon.get_block(height=0) mtx = daemon.get_transactions(["c6988cbd8eec02efdb6ce8e43e5c54c8af898dec8d331025248a066645a259dd"]) create_tables.main() cnx = create_tables.get_cnx() db_height = sql.get_max_block(cnx) if db_height[0] is None: #this is the case when the table is empty db_height = (0,) print(f"table height: ${db_height}") profiler.collect_raw = False block = daemon.get_block(height=884280) top_height = daemon.get_height() for x in range(db_height[0] + 1, top_height): with profiler.profile("get block"): block = daemon.get_block(height=x) with profiler.profile("insert block"): sql.insert_block(cnx, block) if x%500 == 0: logger.info(f"block height: {x}") hack_f = io.StringIO() profiler.print_run_stats(file=hack_f) stats_str = hack_f.getvalue() logger.info(stats_str) #print(block) #coinbase_tx_hash = block["block_header"]["miner_tx_hash"] #print(coinbase_tx_hash) #cb_transactions = daemon.get_transactions([coinbase_tx_hash]) #print(cb_transactions)
async def run_async_func(): with profiler.profile("async-top"): coroutines = [] for x in range(500): coroutines.append(a_sleeper_2()) await asyncio.gather(*coroutines)
async def a_sleeper(): with profiler.profile("async-inner"): await asyncio.sleep(random.randint(1,4))