MCPcopy Create free account
hub / github.com/BerriAI/litellm / PythonProfiler

Class PythonProfiler

tests/rust-python-harness/shared/tracing/profiler.py:31–60  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29
30
31class PythonProfiler:
32 def __init__(self, source_root: Path) -> None:
33 self._source_root: Final = str(source_root.resolve()) + "/"
34 self._seen_frames: Final[set[FrameType]] = set()
35 self._event_ids: Final[dict[FrameType, int]] = {}
36 self._lock: Final = threading.Lock()
37 self.events: Final[list[FunctionTraceEvent]] = []
38
39 def __call__(self, frame: FrameType, event: str, _arg: object) -> None:
40 if event != "call" or frame in self._seen_frames:
41 return
42 function_name: Final = self.function_name(frame)
43 if function_name is None:
44 return
45 with self._lock:
46 event_id: Final = len(self.events)
47 parent_id: Final = next(
48 (self._event_ids[ancestor] for ancestor in _frame_ancestors(frame) if ancestor in self._event_ids),
49 None,
50 )
51 self._seen_frames.add(frame)
52 self._event_ids[frame] = event_id
53 self.events.append(FunctionTraceEvent(id=event_id, parent_id=parent_id, function=function_name))
54
55 def function_name(self, frame: FrameType) -> str | None:
56 code: Final = frame.f_code
57 if not code.co_filename.startswith(self._source_root):
58 return None
59 relative: Final = code.co_filename.removeprefix(self._source_root)
60 return f"{relative}:{code.co_firstlineno} {_qualified_name(frame)}"
61
62
63class PythonFunctionUsageProfiler:

Callers 1

profile_pythonFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected