Decorator that will run the function and print a line-by-line profile
(func=None, stream=None, precision=1, backend='psutil')
| 1161 | |
| 1162 | |
| 1163 | def profile(func=None, stream=None, precision=1, backend='psutil'): |
| 1164 | """ |
| 1165 | Decorator that will run the function and print a line-by-line profile |
| 1166 | """ |
| 1167 | backend = choose_backend(backend) |
| 1168 | if backend == 'tracemalloc' and has_tracemalloc: |
| 1169 | if not tracemalloc.is_tracing(): |
| 1170 | tracemalloc.start() |
| 1171 | if func is not None: |
| 1172 | get_prof = partial(LineProfiler, backend=backend) |
| 1173 | show_results_bound = partial( |
| 1174 | show_results, stream=stream, precision=precision |
| 1175 | ) |
| 1176 | if iscoroutinefunction(func): |
| 1177 | @wraps(wrapped=func) |
| 1178 | @coroutine |
| 1179 | def wrapper(*args, **kwargs): |
| 1180 | prof = get_prof() |
| 1181 | val = yield from prof(func)(*args, **kwargs) |
| 1182 | show_results_bound(prof) |
| 1183 | return val |
| 1184 | else: |
| 1185 | @wraps(wrapped=func) |
| 1186 | def wrapper(*args, **kwargs): |
| 1187 | prof = get_prof() |
| 1188 | val = prof(func)(*args, **kwargs) |
| 1189 | show_results_bound(prof) |
| 1190 | return val |
| 1191 | |
| 1192 | return wrapper |
| 1193 | else: |
| 1194 | def inner_wrapper(f): |
| 1195 | return profile(f, stream=stream, precision=precision, |
| 1196 | backend=backend) |
| 1197 | |
| 1198 | return inner_wrapper |
| 1199 | |
| 1200 | |
| 1201 | def choose_backend(new_backend=None): |
no test coverage detected