Python用プロファイラPyXdebugを作った

PHPXdebugみたいな感じ。
PyPIに登録した。
今のところ、1.2.2が最新。
 
pyxdebug 1.2.2 : Python Package Index
http://pypi.python.org/pypi/pyxdebug
 

インストール

# easy_install pyxdebug

 

メソッド実行をプロファイルする

example_call_func.py
#!/usr/bin/python
# set encoding=utf-8
#
# usage: python example_call_func.py

from pyxdebug import PyXdebug

class Fib(object):
    def __init__(self):
        pass

    def calc(self, n):
        if n<3:
            return 1
        return self.calc(n - 1) + self.calc(n - 2)

xd = PyXdebug()
#xd.collect_imports = 1
#xd.collect_params = 0
#xd.collect_return = 0
#xd.collect_assignments = 0

fib = Fib()
result = xd.run_func(fib.calc, 6)

print "result: %d\n" % result
print xd.get_result()

 

実行結果
$ python example_call_func.py
result: 8

TRACE START [2011-11-20 14:29:44]
    0.0005       1886   -> __main__.Fib.calc() /Volumes/DATA/Users/tetu/work/pyxdebug/pyxdebug.py:133
    0.0008       1887     -> __main__.Fib.calc() example_call_func.py:15
    0.0010       1887       -> __main__.Fib.calc() example_call_func.py:15
    0.0012       1887         -> __main__.Fib.calc() example_call_func.py:15
    0.0015       1888           -> __main__.Fib.calc() example_call_func.py:15
    0.0018       1888           -> __main__.Fib.calc() example_call_func.py:15
    0.0024       1889         -> __main__.Fib.calc() example_call_func.py:15
    0.0029       1889       -> __main__.Fib.calc() example_call_func.py:15
    0.0031       1890         -> __main__.Fib.calc() example_call_func.py:15
    0.0035       1890         -> __main__.Fib.calc() example_call_func.py:15
    0.0039       1891     -> __main__.Fib.calc() example_call_func.py:15
    0.0041       1891       -> __main__.Fib.calc() example_call_func.py:15
    0.0044       1891         -> __main__.Fib.calc() example_call_func.py:15
    0.0048       1892         -> __main__.Fib.calc() example_call_func.py:15
    0.0052       1892       -> __main__.Fib.calc() example_call_func.py:15
    0.0057       1893
TRACE END   [2011-11-20 14:29:44]

 

スクリプト実行をプロファイルする

example_run_file.py
#!/usr/bin/python
# set encoding=utf-8
#
# usage: python -m pyxdebug example_run_file.py

class Fib(object):
    def __init__(self):
        pass

    def calc(self, n):
        if n<3:
            return 1
        return self.calc(n - 1) + self.calc(n - 2)

fib = Fib()
result = fib.calc(6)

print "result: %d\n" % result

 

実行結果
$ python -m pyxdebug  example_run_file.py
result: 8

TRACE START [2011-11-20 14:30:51]
    0.0011       2135   -> Fib() example_run_file.py:18
    0.0015       2136   -> __main__.Fib.__init__() example_run_file.py:18
    0.0018       2137   -> __main__.Fib.calc() example_run_file.py:18
    0.0020       2137     -> __main__.Fib.calc() example_run_file.py:13
    0.0022       2137       -> __main__.Fib.calc() example_run_file.py:13
    0.0025       2138         -> __main__.Fib.calc() example_run_file.py:13
    0.0027       2138           -> __main__.Fib.calc() example_run_file.py:13
    0.0030       2138           -> __main__.Fib.calc() example_run_file.py:13
    0.0034       2138         -> __main__.Fib.calc() example_run_file.py:13
    0.0037       2139       -> __main__.Fib.calc() example_run_file.py:13
    0.0040       2139         -> __main__.Fib.calc() example_run_file.py:13
    0.0043       2140         -> __main__.Fib.calc() example_run_file.py:13
    0.0047       2140     -> __main__.Fib.calc() example_run_file.py:13
    0.0050       2140       -> __main__.Fib.calc() example_run_file.py:13
    0.0052       2141         -> __main__.Fib.calc() example_run_file.py:13
    0.0055       2141         -> __main__.Fib.calc() example_run_file.py:13
    0.0059       2142       -> __main__.Fib.calc() example_run_file.py:13
    0.0064       2142
TRACE END   [2011-11-20 14:30:51]

 

Usage

Usage: pyxdebug.py [-o output_file_path] [-i collect_import] [-p collect_params] [-r collect_return] [-a collect_assignments] script_path [args ...]

Options:
  -h, --help            show this help message and exit
  -o, --outfile         Save stats to <outfile>
  -i, --collect_imports
                        This setting, defaulting to 1, controls whether
                        PyXdebug should write the filename used in import or
                        reload to the trace files.
  -p, --collect_params  This setting, defaulting to 0, controls whether
                        PyXdebug should collect the parameters passed to
                        functions when a function call is recorded in either
                        the function trace or the stack trace.
  -r, --collect_return  This setting, defaulting to 0, controls whether
                        PyXdebug should write the return value of function
                        calls to the trace files.
  -a, --collect_assignments
                        This setting, defaulting to 0, controls whether
                        PyXdebug should add variable assignments to function
                        traces.