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()

実行時に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)])
4

2 に答える 2

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
于 2012-04-06T12:59:22.087 に答える
6

App Engine Mini Profilerにドロップすることもできます。これは、この呪文を処理し、プロファイリングされている各ページに結果を適切に表示します。

API呼び出しのパフォーマンス情報(Appstatsを介して)とすべての関数呼び出しの標準プロファイリングデータ(cProfilerを介して)の両方を提供します

https://github.com/kamens/gae_mini_profiler

于 2012-08-08T19:29:09.050 に答える