vpelletier / pprofile

Line-granularity, thread-aware deterministic and statistic pure-python profiler
GNU General Public License v2.0
447 stars 28 forks source link

Allow invoking pprofile as `pprofile <pprofile-opts> -mmodule_name args ...` #18

Closed anntzer closed 8 years ago

anntzer commented 8 years ago

An invocation like

pprofile <pprofile-opts> -mmodule_name args ...

should run the same thing as

python -mmodule_name args ...

but under pprofile.

See https://github.com/bos/statprof.py/pull/17/files#diff-e0307f3515c8c60a5bd1cf7ab0a95ab7R515 for a patch with a similar functionality that I wrote for statprof some time ago. It basically boils down to running the given module with runpy.run_module.

vpelletier commented 8 years ago

Hello. I've started working on this. I can't seem to be able to get anything out of the code executed in this way - besides the overall execution time.

I've pushed my current work in runmodule branch.

vpelletier commented 8 years ago

My bad, I was reusing a recent pprofile command where I also passed --exclude-syspath - so of course nothing gets displayed as profiled module must be in sys.path...

So determinist profiling works. Statistic does not, likely because sys._current_frames gets isolated. I will have to find how to leak it somehow.

vpelletier commented 8 years ago

And I'm wrong again, statistical profiling does work. It is my test-case which is statistic-hostile somehow (doing lots of memory copy from python variables in a C module, probably preventing statistic collection thread from waking up). So after all, this looks like it's working fine as is. Could you confirm ? I'll then rewrite the commit message, push to master and likely release soon after.

anntzer commented 8 years ago

It mostly works, but you are missing https://github.com/bos/statprof.py/pull/17/files#diff-e0307f3515c8c60a5bd1cf7ab0a95ab7R534: if a module is present both in the current directory and in the global site-packages, then python -mfoo runs the version in the current directory whereas your implementation currently runs the global one (because the first entry in sys.path is, for you, the directory that contains the pprofile launcher, rather than the current directory).

I don't know whether that'll be an issue but you may also want to make sure that the final output will likewise contain the correct source filenames.

A good way to test this is to copy out the lib2to3 folder from /usr/lib/pythonX.Y/site-packages to some temporary folder, add a print to lib2to3/__main__.py to make sure of which version you are running, and run pprofile <whatever-options> -mlib2to3 lib2to3 (which funnily enough outputs a lot of diff :-)).

vpelletier commented 8 years ago

I amended the commit:

anntzer commented 8 years ago

Looks good to me now.

vpelletier commented 8 years ago

Released as 1.10.0 .

anntzer commented 8 years ago

Thanks!