2012-04-06 10 views
7

どのようにプロファイル Pythonのコード?それは、このコードによって行われたランタイムパイソンGoogle App Engineののpython27ランタイム(ではないのpython)をプロファイルするためにどのように

からpython runtime: - 同じことを行うにはどのようにランタイムpython27

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

の両方を提供私は正しく理解しています。あなたは 'concurrent requests'が不要な場合でも古い方法を使うことができます – Dikei

+0

おそらく、app.yamlの使用はcgiなしでテストしapp.yamlを編集したくないので良くありません。 – Chameleon

答えて

14

あなた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 
+0

よく見える私はすぐにそれをテストします... – Chameleon

+0

素晴らしい解決策! – Chameleon

6

またあなたのためだけに、この呪文の世話をするApp Engineのミニプロファイラにドロップすることができますプロファイリングされている各ページに結果をうまく表示します。

それは `あなたはまだ場合app.yaml.`でCGIスクリプトハンドラを指定することができます(cProfiler経由)(Appstatsの経由)情報PERF API呼び出しとすべての関数呼び出しのための標準的なプロファイリングデータ

https://github.com/kamens/gae_mini_profiler

関連する問題