Skip to content

Commit bfd2b80

Browse files
committed
profile also all methods of view class
1 parent d73a78c commit bfd2b80

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

template_profiler_panel/panels/template.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import inspect
2-
from collections import defaultdict
2+
from collections import Callable, defaultdict
33
from time import time
44

55
import wrapt
66
from debug_toolbar.panels import Panel
77
from debug_toolbar.panels.sql.utils import contrasting_color_generator
88
import django
99
from django.dispatch import Signal
10+
from django.urls import resolve
11+
1012

1113
if django.VERSION < (3, 2):
1214
from django.utils.translation import ugettext_lazy as _
@@ -23,6 +25,28 @@
2325
node_element_colors = {}
2426

2527

28+
@wrapt.decorator
29+
def profile_method(wrapped, instance, args, kwargs):
30+
start = time()
31+
32+
result = wrapped(*args, **kwargs)
33+
end = time()
34+
35+
instance_name = (
36+
(wrapped.__self__.__class__.__name__ + ".")
37+
if hasattr(wrapped, '__self__') else ""
38+
) + wrapped.__name__
39+
template_rendered.send(
40+
sender=instance.__class__,
41+
instance=instance_name,
42+
start=start,
43+
end=end,
44+
processing_timeline=[],
45+
level=1,
46+
)
47+
return result
48+
49+
2650
def get_nodelist_timeline(nodelist, level):
2751
timeline = []
2852
for node in nodelist:
@@ -77,6 +101,22 @@ def __init__(self, *args, **kwargs):
77101

78102
have_monkey_patched_template_classes = False
79103

104+
def generate_stats(self, request, response):
105+
match = resolve(request.path)
106+
func, args, kwargs = match
107+
view_class = getattr(func, 'view_class', None)
108+
if view_class and not hasattr(view_class, '_profile_enabled'):
109+
print(view_class)
110+
view_class._profile_enabled = True
111+
for attr in view_class.__dict__:
112+
print(attr)
113+
if isinstance(getattr(view_class, attr), Callable):
114+
setattr(
115+
view_class,
116+
attr,
117+
profile_method(getattr(view_class, attr)),
118+
)
119+
80120
@classmethod
81121
def monkey_patch_template_classes(cls):
82122
if cls.have_monkey_patched_template_classes:
@@ -160,7 +200,10 @@ def record(self, instance, start, end, level,
160200
if not self.enabled:
161201
return
162202

163-
template_name = instance.name
203+
try:
204+
template_name = instance.name
205+
except AttributeError:
206+
template_name = instance
164207
# Logic copied from django-debug-toolbar:
165208
# https://github.com/jazzband/django-debug-toolbar/blob/5d095f66fde8f10b45a93c0b35be0a85762b0458/debug_toolbar/panels/templates/panel.py#L77
166209
is_skipped_template = isinstance(template_name, str) and (

0 commit comments

Comments
 (0)