Skip to content

Commit

Permalink
v0.6.0 Simplify _unwrap_closure_and_profile and pull django generic v…
Browse files Browse the repository at this point in the history
…iew processing into it

Hopefully Closes #4
  • Loading branch information
dmclain committed Mar 3, 2016
1 parent d5f9a96 commit 82a089e
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions debug_toolbar_line_profiler/panel.py
Expand Up @@ -170,32 +170,27 @@ def _unwrap_closure_and_profile(self, func):
self.line_profiler.add_function(func)
for subfunc in getattr(func, 'profile_additional', []):
self._unwrap_closure_and_profile(subfunc)
if func.__closure__:
for cell in func.__closure__:
if hasattr(cell.cell_contents, '__code__'):
self._unwrap_closure_and_profile(cell.cell_contents)

def process_view(self, request, view_func, view_args, view_kwargs):
self.view_func = view_func
self.profiler = cProfile.Profile()
args = (request,) + view_args
self.line_profiler = LineProfiler()
self._unwrap_closure_and_profile(view_func)
if PY2:
view_func_name = view_func.func_globals['__name__']
func_closure = func.func_closure
else:
view_func_name = view_func.__globals__['__name__']
if view_func_name == 'django.views.generic.base':
if PY2:
func_closure = view_func.func_closure
else:
func_closure = view_func.__closure__
func_closure = func.__closure__

if func_closure:
for cell in func_closure:
target = cell.cell_contents
if hasattr(target, '__code__'):
self._unwrap_closure_and_profile(cell.cell_contents)
if inspect.isclass(target) and View in inspect.getmro(target):
for name, value in inspect.getmembers(target):
if name[0] != '_' and inspect.ismethod(value):
self._unwrap_closure_and_profile(value)

def process_view(self, request, view_func, view_args, view_kwargs):
self.view_func = view_func
self.profiler = cProfile.Profile()
args = (request,) + view_args
self.line_profiler = LineProfiler()
self._unwrap_closure_and_profile(view_func)
signals.profiler_setup.send(sender=self,
profiler=self.line_profiler,
view_func=view_func,
Expand Down Expand Up @@ -229,14 +224,19 @@ def add_node(self, func_list, func, max_depth, cum_time=0.1):
return

# func.subfuncs returns FunctionCall objects
for subfunc in func.subfuncs():
subs = sorted(func.subfuncs(), key=FunctionCall.cumtime, reverse=True)
for subfunc in subs:
# a sub function is important if it takes a long time or it has
# line_stats
if (subfunc.cumtime >= cum_time or
if (subfunc.cumtime() >= cum_time or
(hasattr(self.stats, 'line_stats') and
subfunc.func in self.stats.line_stats.timings)):
func.has_subfuncs = True
self.add_node(func_list, subfunc, max_depth, cum_time=cum_time)
self.add_node(
func_list=func_list,
func=subfunc,
max_depth=max_depth,
cum_time=subfunc.cumtime()/16)

def process_response(self, request, response):
if not hasattr(self, 'profiler'):
Expand Down

0 comments on commit 82a089e

Please sign in to comment.