聲明:
本博客歡迎轉發,但請保留原作者信息!內容系本人學習、研究和總結,如有雷同,實屬榮幸!
原文地址:http://blog.csdn.net/gtt116/article/details/8913519
概述
CPU Profiler
$ pip install line_profiler
會安裝一個可執行文件“kernprof.py”。然後修改目標代碼,只要在需要測試的函數加上@profile,不用import任何東西,只要加上這一句即可,因爲kernprof會動態插入。@profile
def run():
a = [1]*100
b = [x*x*x for x in a ]
c = [x for x in b]
d = c*2
run()
kernprof.py -l -v test.py
-l表示動態插入profile.Wrote profile results to test.py.lprof
Timer unit: 1e-06 s
File: test.py
Function: run at line 1
Total time: 0.000322 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
1 @profile
2 def run():
3 1 5 5.0 1.6 a = [1]*100
4 101 166 1.6 51.6 b = [x*x*x for x in a ]
5 101 147 1.5 45.7 c = [x for x in b]
6 1 4 4.0 1.2 d = c*2
Memory Profiler
$ pip install -U memory_profiler
$ pip install psutil
(psutil不是必須,但是安裝上會提高memory_profiler的效率)$ python -m memory_profiler test.py
Filename: test.py
Line # Mem usage Increment Line Contents
================================================
1 @profile
2 9.488 MB 0.000 MB def run():
3 9.496 MB 0.008 MB a = [1]*100
4 9.504 MB 0.008 MB b = [x*x*x for x in a ]
5 9.504 MB 0.000 MB c = [x for x in b]
6 9.504 MB 0.000 MB d = c*2
與之前的結果類似,給出了某行的內存的增量。Profile Openstack Nova
@profile
def main():
config.parse_args([]) # 這裏參數原本是sys.argv,因爲parse_args()會修改sys.argv,導致memory_profiler不能正常執行,所以我決定不傳sys.argv進去,既默認不帶任何參數啓動nova-compute。
logging.setup('nova')
utils.monkey_patch()
if not CONF.conductor.use_local:
block_db_access()
server = service.Service.create(binary='nova-compute',
topic=CONF.compute_topic,
db_allowed=False)
service.serve(server)
service.wait()
kernprof.py -l -v bin/nova-compute
Wrote profile results to nova-compute.lprof
Timer unit: 1e-06 s
File: /opt/stack/nova/nova/cmd/compute.py
Function: main at line 52
Total time: 80.6356 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
52 @profile
53 def main():
54 1 15483 15483.0 0.0 config.parse_args(sys.argv)
55 1 1859 1859.0 0.0 logging.setup('nova')
56 1 85 85.0 0.0 utils.monkey_patch()
57
58 1 132 132.0 0.0 if not CONF.conductor.use_local:
59 1 88 88.0 0.0 block_db_access()
60
61 1 4 4.0 0.0 server = service.Service.create(binary='nova-compute',
62 1 106 106.0 0.0 topic=CONF.compute_topic,
63 1 51468528 51468528.0 63.8 db_allowed=False)
64 1 205 205.0 0.0 service.serve(server)
65 1 29149130 29149130.0 36.1 service.wait()
$ python -m memory_profiler bin/nova-compute
Filename: nova/cmd/compute.py
Line # Mem usage Increment Line Contents
================================================
52 @profile
53 32.883 MB 0.000 MB def main():
54 33.027 MB 0.145 MB config.parse_args([])
55 33.043 MB 0.016 MB logging.setup('nova')
56 33.043 MB 0.000 MB utils.monkey_patch()
57
58 33.043 MB 0.000 MB if not CONF.conductor.use_local:
59 33.047 MB 0.004 MB block_db_access()
60
61 33.047 MB 0.000 MB server = service.Service.create(binary='nova-compute',
62 33.047 MB 0.000 MB topic=CONF.compute_topic,
63 47.371 MB 14.324 MB db_allowed=False)
64 47.371 MB 0.000 MB service.serve(server)
65 49.434 MB 2.062 MB service.wait()