-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagicprof.py
More file actions
51 lines (38 loc) · 1.18 KB
/
magicprof.py
File metadata and controls
51 lines (38 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
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
from __future__ import annotations
import atexit
import cProfile
import functools
import logging
import os
import pathlib
logger_slug = "magicprof"
logger = logging.getLogger(logger_slug)
_prof: cProfile.Profile | None = None
_prof_file: pathlib.Path | None = None
@functools.cache
def _instrument():
global _prof
global _prof_file
p = os.getenv("MAGICPROF")
assert p is not None, (
"magicprof: env var `MAGICPROF` must be set to enable profiling"
)
_prof_file = pathlib.Path(p)
use_subcalls = os.getenv("MAGICPROF_DISABLE_SUBCALLS") is None
use_builtins = os.getenv("MAGICPROF_DISABLE_BUILTINS") is None
logger.debug(f"configuration: subcalls={use_subcalls} builtins={use_builtins}")
_prof = cProfile.Profile(subcalls=use_subcalls, builtins=use_builtins)
_prof.enable()
logger.info("starting profiler")
@functools.cache
def _shutdown():
global _prof
global _prof_file
if _prof is None:
logger.debug("profiler was not instrumented")
return
_prof.disable()
logger.debug("profiling stopped")
_prof.dump_stats(_prof_file)
logger.info(f"profile written to: {_prof_file!r}")
atexit.register(_shutdown)