Tastypie を使用する Django アプリでプロファイリングを行うのに最適なツールは何ですか?
4 に答える
考えられる解決策の 1 つは、TastyPie の応答を HTML としてレンダリングするだけのビューを作成することです。これにより、django-debug-toolbar が正しいプロファイリング データを出力できるようになります。以下は、これに対するかなり手っ取り早い汚い試みです。
urls.py で:
次の行を URL パターンに追加するだけですが、デバッグが有効になっている場合にのみ含まれていることを確認してください。
(r'^api_profile/(?P<resource>.*)$', 'common.views.api_profile')
common/views.py で:
このビューを好きな場所に配置してください。共通アプリに配置しました。
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)
templates/common/api_profile.html 内:
非常にシンプルなテンプレートを含めます。
<body>
{{api_response}}
</body>
これで、django-debug-toolbar を有効にして '/api_profile/a_resource/' に移動し、a_resource リスト ビューを生成するためのプロファイリング データを取得できます。リクエスト パラメータも使用できます。たとえば、リクエスト 'api_profile/posts/?limit=8&offset=16' をプロファイリングしています。
お金を払いたいならNew Relicがいいです。また、 https://github.com/shaunsephton/django-snippetscreamでhotshot プロファイラーを使用して、何が起こっているかを簡単に確認することもできます。
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
私はこのミドルウェアを使用しています - > https://djangosnippets.org/snippets/2126/
コメントが述べているように、ちょうど
これを MIDDLEWARE_CLASSES に追加してインストールします。スーパーユーザーとしてログインしている場合、または settings.DEBUG が True の場合は常にアクティブです。これを使用するには、'profile=1' を GET または POST パラメーターとして任意の HTTP 要求に渡します。
実行されたクエリ、時間、メソッドを示すインタラクティブな HTML が作成されます。ぜひ試してみてください。