Skip to content

Commit

Permalink
Add setting PROFILER_CAPTURE_PROJECT_CODE.
Browse files Browse the repository at this point in the history
This can be used to disable the attempt to include all project code. This is
useful if dependencies are installed within the project.
  • Loading branch information
tim-schilling committed Sep 11, 2022
1 parent 7f4392d commit 2c100cb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions debug_toolbar/panels/profiling.py
Expand Up @@ -44,7 +44,8 @@ def is_project_func(self):
file_name, _, _ = self.func
return (
str(settings.BASE_DIR) in file_name
and "/site-packages" not in file_name
and "/site-packages/" not in file_name
and "/dist-packages/" not in file_name
)
return None

Expand Down Expand Up @@ -139,6 +140,7 @@ class ProfilingPanel(Panel):
title = _("Profiling")

template = "debug_toolbar/panels/profiling.html"
capture_project_code = dt_settings.get_config()["PROFILER_CAPTURE_PROJECT_CODE"]

def process_request(self, request):
self.profiler = cProfile.Profile()
Expand All @@ -151,7 +153,9 @@ def add_node(self, func_list, func, max_depth, cum_time):
for subfunc in func.subfuncs():
# Always include the user's code
if subfunc.stats[3] >= cum_time or (
subfunc.is_project_func() and subfunc.stats[3] > 0
self.capture_project_code
and subfunc.is_project_func()
and subfunc.stats[3] > 0
):
func.has_subfuncs = True
self.add_node(func_list, subfunc, max_depth, cum_time)
Expand Down
1 change: 1 addition & 0 deletions debug_toolbar/settings.py
Expand Up @@ -33,6 +33,7 @@
"django.utils.functional",
),
"PRETTIFY_SQL": True,
"PROFILER_CAPTURE_PROJECT_CODE": True,
"PROFILER_MAX_DEPTH": 10,
"PROFILER_THRESHOLD_RATIO": 8,
"SHOW_TEMPLATE_CONTEXT": True,
Expand Down
4 changes: 4 additions & 0 deletions docs/changes.rst
Expand Up @@ -10,6 +10,10 @@ Pending
* Update Profiling panel to include try to always include user code. This
code is more important to developers than dependency code.
* Highlight the project function calls in the profiling panel.
* Added Profiling panel setting ``PROFILER_CAPTURE_PROJECT_CODE`` to allow
users to disable the inclusion of all project code. This will be useful
to project setups that have dependencies installed under
``settings.BASE_DIR``.


3.6.0 (2022-08-17)
Expand Down
12 changes: 12 additions & 0 deletions docs/configuration.rst
Expand Up @@ -250,6 +250,18 @@ Panel options
WHERE "auth_user"."username" = '''test_username'''
LIMIT 21

* ``PROFILER_CAPTURE_PROJECT_CODE``

Default: ``True``

Panel: profiling

When enabled this setting will include all project function calls in the
panel. Project code is defined as files in the path defined at
``settings.BASE_DIR``. If you install dependencies under
``settings.BASE_DIR`` in a directory other than ``sites-packages`` or
``dist-packages`` you may need to disable this setting.

* ``PROFILER_MAX_DEPTH``

Default: ``10``
Expand Down
2 changes: 2 additions & 0 deletions docs/tips.rst
Expand Up @@ -77,6 +77,8 @@ by disabling some configuration options that are enabled by default:

- ``ENABLE_STACKTRACES`` for the SQL and cache panels,
- ``SHOW_TEMPLATE_CONTEXT`` for the template panel.
- ``PROFILER_CAPTURE_PROJECT_CODE`` and ``PROFILER_THRESHOLD_RATIO`` for the
profiling panel.

Also, check ``SKIP_TEMPLATE_PREFIXES`` when you're using template-based
form widgets.

0 comments on commit 2c100cb

Please sign in to comment.