Skip to main content

Django middleware for performance profiling directly from the browser

Project description

https://img.shields.io/pypi/v/yet-another-django-profiler.svg https://travis-ci.org/safarijv/yet-another-django-profiler.svg?branch=master https://coveralls.io/repos/safarijv/yet-another-django-profiler/badge.svg Supported Python versions Supported Python implementations License Downloads Development Status Wheel Status

Yet Another Django Profiler attempts to combine the best features of assorted other Django profiling utilities that have been created over the years. (For more background information, see my blog post on the topic.)

Installation

First, get the code via pip install:

pip install yet-another-django-profiler

Then add yet_another_django_profiler.middleware.ProfilerMiddleware to your MIDDLEWARE_CLASSES Django setting (typically at the end of the list, if you want to include profiling data on the other middleware that’s in use). If you want to generate call graphs with the middleware, you also need to install Graphviz. If you want to use the “profile” management command, you’ll also need to add yet_another_django_profiler to the INSTALLED_APPS setting.

Middleware Usage

The simplest usage is to just add a profile parameter to the URL of a Django view. This uses Graphviz to generate a PDF representation of the call graph for the code executed to perform the view, and returns that as the response to the request instead of the rendered view itself. So calling a URL like http://localhost:8000/admin/?profile shows a PDF like this in the browser.

Alternatively, you can display a table of called functions ordered by the desired statistic by using a URL such as http://localhost:8000/?profile=time. The available sorting options are:

  • calls (call count)

  • cumulative (cumulative time)

  • file (file name, same as module)

  • module (file name, same as file)

  • pcalls (primitive call count)

  • line (line number)

  • name (function name)

  • nfl ( function name/file/line)

  • stdname (standard name)

  • time (internal time)

By default, only the top 20% of function calls are included in the table. To change that, add a fraction parameter with the desired display ratio (hence the default value is fraction=.2). Alternatively, you can instead specify a maximum number of function calls to display using the max_calls parameter. And if you specify a regular expression with the pattern parameter, only calls of functions whose names match the specified pattern will be displayed. (I’d recommend sticking to basic sub-strings unless you really enjoy figuring out how to URL-escape special characters.)

If you forget the available sorting options and such, you can use profile=help as a request parameter to display the usage instructions in the browser.

Management Command Usage

yet-another-django-profiler includes a profile management command which can be used to profile other Django management commands:

Usage: django-admin.py profile [options] other_command <argument argument ...>

Profile another Django management command

Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings=SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath=PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Raise on exception
  -o PATH, --output=PATH
                        Path to a file in which to store the profiling output
                        (required if generating a call graph PDF, other
                        results are output to the console by default)
  -s SORT, --sort=SORT  Statistic by which to sort the profiling data (default
                        is to generate a call graph PDF instead)
  -f FRACTION, --fraction=FRACTION
                        The fraction of total function calls to display (the
                        default of .2 is omitted if max-calls or pattern are
                        specified)
  -m MAX_CALLS, --max-calls=MAX_CALLS
                        The maximum number of function calls to display
  -p PATTERN, --pattern=PATTERN
                        Regular expression filter for function display names
  -b BACKEND, --backend=BACKEND
                        Profiler backend to use (cProfile or yappi)
  --version             show program's version number and exit
  -h, --help            show this help message and exit

Sample usage:

  • django-admin.py profile -s time test --failfast my_app/my_module.py:TestClass.test_function

  • django-admin.py profile -o ~/Downloads/call_graph.pdf collectstatic

Settings

The middleware is designed to be available whenever the DEBUG setting is True, and removes itself from the middleware chain otherwise (so it can safely be left in the dependencies for production deployments without performance or security problems). If for some reason you want to change this behavior, you can set the YADP_ENABLED boolean setting directly to determine whether the middleware is active or not.

If you have pages where the default profiling parameter names conflict with existing parameters in the application, you can choose different ones via the following settings:

  • YADP_PROFILE_PARAMETER (default is “profile”)

  • YADP_FRACTION_PARAMETER (default is “fraction”)

  • YADP_MAX_CALLS_PARAMETER (default is “max_calls”)

  • YADP_PATTERN_PARAMETER (default is “pattern”)

You can use Yappi (Yet Another Python Profiler) as a profiler backend instead of cProfile. To do that just specify YADP_PROFILER_BACKEND = 'yappi' in the settings. Note that Yappi does not currently work on PyPy or CPython 3.2.

An effort is made to convert the absolute Python file paths provided by the profiler to full-qualified module names (which are typically shorter and easier to understand at a glance). The default rules should work in most cases but can be customized via the following settings:

  • YADP_MODULE_PARENT_DIR_PATTERNS is a list of regular expression patterns. Everything in a module path up to and including a match of one of these patterns is removed from statistic tables and call graphs. The default list is [r'\.egg[/\\]', r'site-packages[/\\]', r'python\d+\.\d+[/\\]']. The absolute path of the current working directory is also pruned.

  • If the previous setting doesn’t allow sufficient customization for your needs, the YADP_PATH_TO_MODULE_FUNCTION setting can be used to completely replace the function used for this task. It should be the fully qualified name of your custom function, which takes an absolute file path as input and returns what you want to appear in the profiling output to represent that path.

In order to get simple and meaningful profiling data, a few other changes to your settings may be in order.

Running Tests

To run tests in all currently supported combinations of Python and Django, run tox. If you’re running tox from a Python 2 environment, you can instead run detox to execute all the test environments in parallel. See the tox documentation for instructions on running a single test case or environment.

Internationalization

Translations of text that can appear in the profiling results pages are managed on Transifex. Feel free to request to be added as translator for a not-yet-supported language. Django recommends not translating management command text for assorted technical reasons, so those phrases currently aren’t included.

For development tasks involving the translations (uploading message changes to Transifex or fetching the latest translations from it), use transifex-client. By default, pip installs a rather old stable version so you may want to specify a newer one:

pip install transifex-client==0.11b3

When running the makemessages or compilemessages management commands, do so from the yet_another_django_profiler directory.

License

Due to gprof2dot being licensed under the LGPL v3, that’s the license that applies to this package as a whole. However, the rest of the source files are individually licensed under a more permissive 3-clause BSD license (so it is possible to assemble a BSD-licensed package that omits only the call graph generation feature).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yet-another-django-profiler-1.0.0.tar.gz (38.3 kB view details)

Uploaded Source

Built Distribution

yet_another_django_profiler-1.0.0-py2.py3-none-any.whl (47.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file yet-another-django-profiler-1.0.0.tar.gz.

File metadata

File hashes

Hashes for yet-another-django-profiler-1.0.0.tar.gz
Algorithm Hash digest
SHA256 aaa9128967eb946a6defa25148ef512420a7909b25e4eda27f91e9b2b0a7b4bd
MD5 d82adcf4ae6658a2ddedaef39ff2ec12
BLAKE2b-256 00fe7475dc6714369d41ae4ace412771139f6e87744fc3711bac67d37be8ecd5

See more details on using hashes here.

File details

Details for the file yet_another_django_profiler-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for yet_another_django_profiler-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6a5f387c8a52a3abffef84a120cba06ca8eee3e738b4402b277d8618a7c1a13d
MD5 c4caff6cec835ea3c4799b6d19a81ad3
BLAKE2b-256 d359a5dd27bcefab8be6978f1e645b0d92d69ef0c4ccefac6be20299656c5e48

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page