Example #1
0
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])
Example #2
0
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")
Example #3
0
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])
Example #4
0
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)
Example #5
0
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)
Example #6
0
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)
__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()
Example #8
0
    #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)
#print(block.txs)
mempool = daemon.mempool()
print(mempool)
top_height = daemon.get_height() - 1
block = daemon.get_block(height=top_height)
pass

# @profiler.profile_async_func
async def hello2():
    with profiler.profile("Hello2"):
        await asyncio.sleep(1)  # Async Sleep for 5 seconds

        print("hello 2")

        return "hello22"


# @profiler.profile_async_func
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)


if __name__ == "__main__":
    asyncio.run(main())
    profiler.print_run_stats("Main", "Hello1", "Hello2", "Groups")
Example #10
0
def test_asyncio_trace_func(new_profiler):
    asyncio.run(run_async_func())
    profiler.print_run_stats()