Skip to content

[experimental] Profile methods of view class #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion template_profiler_panel/panels/template.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import inspect
from collections import defaultdict
from collections.abc import Callable
from time import time

import wrapt
from debug_toolbar.panels import Panel
from debug_toolbar.panels.sql.utils import contrasting_color_generator
import django
from django.dispatch import Signal
from django.urls import resolve


if django.VERSION < (3, 2):
from django.utils.translation import ugettext_lazy as _
Expand All @@ -23,6 +26,66 @@
node_element_colors = {}


@wrapt.decorator
def profile_method(wrapped, instance, args, kwargs):
"""

Profile method decorator
Usage:

from template_profiler_panel.panels.template import profile_method
@profile method
def method():
...
"""
start = time()

result = wrapped(*args, **kwargs)
end = time()

instance_name = (
(wrapped.__self__.__class__.__name__ + ".")
if hasattr(wrapped, '__self__') else ""
) + wrapped.__name__
template_rendered.send(
sender=instance.__class__,
instance=instance_name,
start=start,
end=end,
processing_timeline=[],
level=1,
)
return result


class Profile:
"""
Profile with block
Usage:

from template_profiler_panel.panels.template import Profile
with Profile("block name"):
...
"""
def __init__(self, name="Profile with", *args, **kwargs):
self.name = name

def __enter__(self):
self.start = time()

def __exit__(self, type, value, traceback):
self.end = time()

template_rendered.send(
sender=self.__class__,
instance=self.name,
start=self.start,
end=self.end,
processing_timeline=[],
level=1,
)


def get_nodelist_timeline(nodelist, level):
timeline = []
for node in nodelist:
Expand Down Expand Up @@ -77,6 +140,22 @@ def __init__(self, *args, **kwargs):

have_monkey_patched_template_classes = False

def generate_stats(self, request, response):
match = resolve(request.path)
func, args, kwargs = match
view_class = getattr(func, 'view_class', None)
if view_class and not hasattr(view_class, '_profile_enabled'):
print(view_class)
view_class._profile_enabled = True
for attr in view_class.__dict__:
print(attr)
if isinstance(getattr(view_class, attr), Callable):
setattr(
view_class,
attr,
profile_method(getattr(view_class, attr)),
)

@classmethod
def monkey_patch_template_classes(cls):
if cls.have_monkey_patched_template_classes:
Expand Down Expand Up @@ -160,7 +239,10 @@ def record(self, instance, start, end, level,
if not self.enabled:
return

template_name = instance.name
try:
template_name = instance.name
except AttributeError:
template_name = instance
# Logic copied from django-debug-toolbar:
# https://github.com/jazzband/django-debug-toolbar/blob/5d095f66fde8f10b45a93c0b35be0a85762b0458/debug_toolbar/panels/templates/panel.py#L77
is_skipped_template = isinstance(template_name, str) and (
Expand Down