我正在分析的Flask应用程序花了很长时间来呈现它的Jinja2模板。
我已经安装了烧瓶线型板,这是很有趣的,但不幸的是,我不允许深入模板呈现来查看所有的时间都花在哪里了。
分析Jinja2模板的最佳方法是什么?
发布于 2016-11-02 16:15:49
问得好。我通常对分析器没有多大用处,所以这是一个很好的学习借口。下面的示例是:https://docs.python.org/2/library/profile.html#module-cProfile,我编写了一个简单的分析jinja模板的示例。
import cProfile as profile
import pstats
import StringIO
import jinja2
import time
pr = profile.Profile()
def slow():
time.sleep(2)
return "Booga!"
template = jinja2.Template(r'''
{% for i in RANGE1 %}<h1>hello world {{ i}}</h1>{% endfor %}
{% for i in RANGE2 %}<h1>foo bar {{ i}}</h1>{% endfor %}
{{ SLOW() }}
'''
)
# here is the bit we want to profile
pr.enable()
context = {"RANGE1": range(1000000), "RANGE2":range(100), "SLOW":slow}
template.render(context)
pr.disable()
s = StringIO.StringIO()
ps = pstats.Stats(pr, stream=s).sort_stats("cumulative")
ps.print_stats()
print(s.getvalue())
下面是报告的一个片段:
1000130 function calls in 2.448 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 2.438 2.438 /usr/local/lib/python2.7/dist-packages/jinja2/environment.py:974(render)
1 0.122 0.122 2.438 2.438 {method 'join' of 'unicode' objects}
1000104 0.315 0.000 2.317 0.000 <template>:5(root)
1 0.000 0.000 2.002 2.002 /usr/local/lib/python2.7/dist-packages/jinja2/runtime.py:169(call)
1 0.000 0.000 2.002 2.002 profilej.py:10(slow)
1 2.002 2.002 2.002 2.002 {time.sleep}
2 0.010 0.005 0.010 0.005 {range}
1 0.000 0.000 0.000 0.000 /usr/local/lib/python2.7/dist-packages/jinja2/environment.py:1015(new_context)
1 0.000 0.000 0.000 0.000 /usr/local/lib/python2.7/dist-packages/jinja2/runtime.py:55(new_context)
1 0.000 0.000 0.000 0.000 /usr/local/lib/python2.7/dist-packages/jinja2/runtime.py:115(__init__)
3 0.000 0.000 0.000 0.000 {hasattr}
1 0.000 0.000 0.000 0.000 /usr/local/lib/python2.7/dist-packages/jinja2/_compat.py:59(<lambda>)
1 0.000 0.000 0.000 0.000 /usr/local/lib/python2.7/dist-packages/jinja2/nodes.py:81(__init__)
3 0.000 0.000 0.000 0.000 {getattr}
3 0.000 0.000 0.000 0.000 /usr/local/lib/python2.7/dist-packages/jinja2/runtime.py:149(resolve)
1 0.000 0.000 0.000 0.000 /usr/local/lib/python2.7/dist-packages/jinja2/runtime.py:126(<genexpr>)
1 0.000 0.000 0.000 0.000 {callable}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.000 0.000 0.000 0.000 {method 'iteritems' of 'dict' objects}
1 0.000 0.000 0.000 0.000 {isinstance}
正如我说的,我没有太多经验来解释分析器的输出,但我认为在这个示例中,您可以看到程序在time.sleep上花费了2秒多一点,这是由slow()调用的。其余的时间都被join占用了。我假设这就是Jinja2处理我的两个for循环的方式。
将此示例应用到一个烧瓶应用程序中应该不会太难,只需在模板生成步骤周围添加分析位,并将报告写入文件。也许您甚至可以从web应用程序中提取模板,并在瓶外对它们进行分析。
我希望这是有帮助的。
发布于 2016-11-02 17:54:00
对于多线程应用程序(如正在运行的Flask服务器),我发现通常的Python分析工具并不是很好。
我在雅皮上取得了很好的效果,它是为多线程应用程序设计的。这很简单:
import yappi
yappi.start()
[.. do stuff ..]
yappi.stop()
yappi.convert2pstats(yappi.get_func_stats()).dump_stats('myfile.pstats')
它将配置文件数据保存在与pstats
兼容的文件中,以便您可以在python中交互地检查它:
>>> import pstats
>>> s = pstats.Stats('myfile.pstats')
>>> s.strip_dirs().sort_stats('cumtime').print_stats()
如果你想变得聪明,你可以把start()
位和stop()
位放在烧瓶处理程序中,这样你就可以点击一个URL开始分析,驱动你的应用程序,然后点击一个不同的URL来停止分析并写出统计文件。
https://stackoverflow.com/questions/26807187
复制相似问题