請造訪 https://vmprof.readthedocs.org 以了解更多資訊!
pip install vmprof
python -m vmprof <your program> <your program args>
我們的建置系統將輪子運送到 PyPI(Linux、Mac OS X)。如果從原始程式碼構建,則需要安裝 CPython 開發標頭和 libunwind 標頭(僅限 Linux)。在 Windows 上,這表示您的 Python 版本需要 Microsoft Visual C++ 編譯器。
可以使用以下命令來完成開發設定:
$ virtualenv -p /usr/bin/python3 vmprof3
$ source vmprof3/bin/activate
$ python setup.py develop
需要安裝python開發包。例如,對於 Debian 或 Ubuntu,您需要的軟體包是python3-dev
和libunwind-dev
。現在是時候編寫測試並實現您的功能了。如果您希望所做的變更影響 vmprof.com,請造訪 https://github.com/vmprof/vmprof-server 並依照設定說明進行操作。
有關更多信息,請參閱 https://vmprof.readthedocs.org 上的開發部分。
vmprofshow
是VMProf隨附的命令列工具。它可以讀取配置檔案並產生格式化的輸出。
以下是如何使用vmprofshow
的範例:
運行那個會消耗 CPU 週期的小程式(啟用 vmprof):
$ pypy vmprof/test/cpuburn.py # you can find cpuburn.py in the vmprof-python repo
這將產生一個設定檔vmprof_cpuburn.dat
。現在使用vmprofshow
顯示設定檔。 vmprofshow
有多種顯示資料的模式。我們將從基於樹的模式開始。
$ vmprofshow vmprof_cpuburn.dat tree
您將看到(彩色)輸出:
$ vmprofshow vmprof_cpuburn.dat tree
100.0% <module> 100.0% tests/cpuburn.py:1
100.0% .. test 100.0% tests/cpuburn.py:35
100.0% .... burn 100.0% tests/cpuburn.py:26
99.2% ...... _iterate 99.2% tests/cpuburn.py:19
97.7% ........ _iterate 98.5% tests/cpuburn.py:19
22.9% .......... _next_rand 23.5% tests/cpuburn.py:14
22.9% ............ JIT code 100.0% 0x7fa7dba57a10
74.7% .......... JIT code 76.4% 0x7fa7dba57a10
0.1% .......... JIT code 0.1% 0x7fa7dba583b0
0.5% ........ _next_rand 0.5% tests/cpuburn.py:14
0.0% ........ JIT code 0.0% 0x7fa7dba583b0
還有一個選項--html
可以發出與 HTML 相同的資訊以在瀏覽器中查看。在這種情況下,樹枝可以互動地展開和折疊。
vmprof 支援行分析模式,該模式可以收集和顯示函數內單獨行的統計資料。
若要啟用行統計資訊的收集,請將--lines
參數新增至 vmprof:
$ python -m vmprof --lines -o < output-file > < your program > < your program args >
或從程式碼呼叫 vmprof 時,將lines=True
參數傳遞給vmprof.enable
函數。
要查看所有函數的行統計信息,請使用vmprofshow
的lines
模式:
$ vmprofshow < output-file > lines
要查看特定函數的行統計信息,請使用--filter
參數和函數名稱:
$ vmprofshow < output-file > lines --filter < function-name >
您將看到結果:
$ vmprofshow vmprof_cpuburn.dat lines --filter _next_rand
Total hits: 1170 s
File: tests/cpuburn.py
Function: _next_rand at line 14
Line # Hits % Hits Line Contents
=======================================
14 38 3.2 def _next_rand(self):
15 # http://rosettacode.org/wiki/Linear_congruential_generator
16 835 71.4 self._rand = (1103515245 * self._rand + 12345) & 0x7fffffff
17 297 25.4 return self._rand
vmprofshow
還具有flat
模式。
雖然vmprofshow
的基於樹和基於行的輸出樣式可以很好地了解從調用圖的“根”查看時花費在何處的時間,但有時需要從“葉子”獲取視圖。當存在從多個位置呼叫的函數時,這特別有用,其中每次呼叫不會消耗太多時間,但所有呼叫加在一起確實會產生巨大的成本。
$ vmprofshow vmprof_cpuburn.dat flat andreask_work@dunkel 15:24
28.895% - _PyFunction_Vectorcall:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/call.c:389
18.076% - _iterate:cpuburn.py:20
17.298% - _next_rand:cpuburn.py:15
5.863% - <native symbol 0x563a5f4eea51>:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/longobject.c:3707
5.831% - PyObject_SetAttr:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/object.c:1031
4.924% - <native symbol 0x563a5f43fc01>:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/abstract.c:787
4.762% - PyObject_GetAttr:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/object.c:931
4.373% - <native symbol 0x563a5f457eb1>:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/abstract.c:1071
3.758% - PyNumber_Add:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/abstract.c:957
3.110% - <native symbol 0x563a5f47c291>:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/longobject.c:4848
1.587% - PyNumber_Multiply:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/abstract.c:988
1.166% - _PyObject_GetMethod:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/object.c:1139
0.356% - <native symbol 0x563a5f4ed8f1>:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/longobject.c:3432
0.000% - <native symbol 0x7f0dce8cca80>:-:0
0.000% - test:cpuburn.py:36
0.000% - burn:cpuburn.py:27
有時可能需要排除“本機”函數:
$ vmprofshow vmprof_cpuburn.dat flat --no-native andreask_work@dunkel 15:27
53.191% - _next_rand:cpuburn.py:15
46.809% - _iterate:cpuburn.py:20
0.000% - test:cpuburn.py:36
0.000% - burn:cpuburn.py:27
請注意,輸出表示每個函數花費的時間,不包括呼叫的函數。 (在--no-native
模式下,本機程式碼被呼叫者仍包含在總數中。)
有時,可能還需要取得包含被呼叫函數的計時:
$ vmprofshow vmprof_cpuburn.dat flat --include-callees andreask_work@dunkel 15:31
100.000% - <native symbol 0x7f0dce8cca80>:-:0
100.000% - test:cpuburn.py:36
100.000% - burn:cpuburn.py:27
100.000% - _iterate:cpuburn.py:20
53.191% - _next_rand:cpuburn.py:15
28.895% - _PyFunction_Vectorcall:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/call.c:389
7.807% - PyNumber_Multiply:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/abstract.c:988
7.483% - <native symbol 0x563a5f457eb1>:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/abstract.c:1071
6.220% - <native symbol 0x563a5f4eea51>:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/longobject.c:3707
5.831% - PyObject_SetAttr:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/object.c:1031
4.924% - <native symbol 0x563a5f43fc01>:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/abstract.c:787
4.762% - PyObject_GetAttr:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/object.c:931
3.758% - PyNumber_Add:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/abstract.c:957
3.110% - <native symbol 0x563a5f47c291>:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/longobject.c:4848
1.166% - _PyObject_GetMethod:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/object.c:1139
0.356% - <native symbol 0x563a5f4ed8f1>:/home/conda/feedstock_root/build_artifacts/python-split_1608956461873/work/Objects/longobject.c:3432
該視圖與“樹”視圖非常相似,只是沒有嵌套。