制作一个python cProfile脚本来分析另一个目录中包含的另一个python脚本

时间:2016-09-08 20:30:15

标签: python cprofile

我试图创建一个模块,让你基本上可以做同样的PyCharm专业版,在程序执行期间获取配置文件快照,但作为模块而不是整个UI

为了做到这一点,我试图首先创建一个接收另一个脚本路径的脚本并打印后面脚本的配置文件(我将始终假设后面的脚本有一个 main )。

这是我得到的:

import cProfile
import imp

pr = cProfile.Profile()
pr.disable()
def profileScript(script):
    pr.enable()
    pr.run('script')
    pr.disable()
    pr.print_stats()

if __name__ == '__main__':
    script = imp.load_source('meihn','A:\TestProgram\meihn.py')
    profileScript(script)

和测试脚本i' m profiling:

def func1(a,b):
    return a + b

def func2(lista, listb):
    listc = []
    for i in range(0, len(lista)):
        listc.append(func1(lista[i],listb[i]))
    return listc

def func3(list):
    a = 1
    for i in list:
        a *= i
    return a

if __name__ == '__main__':
    for i in range(0, 1000):
        la = [1, 2, 3, 4]
        lb = [5, 5, 5, 5]

        lc = func2(la, lb)

        ld = func2(la, lc)
        le = func2(lb, lc)

     input()

     for i in range(0, 10000):
          lf = func3(le)

     input()

它会运行,但它不会在输入处停止,结果如下:

         5 function calls in 0.000 seconds

Ordered by: standard name

    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 cProfile.py:132(run)
        1    0.000    0.000    0.000    0.000 cProfile.py:137(runctx)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {method 'enable' of '_lsprof.Profiler' objects}



Process finished with exit code 0

这基本上告诉我它没有调用meihn.py中的任何函数,我认为这是因为它没有因为某些原因而调用main。

对此以及如何进行异步快照的任何评论都会有很大的帮助。

我正在使用python 2.7

提前致谢。

1 个答案:

答案 0 :(得分:0)

很抱歉打破它,但你的测试脚本没有任何可以调用的主要功能。 : - )

无论如何,我的脚本有相同的结果,它有一个主要功能。我认为探查器无法“透视”你的呼叫机制。

通过查看cProfile.py,我想出了以下解决方案:

import os
import sys
import cProfile

pr = cProfile.Profile()
pr.disable()


def profile_script(progname):
    sys.path.insert(0, os.path.dirname(progname))
    with open(progname, 'rb') as fp:
        code = compile(fp.read(), progname, 'exec')
    globs = {
        '__file__': progname,
        '__name__': '__main__',
        '__package__': None,
    }

    pr.enable()
    pr.runctx(code, globs, None)
    pr.disable()
    pr.print_stats()


if __name__ == '__main__':
    profile_script('A:\TestProgram\meihn.py')

此外,我稍微修改了您的测试脚本,因为将inputexec一起使用会导致语法错误:

def func1(a, b):
    return a + b


def func2(lista, listb):
    listc = []
    for i in range(0, len(lista)):
        listc.append(func1(lista[i],listb[i]))
    return listc


def func3(list):
    a = 1
    for i in list:
        a *= i
    return a


if __name__ == '__main__':
    for i in range(0, 1000):
        la = [1, 2, 3, 4]
        lb = [5, 5, 5, 5]

        lc = func2(la, lb)

        ld = func2(la, lc)
        le = func2(lb, lc)

    raw_input('input: ')

    for i in range(0, 10000):
        lf = func3(le)

    raw_input('input: ')

这给了我:

         43098 function calls in 11.249 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        4    0.000    0.000    0.000    0.000 Queue.py:107(put)
        4    0.000    0.000    0.000    0.000 Queue.py:204(_put)
        1    0.000    0.000   11.249   11.249 cProfile.py:137(runctx)
        1    0.023    0.023   11.249   11.249 meihn.py:1(<module>)
    12000    0.002    0.000    0.002    0.000 meihn.py:1(func1)
    10000    0.010    0.000    0.010    0.000 meihn.py:12(func3)
     3000    0.030    0.000    0.035    0.000 meihn.py:5(func2)
        4    0.000    0.000    0.001    0.000 pydev_console_utils.py:145(__pydev_run_command)
        2    0.000    0.000   11.180    5.590 pydev_console_utils.py:154(readline)
        4    0.000    0.000    0.000    0.000 pydevd_comm.py:427(add_command)
        4    0.000    0.000    0.000    0.000 pydevd_comm.py:567(__init__)
        4    0.000    0.000    0.000    0.000 pydevd_comm.py:843(make_input_requested_message)
        4    0.000    0.000    0.000    0.000 pydevd_utils.py:69(is_string)
        4    0.000    0.000    0.000    0.000 pydevd_utils.py:72(to_string)
        4    0.000    0.000    0.000    0.000 pydevd_utils.py:86(quote_smart)
        4    0.000    0.000    0.000    0.000 threading.py:299(_is_owned)
        4    0.000    0.000    0.000    0.000 threading.py:372(notify)
        4    0.000    0.000    0.000    0.000 threading.py:63(_note)
        4    0.000    0.000    0.000    0.000 urllib.py:1248(quote)
        8    0.000    0.000    0.000    0.000 {isinstance}
     3000    0.000    0.000    0.000    0.000 {len}
        8    0.000    0.000    0.000    0.000 {method 'acquire' of 'thread.lock' objects}
        4    0.000    0.000    0.000    0.000 {method 'append' of 'collections.deque' objects}
    12000    0.001    0.000    0.001    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {method 'enable' of '_lsprof.Profiler' objects}
        2   11.179    5.590   11.179    5.590 {method 'readline' of 'file' objects}
        7    0.000    0.000    0.000    0.000 {method 'release' of 'thread.lock' objects}
        3    0.000    0.000    0.000    0.000 {method 'remove' of 'list' objects}
        4    0.000    0.000    0.000    0.000 {method 'rstrip' of 'str' objects}
     3002    0.001    0.000    0.001    0.000 {range}
        2    0.001    0.000   11.181    5.590 {raw_input}
相关问题