@@ -69,6 +69,24 @@ def new_func(ctx, *args, **kwargs):
6969
7070 return update_wrapper (new_func , f )
7171
72+ {% - if cookiecutter .profiling == "Yes" % }
73+ def profile_decorator (f ):
74+ @click .pass_context
75+ def new_func (ctx , * args , ** kwargs ):
76+ if ctx .params ["profiling" ]:
77+ with cProfile .Profile () as profile :
78+ r = ctx .invoke (f , * args , ** kwargs )
79+ with open (ctx .params ["profiling_file" ], "w" ) as sfs :
80+ pstats .Stats (profile , stream = sfs ).strip_dirs ().sort_stats (
81+ ctx .params ["profiling_sort_key" ]
82+ ).print_stats ()
83+ return r
84+ else :
85+ r = ctx .invoke (f , * args , ** kwargs )
86+ return r
87+
88+ return update_wrapper (new_func , f )
89+ {% - endif % }
7290
7391@click .command ()
7492{% - if cookiecutter .file_input == "Yes" % }
@@ -123,8 +141,50 @@ def new_func(ctx, *args, **kwargs):
123141 help = "Set logging level." ,
124142 envvar = "LOG_LEVEL"
125143)
144+ {% - if cookiecutter .profiling == "Yes" % }
145+ @click .option (
146+ "--profiling" ,
147+ default = False ,
148+ is_flag = True ,
149+ help = "Profile the program - get performance data" ,
150+ )
151+ @click .option (
152+ "--profiling-file" ,
153+ help = "Profiling output file" ,
154+ type = click .Path (writable = True , file_okay = True , dir_okay = False ),
155+ default = f"/tmp/{{cookiecutter.project_slug}}_profile.log" ,
156+ show_default = True ,
157+ )
158+ @click .option (
159+ "--profiling-sort-key" ,
160+ help = "Profiling sort key" ,
161+ type = click .Choice (
162+ [
163+ "calls" ,
164+ "cumulative" ,
165+ "cumtime" ,
166+ "file" ,
167+ "filename" ,
168+ "module" ,
169+ "ncalls" ,
170+ "pcalls" ,
171+ "line" ,
172+ "name" ,
173+ "nfl" ,
174+ "stdname" ,
175+ "time" ,
176+ "tottime" ,
177+ ]
178+ ),
179+ default = "cumulative" ,
180+ show_default = True ,
181+ )
182+ {% - endif % }
126183@log_decorator
127184@time_decorator
185+ {% - if cookiecutter .profiling == "Yes" % }
186+ @profile_decorator
187+ {% - endif % }
128188def main (
129189{% - if cookiecutter .file_input == "Yes" % }
130190 input_file ,
0 commit comments