pyinstrument.py

Markata plugin to create a pyinstrument profile if pyinstrument is installed.

The profile will be saved to /_profile/index.html


MarkataInstrument class

None

MarkataInstrument source
class MarkataInstrument(Markata):
    should_profile = False
    profiler = None

configure function

set the should_profile variable

configure source
def configure(markata: MarkataInstrument) -> None:
    "set the should_profile variable"

    if "should_profile" not in markata.__dict__.keys():
        try:
            markata.should_profile = markata.config["pyinstrument"]["should_profile"]
        except KeyError:
            markata.should_profile = False

glob function

start the profiler as soon as possible

glob source
def glob(markata: MarkataInstrument) -> None:
    "start the profiler as soon as possible"
    if markata.should_profile and "profiler" not in markata.__dict__.keys():
        try:
            markata.profiler = Profiler(async_mode="disabled")
            markata.profiler.start()
        except NameError:
            "ignore if Profiler does not exist"
            ...

save function

stop the profiler and save as late as possible

save source
def save(markata: MarkataInstrument) -> None:
    "stop the profiler and save as late as possible"
    if markata.should_profile:
        try:

            if "profiler" in markata.__dict__.keys():
                output_file = (
                    Path(markata.config["output_dir"]) / "_profile" / "index.html"
                )
                output_file.parent.mkdir(parents=True, exist_ok=True)
                markata.profiler.stop()
                html = markata.profiler.output_html()
                output_file.write_text(html)
                markata.console.print(markata.profiler.output_text())

        except AttributeError:
            "ignore if markata does not have a profiler attribute"
            ...