Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to show total time including children in flat profiles #285

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions pyinstrument/renderers/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ def __init__(
color: bool = False,
flat: bool = False,
time: LiteralStr["seconds", "percent_of_total"] = "seconds",
flat_time: LiteralStr["self", "total"] = "self",
**kwargs: Any,
) -> None:
"""
:param unicode: Use unicode, like box-drawing characters in the output.
:param color: Enable color support, using ANSI color sequences.
:param flat: Display a flat profile instead of a call graph.
:param time: How to display the duration of each frame - ``'seconds'`` or ``'percent_of_total'``
:param flat_time: Show ``'self'`` time or ``'total'`` time (including children) in flat profile.
"""
super().__init__(**kwargs)

self.unicode = unicode
self.color = color
self.flat = flat
self.time = time
self.flat_time = flat_time

if self.flat and self.timeline:
raise Renderer.MisconfigurationError("Cannot use timeline and flat options together.")
Expand Down Expand Up @@ -132,6 +135,8 @@ def render_frame_flat(self, frame: Frame) -> str:
def walk(frame: Frame):
frame_id_to_time[frame.identifier] = (
frame_id_to_time.get(frame.identifier, 0) + frame.total_self_time
if self.flat_time == "self"
else frame.time
)

frame_id_to_frame[frame.identifier] = frame
Expand Down
6 changes: 6 additions & 0 deletions test/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ def test_show_all_doesnt_crash(
):
renderer = frame_renderer_class(show_all=True)
renderer.render(profiler_session)


@pytest.mark.parametrize("flat_time", ["self", "total"])
def test_console_renderer_flat_doesnt_crash(profiler_session, flat_time):
renderer = renderers.ConsoleRenderer(flat=True, flat_time=flat_time)
renderer.render(profiler_session)