pyflame是uber官方开源的python性能分析工具,优点是不用修改你的Python代码,直接用命令行对你的Python进程进行profile,拿到结果看图表就好了。
按照官方文档进行安装:https://github.com/uber/pyflame
sudo apt-get install autoconf automake autotools-dev g++ pkg-config python-dev python3-dev libtool make
git clone https://github.com/uber/pyflame.git
cd pyflame
./autogen.sh
./configure
make
安装好后可执行文件在 ./src/pyflame 中。
简单的使用说明:
# Attach to PID 12345 and profile it for 1 second
pyflame -p 12345
# Attach to PID 768 and profile it for 5 seconds, sampling every 0.01 seconds
pyflame -s 5 -r 0.01 -p 768
# Run py.test against tests/, emitting sample data to prof.txt
pyflame -o prof.txt -t py.test tests/
输出的profile结果是 flamegraph.pl 格式的,flamegraph.pl是一个perl脚本,用于把上述输出结果生成svg可视化图像的。
所以你可以直接这样调用
wget "https://raw.githubusercontent.com/brendangregg/FlameGraph/master/