2012-04-06 19 views
7

Come profilo codice Python sotto Google App Engine runtimepython27?Come al profilo Google App Engine runtime python27 (non python)

Nel runtime pitone è stato fatto da questo codice - 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() 

Nel runtime python27 si deve essere fatto in modo diverso in quanto non v'è alcuna principali chiamate - come fare la stessa cosa - Voglio passare a python27 ma non senza profilazione. Come collegare il profiler in 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

'è ancora possibile specificare gestori di script CGI in app.yaml.' Se Capisco correttamente, puoi ancora usare i vecchi metodi se non hai bisogno di 'richieste simultanee' – Dikei

+0

Probabilmente ma l'uso di app.yaml non va bene dato che vuoi testare senza cgi e non modificare app.yaml ogni test (lento) . – Chameleon

risposta

14

È possibile profilo di un'applicazione WSGI usando WSGI middleware inserendo nel vostro 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

Sembra buono Lo testerò presto ... – Chameleon

+0

Ottima soluzione! – Chameleon

6

È anche possibile rilasciare in App Engine Mini Profiler, che si occupa di questo incantesimo per voi e presenta i risultati piacevolmente su ogni pagina che viene profilata.

Esso fornisce sia chiamata API perf informazioni (tramite Appstats) ei dati di profilazione standard per tutte le chiamate di funzione (via cProfiler)

https://github.com/kamens/gae_mini_profiler