【问题标题】:Best way to profile/optimize a website on Google App Engine在 Google App Engine 上分析/优化网站的最佳方式
【发布时间】:2009-03-24 23:27:38
【问题描述】:

我目前正在尝试优化我的网站,该网站在 Google App Engine 上运行。这不是一件容易的事,因为我没有使用任何强大的工具。

有没有人为此目的优化 Python 代码的经验? 你找到好的 Python 分析器了吗?

【问题讨论】:

    标签: python google-app-engine


    【解决方案1】:

    我发现Gprof2Dot 非常有用。我尝试过的分析模块的输出很难解释。

    Gprof2Dot 将 cProfile 输出变成一个漂亮的图表,其中最慢的链(?)突出显示,以及每个函数的一些信息(函数名称、在此函数上花费的时间百分比和调用次数)。

    An example graph (1429x1896px)

    我在 App Engine 方面做得不多,但在分析非 webapp 脚本时,我倾向于分析运行所有单元测试的脚本,这可能对实际情况不太准确

    一个(更好的?)方法是拥有一个执行虚假 WSGI 请求的脚本,然后对其进行分析。

    WSGI 是一个非常简单的协议,它基本上是一个带有两个参数的函数,一个带有请求信息,第二个带有回调函数(用于设置标头等)。也许类似于以下内容(这是可能的工作伪代码)...

    class IndexHandler(webapp.RequestHandler):
        """Your site"""
        def get(self):
            self.response.out.write("hi")
    
    if __name__ == '__main__':
        application = webapp.WSGIApplication([
            ('.*', IndexHandler),
        ], debug=True)
    
        # Start fake-request/profiling bit
        urls = [
            "/",
            "/blog/view/hello",
            "/admin/post/edit/hello",
            "/makeanerror404",
            "/makeanerror500"
        ]
    
        def fake_wsgi_callback(response, headers):
            """Prints heads to stdout"""
            print("\n".join(["%s: %s" % (n, v) for n, v in headers]))
            print("\n")
    
        for request_url in urls:
            html = application({
            'REQUEST_METHOD': 'GET',
            'PATH_INFO': request_url},
            fake_wsgi_callback
            )
            print html
    

    实际上,App Engine 文档解释了一种更好的分析应用程序的方法:

    来自http://code.google.com/appengine/kb/commontasks.html#profiling:

    要分析应用程序的性能,首先将应用程序的 main() 函数重命名为 real_main()。然后,向您的应用程序添加一个新的 main 函数,命名为 profile_main(),如下所示:

    def profile_main():
        # This is the main function for profiling 
        # We've renamed our original main() above to real_main()
        import cProfile, pstats
        prof = cProfile.Profile()
        prof = prof.runctx("real_main()", globals(), locals())
        print "<pre>"
        stats = pstats.Stats(prof)
        stats.sort_stats("time")  # Or cumulative
        stats.print_stats(80)  # 80 = how many to print
        # The rest is optional.
        # stats.print_callees()
        # stats.print_callers()
        print "</pre>"
    

    [...]

    要使用您的应用程序启用分析,请设置main = profile_main。要正常运行您的应用程序,只需设置main = real_main。

    【讨论】:

      【解决方案2】:

      App Engine Mini Profiler 是一种新的嵌入式应用引擎性能工具,它提供 API 调用性能信息(通过 Appstats)和所有函数调用的标准分析数据(通过 cProfiler)

      https://github.com/kamens/gae_mini_profiler

      【讨论】:

      • 很棒的工具!谢谢!
      【解决方案3】:

      为了分析 API 调用,Guido van Rossum 发布了一个名为 Appstats 的库,该库将记录和显示有关您的应用的许多好东西。

      你可以在这里获取图书馆:https://sites.google.com/site/appengineappstats/

      我在我的博客上写了一篇关于它的文章(有一些截图):http://blog.dantup.com/2010/01/profiling-google-app-engine-with-appstats

      Appstats http://blog.dantup.com/pi/appstats_4_thumb.png

      【解决方案4】:

      python 网站列出了 3 个分析器可供选择:http://docs.python.org/library/profile.html

      【讨论】:

        猜你喜欢
        • 2012-10-24
        • 2012-06-19
        • 1970-01-01
        • 2011-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-19
        相关资源
        最近更新 更多