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.