2015-04-14 18 views
5

tôi đã cơ bản thực hiện như sau:Làm cách nào để phân tích tệp được tạo bằng pstats.dump_stats (tên tệp) ngoài dòng?

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 

Vì vậy, bây giờ tôi có tệp có tên 'stats.dmp' lưu trữ ngoại tuyến.

Làm cách nào tôi có thể sử dụng pstats để phân tích tệp này để sử dụng cho con người?

Xin cảm ơn trước.

Trả lời

8

Đây là những gì tôi đã tìm thấy và chương trình Python mà tôi đã tạo. Tôi đã thử nghiệm điều này với một tập tin .dmp được thực hiện trên linux & phân tích trên windows xp. Nó đã làm việc FINE. Tệp Python được đặt tên là "analysis_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 
""" 
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() 
+0

Đây là một tập lệnh đơn giản tuyệt vời cho phép các nhóm chia sẻ các bãi hồ sơ dễ dàng. Cảm ơn! – zerocog

Các vấn đề liên quan