【问题标题】:How can I analyze a file created with pstats.dump_stats(filename) off line?如何离线分析使用 pstats.dump_stats(filename) 创建的文件?
【发布时间】:2015-06-20 06:13:30
【问题描述】:

我基本上做了以下事情:

import cProfile, pstats, StringIO
pr = cProfile.Profile()
pr.enable()
# ... my code did something ...
pr.disable()
s = StringIO.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)

ps.dump_stats('stats.dmp')  # dump the stats to a file named stats.dmp

所以现在我已经离线存储了名为“stats.dmp”的文件。

如何使用 pstats 分析此文件以供人类使用?

提前致谢。

【问题讨论】:

    标签: python profiling analysis pstats


    【解决方案1】:

    这是我发现的内容和我生成的 Python 程序。我使用在 linux 上制作的 .dmp 文件对此进行了测试,并在 windows xp 上进行了分析。它工作得很好。 Python 文件名为“analyze_dmp.py”。

    #!/usr/local/bin/python2.7
    # -*- coding: UTF-8 -*-
    """analyze_dmp.py takes the file INFILEPATH [a pstats dump file] Producing OUTFILEPATH [a human readable python profile]
    Usage:   analyze_dmp.py INFILEPATH  OUTFILEPATH
    Example: analyze_dmp.py stats.dmp   stats.log
    """
    # --------------------------------------------------------------------------
    # Copyright (c) 2019 Joe Dorocak  (joeCodeswell at gmail dot com)
    # Distributed under the MIT/X11 software license, see the accompanying
    # file license.txt or http://www.opensource.org/licenses/mit-license.php.
    # --------------------------------------------------------------------------
    # I added the above License by request here are my research links
    #    https://meta.stackexchange.com/q/18883/311363
    #    https://meta.stackexchange.com/q/128840/311363
    #    https://codereview.stackexchange.com/q/10746
    
    import sys, os
    import cProfile, pstats, StringIO
    
    def analyze_dmp(myinfilepath='stats.dmp', myoutfilepath='stats.log'):
        out_stream = open(myoutfilepath, 'w')
        ps = pstats.Stats(myinfilepath, stream=out_stream)
        sortby = 'cumulative'
    
        ps.strip_dirs().sort_stats(sortby).print_stats(.3)  # plink around with this to get the results you need
    
    NUM_ARGS = 2
    def main():
        args = sys.argv[1:]
        if len(args) != NUM_ARGS or "-h" in args or "--help" in args:
            print __doc__
            s = raw_input('hit return to quit')
            sys.exit(2)
        analyze_dmp(myinfilepath=args[0], myoutfilepath=args[1])
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

    • 以下更改使代码在 Python 3 中工作:(1) 第一行:python2.7 => python3,(2) 删除导入:不导入 StringIO,(3) 添加连字符: print(doc), (4) 重命名函数:将 raw_input(...) 改为 input(...)
    • 介意给这个脚本添加许可证吗?
    【解决方案2】:

    你可以试试snakevizhttps://jiffyclub.github.io/snakeviz

    它是一个基于浏览器的图形查看器,用于 Python 的 cProfile 模块的输出,是使用标准库 pstats 模块的替代方案。

    # to install it with pip
    pip install snakeviz
    
    # once installed, you can use snakeviz to view the file
    snakeviz /path/to/your/dump/pstat/file
    

    这是一个可视化 pstat 文件的示例图像,您可以像上面一样转储。

    【讨论】:

      【解决方案3】:

      如果您事先将pstats 文件转换为pyprof2calltree,则可以使用kcachegrind

      $ pyprof2calltree -i /path/to/your/dump/pstat/file -o /tmp/converted_stats
      $ kcachegrind /tmp/converted_stats
      

      如果您使用的是基于 Debian 的系统,您可以通过运行以下命令轻松安装这两个工具:

      $ sudo apt install pyprof2calltree kcachegrind
      

      另见相关帖子:Using cProfile results with KCacheGrind

      【讨论】:

        猜你喜欢
        • 2023-02-20
        • 2016-01-08
        • 2012-11-29
        • 1970-01-01
        • 2016-07-04
        • 1970-01-01
        • 1970-01-01
        • 2021-10-27
        • 1970-01-01
        相关资源
        最近更新 更多