Skip to content

File: Broken/Core/BrokenProfiler.py

Broken.Core.BrokenProfiler

BrokenProfiler

Source code in Broken/Core/BrokenProfiler.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@define
class BrokenProfiler:
    name: str = "NONE"

    class Profiler(BrokenEnum):
        cprofile      = "cprofile"
        # imports       = "imports"
        # pyinstrument  = "pyinstrument"

    profiler: Profiler = Profiler.cprofile

    @property
    def label(self) -> str:
        return self.name.upper()

    def __attrs_post_init__(self):
        profiler = Environment.get(f"{self.label}_PROFILER", self.profiler)
        self.profiler = self.Profiler.get(profiler)

    @property
    def enabled(self) -> bool:
        return Environment.flag(f"{self.label}_PROFILE", 0)

    @property
    def output(self) -> Path:
        return Path(tempfile.gettempdir())/f"{self.label}.prof"

    __profiler__: Any = None

    def __enter__(self) -> Self:
        if (not self.enabled):
            pass
        elif (self.profiler == self.Profiler.cprofile):
            log.trace("Profiling with cProfile")
            import cProfile
            self.__profiler__ = cProfile.Profile()
            self.__profiler__.enable()
        return self

    def __exit__(self, *args) -> None:
        if (not self.enabled):
            return None

        if (self.profiler == self.Profiler.cprofile):
            log.trace("Finishing cProfile")
            output = self.output.with_suffix(".prof")
            self.__profiler__.disable()
            self.__profiler__.dump_stats(output)
            try:
                shell("snakeviz", output)
            except KeyboardInterrupt:
                pass
            output.unlink()

name

name: str = 'NONE'

Profiler

Bases: BrokenEnum

Source code in Broken/Core/BrokenProfiler.py
14
15
class Profiler(BrokenEnum):
    cprofile      = "cprofile"
cprofile
cprofile = 'cprofile'

profiler

label

label: str

__attrs_post_init__

__attrs_post_init__()
Source code in Broken/Core/BrokenProfiler.py
25
26
27
def __attrs_post_init__(self):
    profiler = Environment.get(f"{self.label}_PROFILER", self.profiler)
    self.profiler = self.Profiler.get(profiler)

enabled

enabled: bool

output

output: Path

__profiler__

__profiler__: Any = None

__enter__

__enter__() -> Self
Source code in Broken/Core/BrokenProfiler.py
39
40
41
42
43
44
45
46
47
def __enter__(self) -> Self:
    if (not self.enabled):
        pass
    elif (self.profiler == self.Profiler.cprofile):
        log.trace("Profiling with cProfile")
        import cProfile
        self.__profiler__ = cProfile.Profile()
        self.__profiler__.enable()
    return self

__exit__

__exit__(*args) -> None
Source code in Broken/Core/BrokenProfiler.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def __exit__(self, *args) -> None:
    if (not self.enabled):
        return None

    if (self.profiler == self.Profiler.cprofile):
        log.trace("Finishing cProfile")
        output = self.output.with_suffix(".prof")
        self.__profiler__.disable()
        self.__profiler__.dump_stats(output)
        try:
            shell("snakeviz", output)
        except KeyboardInterrupt:
            pass
        output.unlink()