python工具——Py-Spy
生活随笔
收集整理的這篇文章主要介紹了
python工具——Py-Spy
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Py-Spy是Python程序的抽樣分析器,可視化查看Python程序在哪些地方花了更多時間
1.安裝
pip install py-spy
驗證安裝是否成功
py-spy -h
py-spy 0.3.3
Sampling profiler for Python programs
USAGE:
py-spy <SUBCOMMAND>
OPTIONS:
-h, --help Prints help information
-V, --version Prints version information
SUBCOMMANDS:
record Records stack trace information to a flamegraph, speedscope or
raw file
top Displays a top like view of functions consuming CPU
dump Dumps stack traces for a target program to stdout
help Prints this message or the help of the given subcommand(s)
從上面看到有三個子命令
record:生成火焰圖
top:實時查看每個函數運行時間并統計
dump:顯示每個python線程的當前調用堆棧
2.示例
main.py
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
is_offer: bool = None
@app.get("/")
def home():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_name": item.name, "item_id": item_id}
啟動
uvicorn main:app --host 0.0.0.0
(1)record
生成火焰圖
py-spy record -o profile.svg --pid 11004
在Ubuntu下會出現
Permission Denied: Try running again with elevated permissions by going 'sudo env "PATH=$PATH" !!'
執行
sudo env "PATH=$PATH" py-spy record -o profile.svg --pid 11004
使用ab壓力測試
ab -n 10000 -c 100 http://localhost:8000/
生成的火焰圖
通過生成火焰圖分析程序瓶頸,看圖中最下方哪個峰頂是平的,程序的性能問題就可以從那入手去解決
(2)top
顯示了在python程序中花費最多時間的功能的實時視圖
py-spy top --pid 11004
說明:
參數
按%Own排序:當前在該函數中花費的時間的百分比 按%Total排序:函數及其子級中當前的時間百分比 按OwnTime排序:函數中花費的總時間 按TotalTime排序:該函數及其子項花費的總時間
通過OwnTime可以比較直接的看出程序運行中,所占比消耗時間最多的函數
https://github.com/benfred/py-spy
(3)dump
顯示每個線程的調用棧
$ sudo env "PATH=$PATH" py-spy dump --pid 1615
Process 1615: /usr/bin/python /home/baby/.local/bin/uvicorn main:app --host 0.0.0.0
Python v3.7.9 (/usr/bin/python3.7)
Thread 1615 (idle): "MainThread"
select (selectors.py:468)
_run_once (asyncio/base_events.py:1750)
run_forever (asyncio/base_events.py:541)
run_until_complete (asyncio/base_events.py:574)
run (uvicorn/server.py:48)
run (uvicorn/main.py:386)
main (uvicorn/main.py:362)
invoke (click/core.py:610)
invoke (click/core.py:1066)
main (click/core.py:782)
__call__ (click/core.py:829)
<module> (uvicorn:8)
Thread 1624 (idle): "ThreadPoolExecutor-0_0"
_worker (concurrent/futures/thread.py:78)
run (threading.py:870)
_bootstrap_inner (threading.py:926)
_bootstrap (threading.py:890)
Thread 1625 (idle): "ThreadPoolExecutor-0_1"
_worker (concurrent/futures/thread.py:78)
run (threading.py:870)
_bootstrap_inner (threading.py:926)
_bootstrap (threading.py:890)
Thread 1626 (idle): "ThreadPoolExecutor-0_2"
_worker (concurrent/futures/thread.py:78)
run (threading.py:870)
_bootstrap_inner (threading.py:926)
_bootstrap (threading.py:890)
Thread 1710 (idle): "ThreadPoolExecutor-0_3"
_worker (concurrent/futures/thread.py:78)
run (threading.py:870)
_bootstrap_inner (threading.py:926)
_bootstrap (threading.py:890)
Thread 1711 (idle): "ThreadPoolExecutor-0_4"
_worker (concurrent/futures/thread.py:78)
run (threading.py:870)
_bootstrap_inner (threading.py:926)
_bootstrap (threading.py:890)
注:
如果在windows下,使用dump,會報錯
>py-spy dump --pid 4644
Error: 函數不正確。 (os error 1)
總結
以上是生活随笔為你收集整理的python工具——Py-Spy的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 读fnl数据
- 下一篇: Java 组件化(gradle)