2012-11-26 5 views
2

정렬, 내가 사용하고있는 아래의 미들웨어 Cprofiler 조각/http://djangosnippets.org/snippets/727/에서)cProfiler 장고 미들웨어 : 통계 장고에서

어떻게 그것을 정렬하는 데 사용되는 것을 변경합니까? sort_stats()를 사용하려면 어디에서 코드에 해당되는가요?

import sys 
import cProfile 
from cStringIO import StringIO 
from django.conf import settings 

class ProfilerMiddleware(object): 
    def process_view(self, request, callback, callback_args, callback_kwargs): 
     if settings.DEBUG and 'prof' in request.GET: 
      self.profiler = cProfile.Profile() 
      args = (request,) + callback_args 
      return self.profiler.runcall(callback, *args, **callback_kwargs) 

    def process_response(self, request, response): 
     if settings.DEBUG and 'prof' in request.GET: 
      self.profiler.create_stats() 
      out = StringIO() 
      old_stdout, sys.stdout = sys.stdout, out 
      self.profiler.print_stats(1) 
      sys.stdout = old_stdout 
      response.content = '<pre>%s</pre>' % out.getvalue() 
     return response 

답변

0

나는 sort_stats 함수를 찾고 있으며 print_stats 바로 앞에 있어야한다고 생각합니다.

def process_response(self, request, response): 
    if settings.DEBUG and 'prof' in request.GET: 
     self.profiler.create_stats() 
     out = StringIO() 
     old_stdout, sys.stdout = sys.stdout, out 
     self.profiler.sort_stats('name') 
     self.profiler.print_stats(1) 
     sys.stdout = old_stdout 
     response.content = '<pre>%s</pre>' % out.getvalue() 
    return response 

또한이 http://docs.python.org/2/library/profile.html

+0

: "프로필 sort_stats ''오브젝트가 속성이 없습니다 '"나는 여기에 해결책을 발견 – user984003

관련 문제