GithubHelp home page GithubHelp logo

pmcarlos / django-speedinfo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from catcombo/django-speedinfo

0.0 1.0 0.0 434 KB

Live profiling tool for Django to measure views performance in small projects

License: MIT License

Python 93.72% CSS 1.24% HTML 1.93% JavaScript 3.12%

django-speedinfo's Introduction

django-speedinfo

Build Status

SpeedInfo is a live profiling tool for the Django framework to find the most high loaded views in your project for the next optimization. SpeedInfo counts number of calls, cache hits, SQL queries, measures average and total call time and more for each of your views. Detailed report and profiler controls are available in Django admin.

https://github.com/catcombo/django-speedinfo/raw/master/screenshots/main.png

Installation

  1. Run pip install django-speedinfo.

  2. Add speedinfo to INSTALLED_APPS.

  3. Add speedinfo.middleware.ProfilerMiddleware to the end of MIDDLEWARE (or MIDDLEWARE_CLASSES for Django < 1.10) list, but before django.middleware.cache.FetchFromCacheMiddleware (if used):

    MIDDLEWARE = [
        ...,
        'speedinfo.middleware.ProfilerMiddleware',
        'django.middleware.cache.FetchFromCacheMiddleware',
    ]
    
  4. Setup any cache backend, except local-memory and dummy caching, using our proxy cache backend. Speedinfo needs cache to store profiler state between requests and to intercept calls to cache:

    CACHES = {
        'default': {
            'BACKEND': 'speedinfo.backends.proxy_cache',
            'CACHE_BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
            'LOCATION': '/var/tmp/django_cache',
        }
    }
    
  5. Run python manage.py migrate.

  6. Run python manage.py collectstatic.

Usage

Open Views profiler in Django admin. Click the Turn on / Turn off button to control profiler state. Press Reset button to reset all data.

Configuration

SpeedInfo automatically detects when using Django per-site caching via UpdateCacheMiddleware and FetchFromCacheMiddleware middlewares or per-view caching via cache_page decorator and counts cache hit when retrieving page from cache.

If you implement your own caching logic and want to mark view response as obtained from a cache, add attribute to the HttpResponse object with the name from SPEEDINFO_CACHED_RESPONSE_ATTR_NAME and the value set to True. Example:

from django.views import View
from speedinfo.settings import SPEEDINFO_CACHED_RESPONSE_ATTR_NAME

class CachedView(View):
    def get(self, request, *args, **kwargs):
        # ...
        # `response` was taken from the cache
        # mark it in appropriate way
        setattr(response, SPEEDINFO_CACHED_RESPONSE_ATTR_NAME, True)
        return response

Change SPEEDINFO_REPORT_COLUMNS setting to customize Django admin profiler columns. Default value:

SPEEDINFO_REPORT_COLUMNS = (
    'view_name', 'method', 'anon_calls_ratio', 'cache_hits_ratio',
    'sql_count_per_call', 'sql_time_ratio', 'total_calls', 'time_per_call', 'total_time'
)

Profiling conditions

SPEEDINFO_PROFILING_CONDITIONS setting allows to declare a list of imported classes to define the conditions for profiling a processed view. By default, the only condition is enabled:

SPEEDINFO_PROFILING_CONDITIONS = [
    'speedinfo.conditions.exclude_urls.ExcludeURLCondition',
]

ExcludeURLCondition allows you to exclude some urls from profiling by adding them to the SPEEDINFO_EXCLUDE_URLS list. ExcludeURLCondition uses re.match internally to test requested url. Example:

SPEEDINFO_EXCLUDE_URLS = [
    r'/admin/',
    r'/news/$',
    r'/movie/\d+/$',
]

To define your own condition class, you must inherit from the base class speedinfo.conditions.base.Condition and implement all abstract methods. See ExcludeURLCondition source code for implementation example. Then add full path to your class to SPEEDINFO_PROFILING_CONDITIONS list as shown above. Conditions in mentioned list are executed in a top-down order. The first condition returning False interrupts the further check.

Separate storage for data

If you want to use different database to store django-speedinfo data:

  1. Define separate database in DATABASES option in the project settings.
  2. Configure database router to return appropriate database for speedinfo application (see an example in documentation).

Notice

The number of SQL queries measured by django-speedinfo may differ from the values of django-debug-toolbar for the same view. It happens because we show the average number of SQL queries for each view. Secondly, we don't take into account SQL queries made before the call of a view (e.g. in the preceding middlewares), as well SQL queries made after the view call.

django-speedinfo's People

Contributors

catcombo avatar victorfabref avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.