在Google App Engine上配置/优化网站的最佳方法 [英] Best way to profile/optimize a website on Google App Engine

查看:73
本文介绍了在Google App Engine上配置/优化网站的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

有人为此目的优化过Python代码吗? 您找到了一个好的Python分析器吗?

解决方案

我发现 Gprof2Dot 非常有用.我尝试解释的概要分析模块的输出非常不直观.

Gprof2Dot将cProfile输出转换为漂亮的图形,突出显示了最慢的链(?),并提供了每个函数的一些信息(函数名称,在此函数上花费的时间百分比以及调用次数). /p>

示例图形(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().然后,将一个名为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.

I'm currently trying to optimize my website, which is run on the Google App Engine. It's not an easy task, because I'm not using any powerful tool.

Does anyone have experience in optimizing Python code for this purpose? Have you find a good Python profiler?

解决方案

I have found Gprof2Dot extremely useful. The output of the profiling modules I've tried as pretty unintuitive to interpret.

Gprof2Dot turns the cProfile output into a pretty looking graph, with the slowest chain(?) highlighted, and a bit of information on each function (function name, percentage of time spend on this function, and number of calls).

An example graph (1429x1896px)

I've not done much with the App Engine, but when profiling non-webapp scripts, I tend to profile the script that runs all the unittests, which may not be very accurate to real-world situations

One (better?) method would be to have a script that does a fake WSGI request, then profile that.

WSGI is really simple protocol, it's basically a function that takes two arguments, one with request info and the second with a callback function (which is used for setting headers, among other things). Perhaps something like the following (which is possible-working pseudo code)...

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

Actually, the App Engine documentation explains a better way of profiling your application:

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

To profile your application's performance, first rename your application's main() function to real_main(). Then, add a new main function to your application, named profile_main() such as the one below:

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

[...]

To enable the profiling with your application, set main = profile_main. To run your application as normal, simply set main = real_main.

这篇关于在Google App Engine上配置/优化网站的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆