Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to profile only certain part of Python process? #531

Open
EricCousineau-TRI opened this issue Oct 25, 2022 · 1 comment
Open

How to profile only certain part of Python process? #531

EricCousineau-TRI opened this issue Oct 25, 2022 · 1 comment

Comments

@EricCousineau-TRI
Copy link

EricCousineau-TRI commented Oct 25, 2022

Not sure if this has already been asked, but was curious if there's a best way to only profile part of process - I'd also be willing to make code modifications if need be.

Motivation is two-fold:

  • I would like to ignore import times for certain things.
  • I am using Bazel w/ additional wrappers. Even using --subprocesses, I found it hard to see the timings I expected (multiple hops with execv, maybe?).

I found this works 'aight, albeit somewhat sloppy:

from contextlib import contextmanager
import os
import signal
import subprocess


@contextmanager
def use_py_spy(output_file, *, sudo=False):
    """Use py-spy in specific context."""
    args = ["py-spy", "record", "-o", output_file, "--pid", str(os.getpid())]
    if sudo:
        args = ["sudo"] + args
    p = subprocess.Popen(args)
    # TODO(eric.cousineau): Startup time of py-spy may lag behind other
    # instructions. However, maybe can assume this isn't critical for profiling
    # purposes?
    yield
    p.send_signal(signal.SIGINT)
    p.wait()


def main():
    with use_py_spy("/tmp/crazy_stuff.svg"):
       do_crazy_stuff()

...

See usecase for CPython API extension stuff:

Thanks for the awesome tool!

@olejorgenb
Copy link

Maybe https://github.com/joerick/pyinstrument is more suited for this? It is designed to be called from within python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants