Description
@ta946 I'm running into an issue when attempting to use autoprofiling.
Maybe I'm remember how to use it wrong, but if I am, there is something unintuitive about the design we came up with that we might want to address in either the docs or in code changes.
The goal is that I'd like to autoprofile an entire package when running a command line tool. Unfortunately kernprof can't run a python module (e.g. python -m modname), it has to run a script (e.g. python modname.py), which makes this annoying. Ideally if I wanted to profile pip
, I could do something like: python -m kernprof -l --prof-mod pip -m pip
, but that's a separate issue and we can work around it by writing a script that calls the module main function of interest:
echo "
from pip._internal.cli.main import main as _main
_main()
" > my_script_v1.py
chmod +x my_script_v1.py
python -m kernprof -l \
--prof-mod pip \
./my_script_v1.py freeze
python -m line_profiler -rmt "my_script_v1.py.lprof"
Now, I would expect that because we are asking it to profile a package directory that all functions and imports in pip itself would be decorated, but that doesn't seem to be the case. They are recognized, but there seems to be a rule that filters them out.
However, if I explicitly import some of the modules beforehand, I will end up profiling their contents:
echo "
from pip._internal.cli.main import main as _main
import pip._vendor
import pip._internal.utils.hashes
_main()
" > my_script_v2.py
chmod +x my_script_v2.py
python -m kernprof -l \
--prof-mod pip \
./my_script_v2.py freeze
python -m line_profiler -rmt "my_script_v2.py.lprof"
Thinking about this: maybe the problem is that the modules aren't loaded when the AST transformer does its decorations? We probably don't want to do anything that would force a different import order than would happen when running the program naturally, but maybe there is a way to hook into the import system and decorate any module that is within the requested package and imported after profiling starts?