Skip to content

Commit

Permalink
move empty file checks from profiler to __main__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
furkanonder committed Oct 11, 2023
1 parent 45ffccf commit 75ef6bd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 58 deletions.
35 changes: 18 additions & 17 deletions src/akarsu/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import io
from collections import Counter
from typing import Final

Expand All @@ -24,25 +23,27 @@ def main() -> None:
args = parser.parse_args()

if file := args.file:
with io.open(file) as fp:
with open(file) as fp:
source = fp.read()
events = Akarsu(source, args.file).profile()
counter: Counter = Counter()

print(f"{'Count':>10}{'Event Type':^20}{'Filename(function)':<50}")
for event, count in Counter(events).most_common():
event_type, file_name, func_name = event
counter[event_type] += count
fmt = f"{count:>10}{event_type:^20}{f'{file_name}({func_name})':<50}"
if args.calls:
if event_type in CALL_EVENTS:

if source := source.strip():
events = Akarsu(source, args.file).profile()
counter: Counter = Counter()

print(f"{'Count':>10}{'Event Type':^20}{'Filename(function)':<50}")
for event, count in Counter(events).most_common():
event_type, file_name, func_name = event
counter[event_type] += count
fmt = f"{count:>10}{event_type:^20}{f'{file_name}({func_name})':<50}"
if args.calls:
if event_type in CALL_EVENTS:
print(fmt)
else:
print(fmt)
else:
print(fmt)

print(f"\nTotal number of events: {counter.total()}")
for event_type, count in counter.most_common():
print(f" {event_type} = {count}")
print(f"\nTotal number of events: {counter.total()}")
for event_type, count in counter.most_common():
print(f" {event_type} = {count}")


if __name__ == "__main__":
Expand Down
65 changes: 30 additions & 35 deletions src/akarsu/akarsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,40 @@ def format_func_name(self, event: tuple[str, str, str]) -> tuple[str, str, str]:

def profile(self) -> list[tuple[str, str, str]]:
events = []
indented_code = "\n".join(f"\t{line}" for line in self.code.splitlines())
source = f"def ____wrapper____():\n{indented_code}\n____wrapper____()"
code = compile(source, self.file_name, "exec")

if code := self.code.strip():
indented_code = "\n".join(f"\t{line}" for line in code.splitlines())
source = f"def ____wrapper____():\n{indented_code}\n____wrapper____()"
code = compile(source, self.file_name, "exec") # type:ignore
for event, event_name in TRACKED_EVENTS:

for event, event_name in TRACKED_EVENTS:

def record(
*args: tuple[types.CodeType, int], event_name: str = event_name
) -> None:
code = cast(types.CodeType, args[0])
events.append((event_name, code.co_filename, code.co_name))

MONITOR.register_callback(TOOL, event, record)

def record_call(
code: types.CodeType, offset: int, obj: Any, arg: Any
def record(
*args: tuple[types.CodeType, int], event_name: str = event_name
) -> None:
file_name = code.co_filename
if isinstance(obj, PY_CALLABLES):
events.append(("PY_CALL", file_name, obj.__code__.co_name))
else:
events.append(("C_CALL", file_name, str(obj)))
code = cast(types.CodeType, args[0])
events.append((event_name, code.co_filename, code.co_name))

MONITOR.register_callback(TOOL, event, record)

MONITOR.use_tool_id(TOOL, "Akarsu")
MONITOR.register_callback(TOOL, EVENTS.CALL, record_call)
MONITOR.set_events(TOOL, EVENT_SET)
try:
exec(code)
except:
pass
MONITOR.set_events(TOOL, 0)
MONITOR.free_tool_id(TOOL)
def record_call(code: types.CodeType, offset: int, obj: Any, arg: Any) -> None:
file_name = code.co_filename
if isinstance(obj, PY_CALLABLES):
events.append(("PY_CALL", file_name, obj.__code__.co_name))
else:
events.append(("C_CALL", file_name, str(obj)))

events = [
self.format_func_name(event)
for event in events[2:-3]
if "____wrapper____" not in event[2]
]
MONITOR.use_tool_id(TOOL, "Akarsu")
MONITOR.register_callback(TOOL, EVENTS.CALL, record_call)
MONITOR.set_events(TOOL, EVENT_SET)
try:
exec(code)
except:
pass
MONITOR.set_events(TOOL, 0)
MONITOR.free_tool_id(TOOL)

events = [
self.format_func_name(event)
for event in events[2:-3]
if "____wrapper____" not in event[2]
]
return events
6 changes: 0 additions & 6 deletions tests/test_akarsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ def test_profile_generator(self):
]
self.check_events(events, expected_events)

def test_profile_empty_code(self):
code = ""
events = Akarsu(code, "<string>").profile()
expected_events = []
self.check_events(events, expected_events)

def test_profile_nested_functions(self):
source = textwrap.dedent("""
def foo():
Expand Down

0 comments on commit 75ef6bd

Please sign in to comment.