Uzi
Uzi

Reputation: 281

Profiling for Django + Tastypie

What is the best tool to do profiling on a Django app that uses Tastypie (all responses or JSON, so django_toolbar is no good in this case)?

Upvotes: 3

Views: 832

Answers (4)

Alberto Megía
Alberto Megía

Reputation: 2255

I use this middleware -> https://djangosnippets.org/snippets/2126/

As the comment states, just

Install this by adding it to your MIDDLEWARE_CLASSES. It is active if you are logged in as a superuser, or always when settings.DEBUG is True. To use it, pass 'profile=1' as a GET or POST parameter to any HTTP request.

It will create an interactive HTML showing queries executed, time, methods... you should give it a try!

Upvotes: 0

alex-mcleod
alex-mcleod

Reputation: 68

One potential solution is to create a view which just renders TastyPie responses as HTML. This will allow django-debug-toolbar to output correct profiling data. The following is a fairly quick and dirty attempt at this.

In urls.py:

Just add the following line to your url patterns, but make sure it is only included if debugging is enabled.

(r'^api_profile/(?P<resource>.*)$', 'common.views.api_profile')

In common/views.py:

Place this view anywhere you want, I've put it in my common app.

from django.shortcuts import render_to_response
# Import the tastypie.api.Api object with which your api resources are registered. 
from apps.api.urls import api

def api_profile(request, resource):
    """ Allows easy profiling of API requests with django-debug-toolbar. """
    context = {}
    resource = resource.strip('/')
    resource = api.canonical_resource_for(resource)
    obj_list = resource.wrap_view('dispatch_list')(request)
    response = resource.create_response(request, obj_list)
    context['api_response'] = response
    return render_to_response('common/api_profile.html', context)

In templates/common/api_profile.html:

Include a very simple template.

<body>
    {{api_response}}
</body>

Now you could just navigate to '/api_profile/a_resource/' with django-debug-toolbar enabled and get profiling data for the generation of the a_resource list view. Request parameters can also be used, e.g. I've been profiling the request 'api_profile/posts/?limit=8&offset=16'.

Upvotes: 1

Pat B
Pat B

Reputation: 564

New Relic is good if you want to pay. You can also use hotshot profiler in https://github.com/shaunsephton/django-snippetscream for a quick peek at whats going on

Upvotes: 0

Jonas Geiregat
Jonas Geiregat

Reputation: 5442

I've never actually tried profiling a django application. But some time ago I've read an interesting article about this subject. http://reinout.vanrees.org/weblog/2012/04/18/profiling-python.html

Upvotes: 0

Related Questions