| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import contextlib |
| 4 |
from typing import Any, List |
| 5 |
|
| 6 |
from pyinstrument import processors |
| 7 |
from pyinstrument.frame import Frame |
| 8 |
from pyinstrument.session import Session |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
ProcessorList = List[processors.ProcessorType] |
| 14 |
|
| 15 |
|
| 16 |
class Renderer: |
| 17 |
""" |
| 18 |
Abstract base class for renderers. |
| 19 |
""" |
| 20 |
|
| 21 |
output_file_extension: str = "txt" |
| 22 |
""" |
| 23 |
Renderer output file extension without dot prefix. The default value is `txt` |
| 24 |
""" |
| 25 |
|
| 26 |
output_is_binary: bool = False |
| 27 |
""" |
| 28 |
Whether the output of this renderer is binary data. The default value is `False`. |
| 29 |
""" |
| 30 |
|
| 31 |
def __init__(self): |
| 32 |
pass |
| 33 |
|
| 34 |
def render(self, session: Session) -> str: |
| 35 |
""" |
| 36 |
Return a string that contains the rendered form of `frame`. |
| 37 |
""" |
| 38 |
raise NotImplementedError() |
| 39 |
|
| 40 |
class MisconfigurationError(Exception): |
| 41 |
pass |
| 42 |
|
| 43 |
|
| 44 |
class FrameRenderer(Renderer): |
| 45 |
""" |
| 46 |
An abstract base class for renderers that process Frame objects using |
| 47 |
processor functions. Provides a common interface to manipulate the |
| 48 |
processors before rendering. |
| 49 |
""" |
| 50 |
|
| 51 |
processors: ProcessorList |
| 52 |
""" |
| 53 |
Processors installed on this renderer. This property is defined on the |
| 54 |
base class to provide a common way for users to add and |
| 55 |
manipulate them before calling :func:`render`. |
| 56 |
""" |
| 57 |
|
| 58 |
processor_options: dict[str, Any] |
| 59 |
""" |
| 60 |
Dictionary containing processor options, passed to each processor. |
| 61 |
""" |
| 62 |
|
| 63 |
show_all: bool |
| 64 |
timeline: bool |
| 65 |
|
| 66 |
def __init__( |
| 67 |
self, |
| 68 |
show_all: bool = False, |
| 69 |
timeline: bool = False, |
| 70 |
processor_options: dict[str, Any] | None = None, |
| 71 |
): |
| 72 |
""" |
| 73 |
:param show_all: Don't hide or filter frames - show everything that pyinstrument captures. |
| 74 |
:param timeline: Instead of aggregating time, leave the samples in chronological order. |
| 75 |
:param processor_options: A dictionary of processor options. |
| 76 |
""" |
| 77 |
|
| 78 |
|
| 79 |
self.processors = self.default_processors() |
| 80 |
self.processor_options = processor_options or {} |
| 81 |
|
| 82 |
self.show_all = show_all |
| 83 |
self.timeline = timeline |
| 84 |
|
| 85 |
if show_all: |
| 86 |
for p in ( |
| 87 |
processors.group_library_frames_processor, |
| 88 |
processors.remove_importlib, |
| 89 |
processors.remove_irrelevant_nodes, |
| 90 |
processors.remove_tracebackhide, |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
): |
| 97 |
with contextlib.suppress(ValueError): |
| 98 |
|
| 99 |
self.processors.remove(p) |
| 100 |
|
| 101 |
if timeline: |
| 102 |
with contextlib.suppress(ValueError): |
| 103 |
self.processors.remove(processors.aggregate_repeated_calls) |
| 104 |
|
| 105 |
def default_processors(self) -> ProcessorList: |
| 106 |
""" |
| 107 |
Return a list of processors that this renderer uses by default. |
| 108 |
""" |
| 109 |
raise NotImplementedError() |
| 110 |
|
| 111 |
def preprocess(self, root_frame: Frame | None) -> Frame | None: |
| 112 |
frame = root_frame |
| 113 |
for processor in self.processors: |
| 114 |
frame = processor(frame, options=self.processor_options) |
| 115 |
return frame |
| 116 |
|
| 117 |
def render(self, session: Session) -> str: |
| 118 |
""" |
| 119 |
Return a string that contains the rendered form of `frame`. |
| 120 |
""" |
| 121 |
raise NotImplementedError() |