How to use the yappi.set_tag_callback function in yappi

To help you get started, we’ve selected a few yappi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sumerc / yappi / tests / test_tags.py View on Github external
return tlocal._tag
            except Exception as e:
                #print(e)
                return -1

        def a(tag):
            tlocal._tag = tag

            burn_cpu(0.1)

        _TCOUNT = 5

        ts = []
        yappi.set_clock_type("cpu")
        tlocal._tag = 0
        yappi.set_tag_callback(tag_cbk)
        yappi.start()
        for i in range(_TCOUNT):
            t = threading.Thread(target=a, args=(i + 1, ))
            ts.append(t)

        for t in ts:
            t.start()

        for t in ts:
            t.join()

        yappi.stop()

        traces = yappi.get_func_stats()
        t1 = '''
        ..op/p/yappi/tests/test_tags.py:21 a  5      0.000137  0.500562  0.000000
github sumerc / yappi / tests / test_tags.py View on Github external
self.assert_traces_almost_equal(t1, traces)

        tagged_traces = yappi.get_func_stats(filter={'tag': 1})
        t1 = '''
        ../yappi/tests/utils.py:125 burn_cpu  1      0.000000  0.100062  0.100062
        '''
        self.assert_traces_almost_equal(t1, tagged_traces)

        yappi.clear_stats()

        # test wall
        yappi.set_clock_type("wall")
        yappi.set_tag_callback(tag_cbk)
        yappi.start()
        burn_io(0.1)
        yappi.set_tag_callback(tag_cbk2)
        burn_io(0.1)
        yappi.stop()
        traces = yappi.get_func_stats()
        t1 = '''
        ..p/yappi/tests/utils.py:134 burn_io  2      0.000000  0.208146  0.104073
        '''
        self.assert_traces_almost_equal(t1, traces)

        tagged_traces = yappi.get_func_stats(filter={'tag': 2})
        t1 = '''
        ..p/yappi/tests/utils.py:134 burn_io  1      0.000000  0.105063  0.105063
        '''
        self.assert_traces_almost_equal(t1, tagged_traces)

        yappi.clear_stats()
github sumerc / yappi / tests / utils.py View on Github external
def setUp(self):
        # reset everything back to default
        yappi.stop()
        yappi.clear_stats()
        yappi.set_clock_type('cpu')  # reset to default clock type
        yappi.set_context_id_callback(None)
        yappi.set_context_name_callback(None)
        yappi.set_tag_callback(None)
github sumerc / yappi / tests / test_functionality.py View on Github external
def test_callback_function_int_return_overflow(self):
        # this test is just here to check if any errors are generated, as the err
        # is printed in C side, I did not include it here. THere are ways to test
        # this deterministically, I did not bother
        import ctypes

        def _unsigned_overflow_margin():
            return 2**(ctypes.sizeof(ctypes.c_void_p) * 8) - 1

        def foo():
            pass

        #with utils.captured_output() as (out, err):
        yappi.set_context_id_callback(_unsigned_overflow_margin)
        yappi.set_tag_callback(_unsigned_overflow_margin)
        yappi.start()
        foo()
github sumerc / yappi / tests / test_tags.py View on Github external
def test_invalid_tag(self):

        def tag_cbk():
            return -1

        yappi.set_tag_callback(tag_cbk)
        yappi.start()
        tag_cbk()
        yappi.stop()
        stats = yappi.get_func_stats()
        stat = find_stat_by_name(stats, 'tag_cbk')
        self.assertEqual(stat.ncall, 1)
github sumerc / yappi / tests / test_asyncio_context_vars.py View on Github external
async def profile_tasks_coroutine(self):
        yappi.set_tag_callback(get_async_context_id)
        yappi.start()
        tasks = self.get_tasks()
        await asyncio.gather(*tasks)
        yappi.stop()
github sumerc / yappi / tests / test_tags.py View on Github external
t1 = '''
        ../yappi/tests/utils.py:125 burn_cpu  2      0.000000  0.200156  0.100078
        '''
        self.assert_traces_almost_equal(t1, traces)

        tagged_traces = yappi.get_func_stats(filter={'tag': 1})
        t1 = '''
        ../yappi/tests/utils.py:125 burn_cpu  1      0.000000  0.100062  0.100062
        '''
        self.assert_traces_almost_equal(t1, tagged_traces)

        yappi.clear_stats()

        # test wall
        yappi.set_clock_type("wall")
        yappi.set_tag_callback(tag_cbk)
        yappi.start()
        burn_io(0.1)
        yappi.set_tag_callback(tag_cbk2)
        burn_io(0.1)
        yappi.stop()
        traces = yappi.get_func_stats()
        t1 = '''
        ..p/yappi/tests/utils.py:134 burn_io  2      0.000000  0.208146  0.104073
        '''
        self.assert_traces_almost_equal(t1, traces)

        tagged_traces = yappi.get_func_stats(filter={'tag': 2})
        t1 = '''
        ..p/yappi/tests/utils.py:134 burn_io  1      0.000000  0.105063  0.105063
        '''
        self.assert_traces_almost_equal(t1, tagged_traces)
github sumerc / yappi / tests / test_asyncio_context_vars.py View on Github external
async def profile_tasks_coroutine(self):
        yappi.set_tag_callback(get_async_context_id)
        await super().profile_tasks_coroutine()
github sumerc / yappi / tests / test_tags.py View on Github external
def test_simple_tagging(self):

        def tag_cbk():
            return 1

        def tag_cbk2():
            return 2

        # test cpu-time
        yappi.set_tag_callback(tag_cbk)
        yappi.start()
        burn_cpu(0.1)
        yappi.set_tag_callback(tag_cbk2)
        burn_cpu(0.1)
        yappi.stop()
        traces = yappi.get_func_stats()
        t1 = '''
        ../yappi/tests/utils.py:125 burn_cpu  2      0.000000  0.200156  0.100078
        '''
        self.assert_traces_almost_equal(t1, traces)

        tagged_traces = yappi.get_func_stats(filter={'tag': 1})
        t1 = '''
        ../yappi/tests/utils.py:125 burn_cpu  1      0.000000  0.100062  0.100062
        '''
        self.assert_traces_almost_equal(t1, tagged_traces)

        yappi.clear_stats()
github sumerc / yappi / performance_issue.py View on Github external
from starlette.responses import Response
from starlette.types import ASGIApp
from yappi import YFuncStats

yappi_request_id = ContextVar('yappi_request_id')
yappi_request_id.set(-10)


def get_context_id() -> int:
    try:
        return yappi_request_id.get()
    except LookupError:
        return -2


yappi.set_tag_callback(get_context_id)


class BenchMiddleware(BaseHTTPMiddleware):

    def __init__(self, app: ASGIApp, calls_to_track: Dict[str, str]) -> None:
        self.calls_to_track = calls_to_track
        super().__init__(app, None)

    async def dispatch(self, request: Request, call_next) -> Response:
        ctx_id = id(request)
        yappi_request_id.set(ctx_id)
        assert yappi_request_id.get() == ctx_id
        response = await call_next(request)
        tracked_stats: Dict[str, YFuncStats] = {}

        for name, call_to_track in self.calls_to_track.items():