我基本上做到了以下几点:
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分析此文件以供人类使用?
提前谢谢。
发布于 2015-04-14 23:56:20
这是我发现的结果和我生成的Python程序。我用一个在linux上制作并在windows xp上分析的.dmp文件对此进行了测试。它工作得很好。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()发布于 2019-04-17 16:18:39
您可以尝试snakeviz https://jiffyclub.github.io/snakeviz
它是一个基于浏览器的图形查看器,用于Python模块的输出,也是使用标准库cProfile模块的替代方法。
# 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文件的示例图像,您可以像上面那样转储。

发布于 2021-08-18 09:15:25
如果事先使用kcachegrind转换pstats文件,则可以使用pyprof2calltree:
$ pyprof2calltree -i /path/to/your/dump/pstat/file -o /tmp/converted_stats
$ kcachegrind /tmp/converted_stats

如果您使用的是基于Debian的系统,则可以通过运行以下命令轻松地安装这两个工具:
$ sudo apt install pyprof2calltree kcachegrindhttps://stackoverflow.com/questions/29630667
复制相似问题