2012-04-06 12 views
7

Jak profil Kod pyton pod Google App Engine wykonywania python27?Jak profilu Google App Engine Runtime (python27 nie Pythona)

W czasie wykonywania Pythona to zostało wykonane przez tego kodu - python runtime:

from google.appengine.ext import webapp 

class PageHandler(webapp.RequestHandler): 
    def get(self): 
    self.response.headers['Content-Type'] = 'text/plain' 
    self.response.out.write('Hello, WebApp World!') 

def real_main(): 
    application = webapp.WSGIApplication([('/', PageHandler)], debug=True) 
    run_wsgi_app(application) 

def profile_main(): 
    # This is the main function for profiling 
    # We've renamed our original main() above to real_main() 
    import cProfile, pstats, StringIO 
    prof = cProfile.Profile() 
    prof = prof.runctx('real_main()', globals(), locals()) 
    stream = StringIO.StringIO() 
    stats = pstats.Stats(prof, stream=stream) 
    stats.sort_stats('cumulative') 
    logging.info("Profile data:\n%s", stream.getvalue()) 

if __name__ == "__main__": 
    profile_main() 

W czasie wykonywania python27 jest ma być zrobione inaczej, ponieważ nie ma głównego połączenia - jak zrobić to samo - Chcę przejść do python27, ale nie bez profilowania. Jak dołączyć profiler do python27 - python27 runtime?

import webapp2 

class PageHandler(webapp2.RequestHandler): 
    def get(self): 
     self.response.headers['Content-Type'] = 'text/plain' 
     self.response.out.write('Hello, WebApp World!') 

app = webapp2.WSGIApplication([('/', PageHandler)]) 
+0

'Nadal można określić programy obsługi skryptów CGI w app.yaml.' If Rozumiem, że nadal możesz używać starych sposobów, jeśli nie potrzebujesz "współbieżnych żądań" – Dikei

+0

Prawdopodobnie użycie app.yaml nie jest dobre, ponieważ chcesz testować bez cgi i nie edytować app.yaml każdego testu (to wolno) . – Chameleon

Odpowiedz

14

Można profil aplikację WSGI przy użyciu WSGI middleware, wkładając w appengine_config.py:

import cProfile 
import cStringIO 
import logging 
import pstats 

def webapp_add_wsgi_middleware(app): 

    def profiling_wrapper(environ, start_response): 
    profile = cProfile.Profile() 
    response = profile.runcall(app, environ, start_response) 
    stream = cStringIO.StringIO() 
    stats = pstats.Stats(profile, stream=stream) 
    stats.sort_stats('cumulative').print_stats() 
    logging.info('Profile data:\n%s', stream.getvalue()) 
    return response 

    return profiling_wrapper 
+0

Wygląda dobrze, przetestuję to wkrótce ... – Chameleon

+0

Świetne rozwiązanie! – Chameleon

6

Można też po prostu spadek App Engine Mini Profiler, który dba o to zaklęcie dla Ciebie i ładnie prezentuje wyniki na każdej profilowanej stronie.

Zapewnia zarówno wywołanie API perf informacji (przez Appstats) oraz standardowe dane profilowania dla wszystkich wywołań funkcji (poprzez cProfiler)

https://github.com/kamens/gae_mini_profiler