繁体   English   中英

在 google 的 appengine 上分析/优化网站的最佳方式

[英]Best way to profile/optimize a website on google's appengine

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

有没有人为此目的优化 python 代码的经验? 您是否找到了一个好的 python 分析器?

我发现Gprof2Dot非常有用。 我尝试过的分析模块的 output 解释起来非常不直观。

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

示例图 (1429x1896px)

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

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

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

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() function 重命名为real_main() 然后,将新的主 function 添加到您的应用程序中,命名为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

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

https://github.com/kamens/gae_mini_profiler

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

您可以在此处获取该库: https://sites.google.com/site/appengineappstats/

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

应用统计 http://blog.dantup.com/pi/appstats_4_thumb.png

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM