How to profile a Google App Engine python27 application (not python)

How is the python code profile in Google App Engine runtime python27 ?

In runtime python, this was done using this code - 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() 

In runtime, python27 needs to be done differently since there are no basic calls - how to do the same - I want to switch to python27, but not without profiling. How to connect 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)]) 
+7
source share
2 answers

You can profile a WSGI application using the WSGI middleware by pasting into your 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 
+15
source

You can also just take a look at the App Engine Mini Profiler, which will take care of this spell for you and present the results well on each profiled page.

It provides both primary API information (via Appstats) and standard profiling data for all function calls (via cProfiler)

https://github.com/kamens/gae_mini_profiler

+6
source

All Articles