Python:用pyinstrument做性能分析
2022-03-21 14:50
911 查看
导引
在计算密集型计算或一些Web应用中,我们常常需要对代码做性能分析。在Python中,最原始的方法即是使用
time包中的
time函数(该函数以秒为计时单位):
from time import sleep, time def func1(): sleep(0.001) def func2(): sleep(2) begin1 = time() func1() end1 = time() begin2 = time() func2() end2 = time() print("func1 consume: %f, func2 consume:%f, func3 consume: %f"\ % (end1-begin1, end2-begin2, end2-begin1))
控制台输出如下:
func1 consume: 0.001271, func2 consume:2.000421, func3 consume: 2.001692
pyinstrument基本用法
但是一旦函数多了起来,这种方式显然过于繁杂。类似C语言中的cProfile,在Python中,也有专门的性能分析工具
pyinstrument(该库非内置,需要使用conda/pip安装),我们在复杂的项目中可以使用它来代替简陋的
time.time()。
首先来看一下基本的使用,它的使用框架如下:
from pyinstrument import Profiler from time import sleep def func1(): sleep(0.1) def func2(): sleep(2) profiler = Profiler() profiler.start() # 这里是你要分析的代码,我们这里分析func1和func2两个函数 func1() func2() profiler.stop() profiler.print()
可以看到,该工具也将其成功分析出了个函数的运行时间,并为我们标红指出了运行2s的func2函数是性能瓶颈:
如果我们进一步调低函数的运行时间:
def func3(): sleep(0.0001) profiler = Profiler() profiler.start() func3() profiler.stop() profiler.print()
此时会显示“No samples were recorded”,如下:
这是因为你的代码运行时间小于了1ms,如果你仍然想分析这段代码,你可以选择将间隔值调到比默认的0.001(1ms)小,比如这样:
profiler = Profiler(interval=0.0001)
此时你会发现,func3也能被检测出来了:
此外,如果你要在浏览器中查看分析结果,可以使用
profiler.open_in_browser()代替
profiler.print()的控制台打印输出:
也可以使用
profiler.output_html()将profile以html形式输出。
分析Flask中的web响应性能
我们也可以对Flask应用进行性能分析,具体的用法如下:
from flask import Flask, g, make_response, request app = Flask(__name__) @app.before_request def before_request(): if "profile" in request.args: g.profiler = Profiler() g.profiler.start() @app.after_request def after_request(response): if not hasattr(g, "profiler"): return response g.profiler.stop() output_html = g.profiler.output_html() return make_response(output_html)
这样程序会检测每个request中的
?profile参数,如果检测到则会开始分析。在运行了profiler的request结束后,它会生成一个html输出替代掉真实的response并返回。
参考
相关文章推荐
- python性能分析之line_profiler模块
- python 性能分析器 profile
- 每天将MYSQL SLOW QUERY REPORT分发到各个邮箱供分析改善数据库性能-PYTHON
- Python性能分析 (Profiling)
- 2.python数据结构的性能分析
- Day15(算法效率衡量 算法分析 常见时间复杂度 Python内置类型性能分析 数据结构)
- Python程序的性能分析指南
- python的性能分析工具line_profiler
- 【python】时间性能分析
- Python性能分析方法
- Python性能分析 (Profiling)
- 面试中常用排序算法的python实现和性能分析
- Python程序的性能分析指南
- Python判断列表是否已排序的各种方法及其性能分析
- Python 性能分析入门指南
- Python程序性能分析模块----------cProfile
- python性能分析
- python几个实际代码的性能分析
- 如何进行 Python性能分析,你才能如鱼得水?
- Python 性能分析入门指南