watch cpu and watch python

yum install sysstat
mpstat

 mpstat -P ALL
sar

To get multiple samples and multiple reports set an output file for the sar command. Run the sar command as a background process using.
# sar -o output.file 12 8 >/dev/null 2>&1 &

Better use nohup command so that you can logout and check back report later on:
# nohup sar -o output.file 12 8 >/dev/null 2>&1 &

All data is captured in binary form and saved to a file (data.file). The data can then be selectively displayed ith the sar command using the -f option.
# sar -f data.file

iostat
 python3.6 -m timeit -n 5 -r 5 -s "import julia1" "julia1│sys     0m0.000s
.calc_pure_python(draw_output=True, desired_width=1000, max_iterations=300)

# ipython
%timeit calc_pure_python(draw_output=True, desired_width=1000, max_iterations=300)

# shell
/usr/bin/time -p python julia1_nopil.py
# out
real # all time
user # cup time but not contain kernal function time
sys  # kernal  function time 
sys + user <= real because wait i/o
# contain time of start Interpreter

[root@test-server cpu_profiling]# /usr/bin/time --verbose python3.6 julia1_nopil.py
Length of x: 1000
Total elements: 1000000
calculate_z_serial_purepython took 7.895394802093506 seconds
        Command being timed: "python3.6 julia1_nopil.py"
        User time (seconds): 8.45
        System time (seconds): 0.05
        Percent of CPU this job got: 99%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:08.51
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 96712
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0  # read page from hand  to RAM # 内存密集型有
        Minor (reclaiming a frame) page faults: 26283
        Voluntary context switches: 1
        Involuntary context switches: 95
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0

[root@ecpro-draw-app-test-server cpu_profiling]# python3.6 -m cProfile -s cumulative julia1_nopil.py
Length of x: 1000
Total elements: 1000000
calculate_z_serial_purepython took 11.42645788192749 seconds
         36221995 function calls in 12.236 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000   12.236   12.236 {built-in method builtins.exec}
        1    0.033    0.033   12.236   12.236 julia1_nopil.py:1(<module>)
        1    0.638    0.638   12.204   12.204 julia1_nopil.py:25(calc_pure_python)
        1    8.376    8.376   11.426   11.426 julia1_nopil.py:9(calculate_z_serial_purepython)
 34219980    3.050    0.000    3.050    0.000 {built-in method builtins.abs}
  2002000    0.133    0.000    0.133    0.000 {method 'append' of 'list' objects}
        1    0.006    0.006    0.006    0.006 {built-in method builtins.sum}
        3    0.000    0.000    0.000    0.000 {built-in method builtins.print}
        4    0.000    0.000    0.000    0.000 {built-in method builtins.len}
        2    0.000    0.000    0.000    0.000 {built-in method time.time}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


# python3.6 -m cProfile -o profile.stats -s cumulative julia1.py
import pstats
In [6]: p = pstats.Stats("profile.stats")                                                                                                                                                                          

In [7]: p.sort_stats("cumulative")                                                                                                                                                                                 
Out[7]: <pstats.Stats at 0x7fed02abfbe0>

In [8]: p.print_stats()                                                                                                                                                                                            
Thu Apr  4 20:48:13 2019    profile.stats

         36252766 function calls (36251973 primitive calls) in 12.711 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     62/1    0.003    0.000   12.711   12.711 {built-in method builtins.exec}
        1    0.045    0.045   12.711   12.711 julia1.py:1(<module>)
        1    0.653    0.653   12.649   12.649 julia1.py:78(calc_pure_python)
        1    8.391    8.391   11.468   11.468 julia1.py:63(calculate_z_serial_purepython)
 34219980    3.076    0.000    3.076    0.000 {built-in method builtins.abs}
        1    0.029    0.029    0.376    0.376 julia1.py:25(show_greyscale)
        1    0.207    0.207    0.207    0.207 julia1.py:32(<listcomp>)
  2003006    0.146    0.000    0.146    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.122    0.122 /usr/local/lib64/python3.6/site-packages/PIL/Image.py:2021(show)
        1    0.000    0.000    0.122    0.122 /usr/local/lib64/python3.6/site-packages/PIL/Image.py:2904(_show)
        1    0.000    0.000    0.122    0.122 /usr/local/lib64/python3.6/site-packages/PIL/Image.py:2909(_showxv)
     75/9    0.000    0.000    0.120    0.013 <frozen importlib._bootstrap>:966(_find_and_load)
     75/9    0.000    0.000    0.119    0.013 <frozen importlib._bootstrap>:936(_find_and_load_unlocked)
   111/10    0.000    0.000    0.119    0.012 <frozen importlib._bootstrap>:211(_call_with_frames_removed)
   144/10    0.000    0.000    0.119    0.012 <frozen importlib._bootstrap>:997(_handle_fromlist)
     22/8    0.000    0.000    0.119    0.015 {built-in method builtins.__import__}
     71/9    0.000    0.000    0.119    0.013 <frozen importlib._bootstrap>:651(_load_unlocked)
     53/8    0.000    0.000    0.118    0.015 <frozen importlib._bootstrap_external>:672(exec_module)
       53    0.000    0.000    0.083    0.002 <frozen importlib._bootstrap_external>:743(get_code)
       53    0.000    0.000    0.080    0.002 <frozen importlib._bootstrap_external>:485(_compile_bytecode)
       53    0.080    0.002    0.080    0.002 {built-in method marshal.loads}
        1    0.000    0.000    0.070    0.070 /usr/local/lib64/python3.6/site-packages/PIL/ImageShow.py:43(show)

p.print_callees() 
# package 
pip install wxpython
pip install runsnake

# 逐行分析被修饰函数的CPU开销
 kernprof -l -v julia1_lineprofiler.py
# 拆分一行的代码,重新使用kernprof 测试具体哪个操作耗时

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值