2

I have code base for which I am using Click package to manage the CLI. Is there a way to profile the code for optimization using cProfiler?

import cProfile
import click
import io

def profile(fnc):

"""A decorator that uses cProfile to profile a function"""

def inner(*args, **kwargs):

    pr = cProfile.Profile()
    pr.enable()
    retval = fnc(*args, **kwargs)
    pr.disable()
    s = io.StringIO()
    sortby = 'cumulative'
    ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
    ps.print_stats()
    print(s.getvalue())
    return retval

return inner

@click.group()
def cli1():
    pass

@profile
@cli1.command()
@click.option('--zoom', '-z', type=int, default=1)
def fixed(zoom):
   # Function goes here

cli = click.CommandCollection(sources=[cli1, default])

if __name__ == '__main__':
    cli()

I need to profile the fixed function in the Python code.

2
  • What is the point of measuring the performance of a command line processor? Commented Apr 28, 2019 at 1:44
  • I don't want to measure the performance of CLI processor. I wish to check the performance of the individual functions within. Commented Apr 29, 2019 at 6:29

1 Answer 1

10

I needed to do the same, and with some help from your code I came up with this:

import click

@click.group()
@click.option("--profile", is_flag=True)
def main(profile: bool) -> None:
    if profile:
        import cProfile
        import pstats
        import io
        import atexit

        print("Profiling...")
        pr = cProfile.Profile()
        pr.enable()

        def exit():
            pr.disable()
            print("Profiling completed")
            s = io.StringIO()
            pstats.Stats(pr, stream=s).sort_stats("cumulative").print_stats()
            print(s.getvalue())

        atexit.register(exit)

My main function is your cli1 function.

Then you can simply pass the --profile flag to your CLI and it'll profile whatever subcommand of the group you want.

Sign up to request clarification or add additional context in comments.

1 Comment

hooo what an elegant way to integrate this with click ! thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.