总是忘记,做个记录。
性能定义
一般而言,处理一个请求的速度是一个重要的性能指标(Latency, 系统延迟)。
观察工具
有时候不需要特殊工具,直接看一个http请求的时长即可。
定位瓶颈
line_profiler是一个可以查看每行代码的执行次数、执行时间的工具。
安装好后,可以在代码中对方法/函数添加装饰器@profile
,然后在命令行使用kernprof -l -v xx.py
来查看结果。
这样太麻烦,可以直接在IPython中直接使用。
导入:
In [1]: %load_ext line_profiler
help:
In [2]: %lprun?
Docstring:
Execute a statement under the line-by-line profiler from the
line_profiler module.
Usage:
%lprun -f func1 -f func2 <statement>
The given statement (which doesn't require quote marks) is run via the
LineProfiler. Profiling is enabled for the functions specified by the -f
options. The statistics will be shown side-by-side with the code through the
pager once the statement has completed.