Skip to content

Commit efed434

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

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

template_profiler_panel/panels/template.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import inspect
22
from collections import defaultdict
3+
from collections.abc import Callable
34
from time import time
45

56
import wrapt
67
from debug_toolbar.panels import Panel
78
from debug_toolbar.panels.sql.utils import contrasting_color_generator
89
import django
910
from django.dispatch import Signal
11+
from django.urls import resolve
12+
1013

1114
if django.VERSION < (3, 2):
1215
from django.utils.translation import ugettext_lazy as _
@@ -23,6 +26,28 @@
2326
node_element_colors = {}
2427

2528

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

78103
have_monkey_patched_template_classes = False
79104

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

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

0 commit comments

Comments
 (0)