Skip to content

Commit

Permalink
restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasdiener committed May 16, 2023
1 parent bcca59c commit d52a191
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions pyinstrument/renderers/console.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import time
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple

import pyinstrument
Expand Down Expand Up @@ -148,33 +147,25 @@ def render_frame(self, frame: Frame, indent: str = "", child_indent: str = "") -
return result

def render_frame_flat(self, frame: Frame) -> str:
@dataclass(frozen=True, eq=True)
class FrameDesc:
code_position_short: Optional[str]
function: str

def walk(frame: Frame):
frame_desc = FrameDesc(frame.code_position_short, frame.function)
frame_desc_to_self_time[frame_desc] = (
frame_desc_to_self_time.get(frame_desc, 0) + frame.total_self_time
frame_id_to_time_frame[frame.identifier] = (
frame_id_to_time_frame.get(frame.identifier, 0) + frame.total_self_time,
frame,
)
frame_desc_to_frame[frame_desc] = frame

for child in frame.children:
walk(child)

frame_desc_to_self_time: Dict[FrameDesc, float] = {}
frame_desc_to_frame: Dict[FrameDesc, Frame] = {}
frame_id_to_time_frame: Dict[str, Tuple[float, Frame]] = {}

walk(frame)

cost_list: List[Tuple[FrameDesc, float]] = sorted(
frame_desc_to_self_time.items(), key=(lambda item: item[1]), reverse=True
cost_list: List[Tuple[str, float]] = sorted(
frame_id_to_time_frame.items(), key=(lambda item: item[1][0]), reverse=True
)

res = ""

for frame_desc, self_time in cost_list:
for _frame_id, (self_time, cur_frame) in cost_list:
if self.time == "percent_of_total":
val = self_time / frame.time * 100
unit = "%"
Expand All @@ -189,9 +180,9 @@ def walk(frame: Frame):
val=val,
unit=unit,
c=self.colors,
name_color=self._ansi_color_for_name(frame_desc_to_frame[frame_desc]),
function=frame_desc.function,
code_position=frame_desc.code_position_short,
name_color=self._ansi_color_for_name(cur_frame),
function=cur_frame.function,
code_position=cur_frame.code_position_short,
)

return res
Expand Down

0 comments on commit d52a191

Please sign in to comment.