Pyinstrument.Py

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

The profile will be saved to <output_dir>/_profile/index.html

!! class

ProfilerConfig class

"ProfilerConfig source"


        class ProfilerConfig(pydantic.BaseModel):
            output_dir: pydantic.DirectoryPath = Path("markout")
            should_profile: bool = SHOULD_PROFILE
            profiler: Optional[
                Any
            ] = None  # No valicator for type pyinstrument.profiler.Profiler
            output_file: Optional[Path] = None

            @pydantic.validator("output_file", pre=True, always=True)
            def validate_output_file(cls, v, *, values):
                if v is None:
                    output_file = values["output_dir"] / "_profile" / "index.html"
                    output_file.parent.mkdir(parents=True, exist_ok=True)
                    return output_file
                return v

!! class

Config class

"Config source"


        class Config(pydantic.BaseModel):
            profiler: ProfilerConfig = ProfilerConfig()

!! function

config_model function

"config_model source"


        def config_model(markata: "Markata") -> None:
            markata.config_models.append(Config)

!! function

configure function

"configure source"


        def configure(markata: "Markata") -> None:
            # profiler must exist in the same thread and cannot be configured through pydantic validation
            if (
                markata.config.profiler.should_profile
                and markata.config.profiler.profiler is None
            ):
                markata.config.profiler.profiler = Profiler()
                markata.config.profiler.profiler.start()

!! function

save function

stop the profiler and save as late as possible

"save source"


        def save(markata: Markata) -> None:
            "stop the profiler and save as late as possible"
            if markata.config.profiler.should_profile:
                if markata.config.profiler.profiler is not None:
                    if markata.config.profiler.profiler.is_running:
                        try:
                            markata.config.profiler.profiler.stop()
                            html = markata.config.profiler.profiler.output_html()
                            markata.config.profiler.output_file.write_text(html)
                            markata.console.print(
                                markata.config.profiler.profiler.output_text()
                            )

                        except AttributeError:
                            markata.console.log(
                                "profiler not available, skipping save pyinstrument save",
                            )
                            markata.console.log(
                                "[red]to enable profiler [wheat1][itallic]pip install 'markata\[pyinstrument]'",
                            )

!! function

teardown function

stop the profiler on exit

"teardown source"


        def teardown(markata: Markata) -> None:
            "stop the profiler on exit"
            # import logging

            # logger = logging.getLogger()
            # logger.handlers.clear()
            if markata.config.profiler.should_profile:
                if markata.config.profiler.profiler is not None:
                    if markata.config.profiler.profiler.is_running:
                        markata.config.profiler.profiler.stop()

!! method

validate_output_file method

"validate_output_file source"


        def validate_output_file(cls, v, *, values):
                if v is None:
                    output_file = values["output_dir"] / "_profile" / "index.html"
                    output_file.parent.mkdir(parents=True, exist_ok=True)
                    return output_file
                return v