Cha*_*eon 7 python profile google-app-engine python-2.7
如何在Google App Engine运行时python27下分析 python代码?
在运行时python中,它是由这段代码完成的 - python运行时:
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()
Run Code Online (Sandbox Code Playgroud)
在运行时,python27必须以不同的方式完成,因为没有主要调用 - 如何做同样的事情 - 我想切换到python27但不是没有分析.如何在python27中附加分析器- python27运行时?
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)])
Run Code Online (Sandbox Code Playgroud)
小智 16
您可以通过在appengine_config.py中插入来使用WSGI中间件来分析WSGI应用程序:
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
Run Code Online (Sandbox Code Playgroud)
您也可以直接放入App Engine Mini Profiler,它会为您处理这个咒语,并在每个被分析的页面上很好地呈现结果.
它提供API调用perf信息(通过Appstats)和所有函数调用的标准分析数据(通过cProfiler)
https://github.com/kamens/gae_mini_profiler
| 归档时间: |
|
| 查看次数: |
1781 次 |
| 最近记录: |