Giter Site home page Giter Site logo

char0n / django-profiler Goto Github PK

View Code? Open in Web Editor NEW
58.0 4.0 12.0 42 KB

django-profiler is util for profiling python code mainly in django projects but can be used also on ordinary python code. It counts sql queries a measures time of code execution. It logs its output via standard python logging library and uses logger profiling. If your profiler name doesn't contain any empty spaces e.g. Profiler('Profiler1') django-profiler will log all the output to the profiling.Profiler logger.

License: Other

Python 100.00%

django-profiler's Introduction

django-profiler

django-profiler is util for profiling python code mainly in django projects but can be used also on ordinary python code. It counts sql queries a measures time of code execution. It logs its output via standard python logging library and uses logger profiling. If your profiler name doesn't contain any empty spaces e.g. Profiler('Profiler1') django-profiler will log all the output to the profiling.Profiler logger.

Requirements

  • python 2.7+

Installation

Install via pip or copy this module into your project or into your PYTHON_PATH.

Configuration

django settings.py constants

PROFILING_LOGGER_NAME
PROFILING_SQL_QUERIES

It is possible to change default django-profiler logger name by defining PROFILING_LOGGER_NAME = 'logger_name' in your django settings.py.

To log also sql queries into profiler logger set PROFILING_SQL_QUERIES to True in your django settings.py module.

Examples

Example 1

Using context manager approach. Output will be logged to profiling logger.

from profiling import Profiler
with Profiler('Complex Computation'):
    # code with some complex computations

Example 2

Using context manager approach. Output will be logged to profiling.Computation logger.

from profiling import Profiler
with Profiler('Computation'):
    # code with some complex computations

Example 3

Using standard approach. Output will be logged to profiling logger.

from profiling import Profiler
profiler =  Profiler('Complex Computation')
profiler.start()
# code with some complex computations
profiler.stop()

Example 4

Using standard approach and starting directly in constructor. Output will be logged to profiling logger.

from profiling import Profiler
profiler =  Profiler('Complex Computation', start=True)
# code with some complex computations
profiler.stop()

Example 5

Using decorator approach. Output will be logged to profiling.complex_computations logger.

from profiling import profile

@profile
def complex_computations():
    #some complex computations

Example 6

Using decorator approach. Output will be logged to profiling.ComplexClass.complex_computations logger.

from profiling import profile

class ComplexClass(object):
    @profile
    def complex_computations():
        #some complex computations

Example 7

Using decorator approach. Output will be logged to profiling.complex_computations logger. profile execution stats are logged to profiling.complex_computations logger.

from profiling import profile

@profile(stats=True)
def complex_computations():
    #some complex computations

Example 8

Using decorator approach. Output will be logged to profiling.complex_computations logger. profile execution stats are printed to sys.stdout.

import sys

from profiling import profile

@profile(stats=True, stats_buffer=sys.stdout)
def complex_computations():
    #some complex computations

Example 9

Using decorator approach. Output will be logged to profiling.ComplexClass.complex_computations logger. profile stats will be logged to profiling.ComplexClass.complex_computations.

from profiling import profile

class ComplexClass(object)
   @profile(stats=True)
   def complex_computations():
       #some complex computations

Example 10

Using decorator approach. Output will be stored in /tmp/stats and can be analyzed with pstats module. profile stats will be logged to profiling.ComplexClass.complex_computations.

from profiling import profile

class ComplexClass(object)
   @profile(stats=True, stats_filename='/tmp/stats')
   def complex_computations():
       #some complex computations

Tests

Tested on evnironment

  • Xubuntu Linux 11.10 oneiric 64-bit
  • python 2.7.2+
  • python unittest

Running tests

To run the test run command:

$ python test.py
$ python setup.py test

Author

char0n (Vladimír Gorej, CodeScale s.r.o.)

References

django-profiler's People

Contributors

pwhipp avatar robsco-git avatar shivadp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

django-profiler's Issues

Incorrect io import

File "/Users/Shared/workspace/VOSM/CL_StoreMaps/venv/lib/python3.7/site-packages/profiling/init.py", line 9, in
from StringIO import StringIO
ModuleNotFoundError: No module named 'StringIO'

It was wrongly imported
fromStringIO import StringIO.

Please correct this line in _init.py , line 9 to
from io import StringIO

timeit module

Use timeit module for making time measurements instead of using time module

Support for query lists

As Profiler context manager gets executed, it can measure number of executed queries. API user should also be able to get list of all SQL queries, that have been executed with manager.

e.g.

with Profiler('sql_profiler') as profiler:
Model.objects.filter(has_attribute=True)
print p.sql_list

Time, Query expectations

It would be nice to define on profiler level assertions/expections and make profiler report all not satisfied assertions/expectations.

Substitute profilehook with profile python module

Python 2.7 now contains profile module with virtually the same functionality as profilehook package which uses python-profiles debian package that have installation problems on new ubuntu/xubuntu installations.

Find out if it is even substitutable and if so, substitute it.

Django configuration error while installing library from pip

While installing django-profiler into environment with django installed, django.core.exceptions.ImproperlyConfigured exception is raised. This is caused by the fact that profiler detects that django is installed and tries to import it. But import fails, because profiler tries to use django unconfigured.

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.