From ff5990b28ff4ee0fc2066156c949312995667922 Mon Sep 17 00:00:00 2001 From: Mykhailo Keda Date: Sat, 16 May 2020 19:23:16 +0200 Subject: [PATCH 1/5] Make it work with Django 3 and django-debug-toolbar 3. --- .gitignore | 1 + debug_toolbar_line_profiler/panel.py | 27 ++++++++++++------- .../panels/profiling.html | 6 ++--- setup.py | 5 ++-- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index d704fe3..6f97ccd 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ docs/_build example/db.sqlite3 htmlcov .tox +.idea/ diff --git a/debug_toolbar_line_profiler/panel.py b/debug_toolbar_line_profiler/panel.py index 3aa8be4..2ad68e8 100644 --- a/debug_toolbar_line_profiler/panel.py +++ b/debug_toolbar_line_profiler/panel.py @@ -7,9 +7,13 @@ from pstats import Stats from six import PY2 +from django.urls import resolve from django.utils.translation import ugettext_lazy as _ from django.utils.safestring import mark_safe -from django.utils.six.moves import cStringIO +try: + from django.utils.six.moves import cStringIO +except ImportError: + from io import StringIO as cStringIO from debug_toolbar.panels import Panel from django.views.generic.base import View @@ -26,7 +30,7 @@ def get_root_func(self, view_func): filename = view_func.__code__.co_filename firstlineno = view_func.__code__.co_firstlineno for func, (cc, nc, tt, ct, callers) in self.stats.items(): - if (len(callers) == 0 + if (len(callers) >= 0 and func[0] == filename and func[1] == firstlineno): self.__root = func @@ -76,7 +80,9 @@ def func_std_string(self): # match what old profile produced if idx > -1: file_name = file_name[(idx + 14):] - file_path, file_name = file_name.rsplit(os.sep, 1) + file_items = file_name.rsplit(os.sep, 1) + file_path, file_name = file_items if len(file_items) > 1 else [ + None, file_name] return mark_safe( '{0}/' @@ -182,22 +188,25 @@ def _unwrap_closure_and_profile(self, func): 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): + if not name.startswith('__') and ( + inspect.ismethod(value) or + inspect.isfunction(value) + ): self._unwrap_closure_and_profile(value) - def process_view(self, request, view_func, view_args, view_kwargs): + def process_request(self, request): + view_func, view_args, view_kwargs = resolve(request.path) self.view_func = view_func self.profiler = cProfile.Profile() - args = (request,) + view_args self.line_profiler = LineProfiler() - self._unwrap_closure_and_profile(view_func) + self._unwrap_closure_and_profile(self.view_func) signals.profiler_setup.send(sender=self, profiler=self.line_profiler, view_func=view_func, view_args=view_args, view_kwargs=view_kwargs) self.line_profiler.enable_by_count() - out = self.profiler.runcall(view_func, *args, **view_kwargs) + out = self.profiler.runcall(super().process_request, request) self.line_profiler.disable_by_count() return out @@ -238,7 +247,7 @@ def add_node(self, func_list, func, max_depth, cum_time=0.1): max_depth=max_depth, cum_time=subfunc.cumtime()/16) - def process_response(self, request, response): + def generate_stats(self, request, response): if not hasattr(self, 'profiler'): return None # Could be delayed until the panel content is requested (perf. optim.) diff --git a/debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler/panels/profiling.html b/debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler/panels/profiling.html index 442437b..dd1d4c7 100644 --- a/debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler/panels/profiling.html +++ b/debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler/panels/profiling.html @@ -1,4 +1,4 @@ -{% load i18n %}{% load static from staticfiles %} +{% load i18n %}{% load static %} @@ -13,11 +13,11 @@ {% for call in func_list %} - +
{% if call.has_subfuncs %} - - + - {% else %} {% endif %} diff --git a/setup.py b/setup.py index 5b136de..25286fa 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='django-debug-toolbar-line-profiler', - version='0.6.1', + version='0.7.0', description='A panel for django-debug-toolbar that integrates ' + 'information from line_profiler', long_description=open('README.rst').read(), @@ -13,7 +13,7 @@ license='BSD', packages=find_packages(exclude=('tests', 'example')), install_requires=[ - 'django-debug-toolbar>=1.0', + 'django-debug-toolbar>=2.0', 'line_profiler>=1.0b3', 'six>=1.10', ], @@ -31,6 +31,7 @@ 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.7', 'Topic :: Software Development :: Libraries :: Python Modules', ], ) From 4ae8cd050db7c8ccb10304a47b60e45a91e72368 Mon Sep 17 00:00:00 2001 From: Mykhailo Keda Date: Sat, 16 May 2020 23:37:48 +0200 Subject: [PATCH 2/5] Make it work with recursive functions. --- debug_toolbar_line_profiler/panel.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/debug_toolbar_line_profiler/panel.py b/debug_toolbar_line_profiler/panel.py index 2ad68e8..a7db79e 100644 --- a/debug_toolbar_line_profiler/panel.py +++ b/debug_toolbar_line_profiler/panel.py @@ -171,8 +171,11 @@ class ProfilingPanel(Panel): template = 'debug_toolbar_line_profiler/panels/profiling.html' def _unwrap_closure_and_profile(self, func): - if not hasattr(func, '__code__'): + if not hasattr(func, '__code__') or func in self.added: return + + self.added.add(func) + self.line_profiler.add_function(func) for subfunc in getattr(func, 'profile_additional', []): self._unwrap_closure_and_profile(subfunc) @@ -199,6 +202,7 @@ def process_request(self, request): self.view_func = view_func self.profiler = cProfile.Profile() self.line_profiler = LineProfiler() + self.added = set() self._unwrap_closure_and_profile(self.view_func) signals.profiler_setup.send(sender=self, profiler=self.line_profiler, From f967b13e259c92ce249676e864c5a5f5c7b7cb42 Mon Sep 17 00:00:00 2001 From: Mykhailo Keda Date: Sun, 17 May 2020 01:11:52 +0200 Subject: [PATCH 3/5] Removed unnecessary js. --- MANIFEST.in | 1 - .../js/toolbar.profiling.js | 27 ------------------- .../panels/profiling.html | 2 -- 3 files changed, 30 deletions(-) delete mode 100644 debug_toolbar_line_profiler/static/debug_toolbar_line_profiler/js/toolbar.profiling.js diff --git a/MANIFEST.in b/MANIFEST.in index 02b2e0a..58d8423 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,3 @@ include LICENSE include README.rst recursive-include debug_toolbar_line_profiler/templates * -recursive-include debug_toolbar_line_profiler/static * diff --git a/debug_toolbar_line_profiler/static/debug_toolbar_line_profiler/js/toolbar.profiling.js b/debug_toolbar_line_profiler/static/debug_toolbar_line_profiler/js/toolbar.profiling.js deleted file mode 100644 index 172c2a6..0000000 --- a/debug_toolbar_line_profiler/static/debug_toolbar_line_profiler/js/toolbar.profiling.js +++ /dev/null @@ -1,27 +0,0 @@ -(function (factory) { - if (typeof define === 'function' && define.amd) { - define(['jquery'], factory); - } else { - factory(jQuery); - } -}(function ($) { - function getSubcalls(row) { - var id = row.attr('id'); - return $('.djDebugProfileRow[id^="'+id+'_"]'); - } - function getDirectSubcalls(row) { - var subcalls = getSubcalls(row); - var depth = parseInt(row.attr('depth'), 10) + 1; - return subcalls.filter('[depth='+depth+']'); - } - $('.djDebugProfileRow .djDebugProfileToggle').on('click', function(){ - var row = $(this).closest('.djDebugProfileRow'); - var subcalls = getSubcalls(row); - if (subcalls.css('display') == 'none') { - getDirectSubcalls(row).show(); - } else { - subcalls.hide(); - } - }); - -})); diff --git a/debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler/panels/profiling.html b/debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler/panels/profiling.html index dd1d4c7..716f31f 100644 --- a/debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler/panels/profiling.html +++ b/debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler/panels/profiling.html @@ -40,5 +40,3 @@ {% endfor %}
- - From b2b31f6124d7ce7bea1be3bac7bc991691e32397 Mon Sep 17 00:00:00 2001 From: Mykhailo Keda Date: Fri, 14 Aug 2020 23:20:51 +0300 Subject: [PATCH 4/5] Make a fork of django-debug-toolbar-line-profiler --- README.rst | 8 ++++---- .../__init__.py | 0 .../panel.py | 3 ++- .../signals.py | 0 .../panels/profiling.html | 0 setup.py | 10 +++++----- 6 files changed, 11 insertions(+), 10 deletions(-) rename {debug_toolbar_line_profiler => debug_toolbar_line_profiling}/__init__.py (100%) rename {debug_toolbar_line_profiler => debug_toolbar_line_profiling}/panel.py (98%) rename {debug_toolbar_line_profiler => debug_toolbar_line_profiling}/signals.py (100%) rename {debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler => debug_toolbar_line_profiling/templates/debug_toolbar_line_profiling}/panels/profiling.html (100%) diff --git a/README.rst b/README.rst index b06fc8c..782b75f 100644 --- a/README.rst +++ b/README.rst @@ -3,7 +3,7 @@ Django Debug Toolbar Line Profile Panel ======================================= The `Django Debug Toolbar -`_ is a configurable set of panels that display various +`_ is a configurable set of panels that display various debug information about the current request/response and when clicked, display more details about the panel's content. @@ -32,18 +32,18 @@ in August 2008 and was further developed by many contributors. Installation ============ -To install the line_profiler panel, first install this package with ``pip install django-debug-toolbar-line-profiler``, then add debug_toolbar_line_profiler to the INSTALLED_APPS:: +To install the line_profiler panel, first install this package with ``pip install django-debug-toolbar-line-profiling``, then add debug_toolbar_line_profiler to the INSTALLED_APPS:: INSTALLED_APPS = ( ... - 'debug_toolbar_line_profiler', + 'debug_toolbar_line_profiling', ) and add the panel to DEBUG_TOOLBAR_PANELS:: DEBUG_TOOLBAR_PANELS = ( ... - 'debug_toolbar_line_profiler.panel.ProfilingPanel', + 'debug_toolbar_line_profiling.panel.ProfilingPanel', ) Configuration diff --git a/debug_toolbar_line_profiler/__init__.py b/debug_toolbar_line_profiling/__init__.py similarity index 100% rename from debug_toolbar_line_profiler/__init__.py rename to debug_toolbar_line_profiling/__init__.py diff --git a/debug_toolbar_line_profiler/panel.py b/debug_toolbar_line_profiling/panel.py similarity index 98% rename from debug_toolbar_line_profiler/panel.py rename to debug_toolbar_line_profiling/panel.py index a7db79e..1c67021 100644 --- a/debug_toolbar_line_profiler/panel.py +++ b/debug_toolbar_line_profiling/panel.py @@ -57,6 +57,7 @@ def __init__(self, statobj, func, depth=0, stats=None, self.parent_ids = parent_ids self.hsv = hsv self._line_stats_text = None + self.has_subfuncs = False def parent_classes(self): return self.parent_classes @@ -168,7 +169,7 @@ class ProfilingPanel(Panel): """ title = _('Profiling') - template = 'debug_toolbar_line_profiler/panels/profiling.html' + template = 'debug_toolbar_line_profiling/panels/profiling.html' def _unwrap_closure_and_profile(self, func): if not hasattr(func, '__code__') or func in self.added: diff --git a/debug_toolbar_line_profiler/signals.py b/debug_toolbar_line_profiling/signals.py similarity index 100% rename from debug_toolbar_line_profiler/signals.py rename to debug_toolbar_line_profiling/signals.py diff --git a/debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler/panels/profiling.html b/debug_toolbar_line_profiling/templates/debug_toolbar_line_profiling/panels/profiling.html similarity index 100% rename from debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler/panels/profiling.html rename to debug_toolbar_line_profiling/templates/debug_toolbar_line_profiling/panels/profiling.html diff --git a/setup.py b/setup.py index 25286fa..22b231c 100644 --- a/setup.py +++ b/setup.py @@ -1,15 +1,15 @@ from setuptools import setup, find_packages setup( - name='django-debug-toolbar-line-profiler', + name='django-debug-toolbar-line-profiling', version='0.7.0', description='A panel for django-debug-toolbar that integrates ' + 'information from line_profiler', long_description=open('README.rst').read(), - author='Dave McLain', - author_email='dmclain@gmail.com', - url='https://github.com/dmclain/django-debug-toolbar-line-profiler', - download_url='https://pypi.python.org/pypi/django-debug-toolbar-line-profiler', + author='Mykhailo Keda', + author_email='mriynuk@gmail.com', + url='https://github.com/mikekeda/django-debug-toolbar-line-profiling', + download_url='https://pypi.python.org/pypi/django-debug-toolbar-line-profiling', license='BSD', packages=find_packages(exclude=('tests', 'example')), install_requires=[ From 3debff4d644f8566dbd720b68115f88a98881b9c Mon Sep 17 00:00:00 2001 From: Mykhailo Keda Date: Tue, 18 Aug 2020 18:47:41 +0300 Subject: [PATCH 5/5] Fixed internal structure. --- .gitignore | 1 + README.rst | 8 ++++---- .../__init__.py | 0 .../panel.py | 3 +-- .../signals.py | 0 .../debug_toolbar_line_profiler}/panels/profiling.html | 0 setup.py | 4 ++-- 7 files changed, 8 insertions(+), 8 deletions(-) rename {debug_toolbar_line_profiling => debug_toolbar_line_profiler}/__init__.py (100%) rename {debug_toolbar_line_profiling => debug_toolbar_line_profiler}/panel.py (98%) rename {debug_toolbar_line_profiling => debug_toolbar_line_profiler}/signals.py (100%) rename {debug_toolbar_line_profiling/templates/debug_toolbar_line_profiling => debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler}/panels/profiling.html (100%) diff --git a/.gitignore b/.gitignore index 6f97ccd..fa12b50 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ example/db.sqlite3 htmlcov .tox .idea/ +*.egg-info/ diff --git a/README.rst b/README.rst index 782b75f..9ba2139 100644 --- a/README.rst +++ b/README.rst @@ -3,7 +3,7 @@ Django Debug Toolbar Line Profile Panel ======================================= The `Django Debug Toolbar -`_ is a configurable set of panels that display various +`_ is a configurable set of panels that display various debug information about the current request/response and when clicked, display more details about the panel's content. @@ -32,18 +32,18 @@ in August 2008 and was further developed by many contributors. Installation ============ -To install the line_profiler panel, first install this package with ``pip install django-debug-toolbar-line-profiling``, then add debug_toolbar_line_profiler to the INSTALLED_APPS:: +To install the line_profiler panel, first install this package with ``pip install django-debug-toolbar-line-profiler``, then add debug_toolbar_line_profiler to the INSTALLED_APPS:: INSTALLED_APPS = ( ... - 'debug_toolbar_line_profiling', + 'debug_toolbar_line_profiler', ) and add the panel to DEBUG_TOOLBAR_PANELS:: DEBUG_TOOLBAR_PANELS = ( ... - 'debug_toolbar_line_profiling.panel.ProfilingPanel', + 'debug_toolbar_line_profiler.panel.ProfilingPanel', ) Configuration diff --git a/debug_toolbar_line_profiling/__init__.py b/debug_toolbar_line_profiler/__init__.py similarity index 100% rename from debug_toolbar_line_profiling/__init__.py rename to debug_toolbar_line_profiler/__init__.py diff --git a/debug_toolbar_line_profiling/panel.py b/debug_toolbar_line_profiler/panel.py similarity index 98% rename from debug_toolbar_line_profiling/panel.py rename to debug_toolbar_line_profiler/panel.py index 1c67021..a7db79e 100644 --- a/debug_toolbar_line_profiling/panel.py +++ b/debug_toolbar_line_profiler/panel.py @@ -57,7 +57,6 @@ def __init__(self, statobj, func, depth=0, stats=None, self.parent_ids = parent_ids self.hsv = hsv self._line_stats_text = None - self.has_subfuncs = False def parent_classes(self): return self.parent_classes @@ -169,7 +168,7 @@ class ProfilingPanel(Panel): """ title = _('Profiling') - template = 'debug_toolbar_line_profiling/panels/profiling.html' + template = 'debug_toolbar_line_profiler/panels/profiling.html' def _unwrap_closure_and_profile(self, func): if not hasattr(func, '__code__') or func in self.added: diff --git a/debug_toolbar_line_profiling/signals.py b/debug_toolbar_line_profiler/signals.py similarity index 100% rename from debug_toolbar_line_profiling/signals.py rename to debug_toolbar_line_profiler/signals.py diff --git a/debug_toolbar_line_profiling/templates/debug_toolbar_line_profiling/panels/profiling.html b/debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler/panels/profiling.html similarity index 100% rename from debug_toolbar_line_profiling/templates/debug_toolbar_line_profiling/panels/profiling.html rename to debug_toolbar_line_profiler/templates/debug_toolbar_line_profiler/panels/profiling.html diff --git a/setup.py b/setup.py index 22b231c..34ed27f 100644 --- a/setup.py +++ b/setup.py @@ -2,13 +2,13 @@ setup( name='django-debug-toolbar-line-profiling', - version='0.7.0', + version='0.7.1', description='A panel for django-debug-toolbar that integrates ' + 'information from line_profiler', long_description=open('README.rst').read(), author='Mykhailo Keda', author_email='mriynuk@gmail.com', - url='https://github.com/mikekeda/django-debug-toolbar-line-profiling', + url='https://github.com/mikekeda/django-debug-toolbar-line-profiler', download_url='https://pypi.python.org/pypi/django-debug-toolbar-line-profiling', license='BSD', packages=find_packages(exclude=('tests', 'example')),