Login

Tag "profile"

Snippet List

Quick line profiler decorator

Requires [line_profiler](https://pypi.python.org/pypi/line_profiler) pip install line_profiler Will print profile info into console @line_profiler def my_view(request): context = some_quick_func() return some_heavy_func(context) It does not work good when nested, so don't wrap `some_heavy_func`. If you want to profile also some nested call - use `extra_view` parameter: @line_profiler(extra_view=[some_heavy_func]) def my_view(request): context = some_quick_func() return some_heavy_func(context) It will profile `my_view` and `some_heavy_func`

  • profile
  • decorator
  • line_profiler
Read More

Unit Test Profiling for Django 1.3/1.4

The snippet is a modification of [snippet 1315](http://djangosnippets.org/snippets/1315/) to fit the needs for Django 1.3 and 1.4. You can follow the explanations and instructions there. To plot a nice and so useful call-graph with timings, call: $ gprof2dot -f pstats unittest.profile | dot -Tpng -o unittest.profile.graph.png where 'unittest.profile' is the test runners profile output defined in your settings.

  • profile
  • unittest
  • runtime
  • cprofile
Read More

Multiple User subclasses custom Auth backend

A project I'm working on requires multiple different classes of users, all with different fields/attributes. Having a single UserProfile class with a generic relation was a complete pain in practice. So, I changed my classes to all subclass User directly and then used django-model-utils to create a custom ModelBackend that returns the appropriate class when accessing request.user. The InheritanceQuerySet manager provided by django-model-utils makes it all possible and with only a single database query. No need to add anything directly to the User class, by the way. Just subclass it directly with each of your custom classes: class CustomUser1(User): field1 = models.CharField(...) class CustomUser2(User): field2 = models.CharField(...)

  • user
  • profile
  • auth
  • subclass
  • backend
  • modelbackend
Read More

User Profile minimal code

Usually I start an authentication app with this model. Don't forget to set it up in the settings file AUTH_PROFILE_MODULE = 'authentication.UserProfile'

  • user
  • profile
  • signals
  • get_profile
Read More

Interactive Profiling Middleware

Based on [Extended Profiling Middleware](http://djangosnippets.org/snippets/605/), this version allows interactive sorting of functions and inspection of SQL queries.

  • middleware
  • profile
  • hotshot
Read More

Combine ProfileForm an UserForm in one.

Using this method you can combine form for standart django.contrib.auth.models.User model and for your project profile model. As now, ProfileForm can be used as usual, and it will also contain UserForm fields.

  • forms
  • user
  • profile
Read More

UserProfileForm

We often need to use a Profile form and we want to be able to modify the first_name, last_name and sometimes the email address. Here is how I do it. In this case I want to check the email so I did a specific form for it. But it is quite easy to add it.

  • user
  • profile
  • form
Read More

pycallgraph

Simple debug middleware that uses [pycallgraph](http://pycallgraph.slowchop.com) to get a visual representation of the call graph, including number of calls and execution times. Usage: 1. Replace *myapp* in the snippet with the name of your application and or adjust include and exclude according to your needs 2. Add CallgraphMiddleware to your middlewares in settings.py 3. Append ?prof to any URL in your application to trigger the callgraph creation Each callgraph cerated will be named callgraph-*timestamp*.png. This is because multiple callgraphs will be created when a re-direction occurs for example.

  • middleware
  • profile
  • debug
Read More

Unit Test Profiling

A test runner for Django unittests which profiles the tests run, and saves the result. Very useful for diagnosing your apps. Place the top portion of the code into a file called `profiling.py` someplace in your python path. Update your `settings.py` file with the bottom two lines of the code. Now you are ready, so just run `python manage.py test [appnames...]` to test any apps listed with profiling. By default this will just print a nice report after the unittests. If you change the value of `TEST_PROFILE` to a file, the profile will be saved to that file. The latter is recommended because these profiling reports have a lot of info in them, so it is best to tear through them with the `pstats` module.

  • profile
  • unittest
  • cprofile
Read More

Alternative to User.get_profile()

**This is an alternative to User.get_profile.** Rather than having you call `User.get_profile` directly, this retrieves the profile instance for a `User` and attaches the fields from the profile to the `User` object when instantiated. The special methods for `DateField`, `FileField`, `ImageField` and fields with `choices` are also created. Since the profile object still has to be retrieved from the database before its fields can be added to the `User`, the costs for using this might outweigh the rewards unless you are heavily using profiles. To install, place it in a module on your `PYTHONPATH` and add it to `INSTALLED_APPS`.

  • user
  • profile
  • auth
  • userprofile
  • profiles
Read More

Extended Profiling Middleware

Modified version of [Profiling Middleware](http://www.djangosnippets.org/snippets/186/) Prints profile results for method, additionally groups results by files and by modules (for django uses top level modules as groups). Works for Windows. Usage: append ?prof or &prof= to any URL pointing to django application after adding ProfileMiddleware to middlewares in yours settings.py. NOTICE: ProfileMiddleware uses hotshot profiler which is not thread safe.

  • middleware
  • profile
  • hotshot
Read More

Django profiler

The script that can be used to profile any Django-powered web-site and find how many SQL-queries are used per page, how heavy html-pages are, etc. To use the the script it's enough to put django-profile.py somewhere in PYTHONPATH and call it from the directory that holds projects' settings.py. For more info and examples please see: [www.mysoftparade.com/blog/django-profile-sql-performance/](http://www.mysoftparade.com/blog/django-profile-sql-performance/)

  • sql
  • profile
  • performance
Read More

Debug Page Load Time Stats Middleware

Use this to display a split of page execution time between python and the db in your base template when debugging. I originally got the base of this code from another snippet, but I can't find it anymore and want to share with new folks because I find this handy.

  • middleware
  • profile
  • time
  • debug
  • performance
  • query
  • basetemplate
Read More

SQL Log Middleware + duplicates

This is based on [Snippet 161](/snippets/161/) It marks duplicated SQL queries. To avoid duplicates read: [Caching and Queryset](http://www.djangoproject.com/documentation/db-api/#caching-and-querysets) Sept. 07: Updated for current trunk: 'response' behaves like 'response.header' 22. October '07: Log into directory.

  • sql
  • middleware
  • log
  • profile
  • debug
Read More

18 snippets posted so far.