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?
    – Stephen Rauch
    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.
    – naseefo
    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.

1
  • 1
    hooo what an elegant way to integrate this with click ! thanks
    – Newtopian
    Sep 9, 2021 at 22:12

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.