【问题标题】:Make a python cProfile script to profile another python script contained in a different directory制作一个 python cProfile 脚本来分析另一个目录中包含的另一个 python 脚本
【发布时间】:2016-09-08 20:30:14
【问题描述】:

我正在尝试制作一个模块,让您可以执行与 PyCharm 专业版基本相同的操作,在程序执行期间拍摄个人资料快照,但作为一个模块而不是整个 UI

为了做到这一点,我试图首先制作一个脚本,它接收另一个脚本的路径并打印后面的脚本的配置文件(我总是假设后面的脚本有一个 ma​​in)。

这是我得到的:

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)

以及我正在分析的测试脚本:

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

提前致谢。

【问题讨论】:

    标签: python cprofile


    【解决方案1】:

    很抱歉打扰您,但您的测试脚本没有任何可以调用的主函数。 :-)

    无论如何,我的脚本得到了相同的结果,它有一个 main 函数。我想探查器只是不能“看穿”你的调用机制。

    通过查看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}
    

    【讨论】:

      猜你喜欢
      • 2020-04-21
      • 2017-03-30
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      相关资源
      最近更新 更多