Programing

Django 프로파일 링

lottogame 2020. 9. 1. 07:54
반응형

Django 프로파일 링


내 django 응용 프로그램이 프로덕션에서 고통스럽게 느려졌습니다. 아마도 복잡하거나 색인화되지 않은 쿼리 때문일 것입니다.

내 애플리케이션을 프로파일 링하는 django-ish 방법이 있습니까?


Django Debug Toolbar를 사용해보십시오 . 각 페이지에서 실행되는 쿼리와 소요 시간이 표시됩니다. 정말 유용하고 강력하며 사용하기 쉬운 도구입니다.

또한 문서 에서 데이터베이스 액세스 최적화의 Django 성능에 대한 권장 사항을 읽어보십시오 .

그리고 장고 성능 정보 야곱 카플란 - 모스에 의해.


Google에 "django-profiling"을 입력하면 다음과 같은 링크가 표시됩니다.

http://code.djangoproject.com/wiki/ProfilingDjango

http://code.google.com/p/django-profiling/

http://www.rkblog.rk.edu.pl/w/p/django-profiling-hotshot-and-kcachegrind/

개인적으로 저는 미들웨어 접근 방식을 사용하고 있습니다. 즉, 각 사용자가 세션에 저장된 "프로파일 링"플래그를 토글 할 수 있으며, 프로파일 링 미들웨어가 플래그가 설정되었음을 알아 차리면 다음과 같은 Python의 핫샷 모듈을 사용합니다.

def process_view(self, request, view_func, view_args, view_kwargs):

     # setup things here, along with: settings.DEBUG=True 
     # to get a SQL dump in connection.queries

     profiler = hotshot.Profile(fname)
     response = profiler.runcall(view_func, request, *view_args, **view_kwargs)
     profiler.close()

     # process results

     return response

편집 : Konstantin에서 언급 한 http://github.com/robhudson/django-debug-toolbar 프로파일 링 SQL 쿼리의 경우 좋은 일이지만 쿼리가 정말 느리면 (아마도 수백 또는 수천 개가 있기 때문에) 브라우저에로드 될 때까지 엄청난 시간을 기다려야합니다. 그런 다음 속도가 느려서 탐색하기가 어려울 것입니다. 또한 django-debug-toolbar는 설계 상 AJAX 요청의 내부에 대한 유용한 정보를 제공 할 수 없습니다.

EDIT2 : django-extensions 에는 훌륭한 프로파일 링 명령이 내장되어 있습니다.

https://github.com/django-extensions/django-extensions/blob/master/docs/runprofileserver.rst

그냥 이렇게하고 짜잔 :

$ mkdir /tmp/my-profile-data
$ ./manage.py runprofileserver --kcachegrind --prof-path=/tmp/my-profile-data

데이터 액세스를 프로파일 링하려면 (대부분 병목 현상이 발생하는) django-live-profiler를 확인하세요 . Django Debug Toolbar와 달리 모든 요청에서 동시에 데이터를 수집하고 너무 많은 성능 오버 헤드 나 앱 내부를 노출하지 않고 프로덕션에서 실행할 수 있습니다.

이 스크린 샷 확인


여기에 뻔뻔한 플러그이지만 최근 에이 목적 으로 https://github.com/django-silk/silk만들었습니다 . django 툴바와 다소 비슷하지만 히스토리, 코드 프로파일 링 및 모든 것에 대한보다 세밀한 제어 기능이 있습니다.


For all you KCacheGrind fans, I find it's very easy to use the shell in tandem with Django's fantastic test Client for generating profile logs on-the-fly, especially in production. I've used this technique now on several occasions because it has a light touch — no pesky middleware or third-party Django applications are required!

For example, to profile a particular view that seems to be running slow, you could crack open the shell and type this code:

from django.test import Client
import hotshot

c = Client()
profiler = hotshot.Profile("yourprofile.prof")  # saves a logfile to your pwd
profiler.runcall(c.get, "/pattern/matching/your/view/")
profiler.close()

To visualize the resulting log, I've used hotshot2cachegrind:

But there are other options as well:


When the views are not HTML, for example JSON, use simple middleware methods for profiling.

Here are a couple examples:

https://gist.github.com/1229685- 보기에 들어간 모든 SQL 호출 캡처

https://gist.github.com/1229681- 보기를 만드는 데 사용 된 모든 메서드 호출을 프로파일 링합니다.

참고 URL : https://stackoverflow.com/questions/2361985/profiling-django

반응형