如何离线分析使用pstats.dump_stats(filename)创建的文件? [英] How can I analyze a file created with pstats.dump_stats(filename) off line?

查看:207
本文介绍了如何离线分析使用pstats.dump_stats(filename)创建的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上已经完成了以下工作:

I have essentially done the following:

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"的文件离线存储了.

So now i have the file named 'stats.dmp' stored offline.

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

How can i use pstats to analyze this file for human consumption?

谢谢.

推荐答案

这是我发现的内容以及生成的Python程序.我用linux&上的.dmp文件进行了测试.在Windows XP上进行了分析.它很好. Python文件名为"analyze_dmp.py".

Here's what i found out and the Python program I generated. I tested this with a .dmp file made on linux & analyzed on windows xp. It worked FINE. The Python file is named, "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()

这篇关于如何离线分析使用pstats.dump_stats(filename)创建的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆