Skip to content

Support kernprof -m module #29

Open

Description

@mgedmin

Sometimes I wish to profile a unit test. To do this I have to do

kernprof -l $(which pytest) path/to/testfile.py::test_that_I_want

It would be nice if kernprof allowed me to run a Python module rather than a script with

kernprof -l -m pytest path/to/testfile.py::test_that_I_want

This is what Python itself (python -m ...) and some related tools like coverage run -m ... allow.

Activity

  1. shouldsee commented on Aug 24, 2023

    @shouldsee

    Is this supported yet?

  2. Erotemic commented on Aug 25, 2023

    @Erotemic
    Member

    It's a good idea. I'll support a PR adding the -m argument to kernprof.

    However, the new docs outline a new alternative that is often easier than using kernprof. As of 4.1.x you can now just from line_profiler import profile and by default the decorator does nothing. But if you set the environment variable LINE_PROFILE=1 or pass a --line-profile on sys.argv the profile decorators will enable and dump a profile of your code at the end of running. This is how I test my modules when I use python -m.

    You can also enable it programatically: e.g.

    from line_profiler import profile
    
    profile.enable()
    
    @profile 
    def slow_func():
        1 + 1
    
    slow_func()
    

    and profile has a configuration dictionary you can use to modify behavior. I'd also accept PRs if this config doesn't have everything necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions