yappi / yappi.py

'''

 yappi.py
 Yet Another Python Profiler

 Sumer Cip 2010

'''
import sys
import threading
import _yappi

__all__ = ['start', 'stop', 'enum_stats', 'print_stats', 'clear_stats']

SORTTYPE_NAME = _yappi.SORTTYPE_NAME
SORTTYPE_NCALL = _yappi.SORTTYPE_NCALL
SORTTYPE_TTOTAL = _yappi.SORTTYPE_TTOTAL
SORTTYPE_TSUB = _yappi.SORTTYPE_TSUB
SORTTYPE_TAVG = _yappi.SORTTYPE_TAVG
SORTORDER_ASCENDING = _yappi.SORTORDER_ASCENDING
SORTORDER_DESCENDING = _yappi.SORTORDER_DESCENDING
SHOW_ALL = _yappi.SHOW_ALL

'''
 __callback will only be called once per-thread. _yappi will detect
 the new thread and changes the profilefunc param of the ThreadState
 structure. This is an internal function please don't mess with it.
'''
def __callback(frame, event, arg):
    _yappi.profile_event(frame, event, arg)
    return __callback

def start(builtins = False, timing_sample=1):
    '''
    Args:
    builtins: If set true, then builtin functions are profiled too.
    timing_sample: will cause the profiler to do timing measuresements
                   according to the value. Will increase profiler speed but
                   decrease accuracy.
    '''
    threading.setprofile(__callback)
    _yappi.start(builtins, timing_sample)

def stop():
    threading.setprofile(None)
    _yappi.stop()

def enum_stats(fenum):
    _yappi.enum_stats(fenum)

def get_stats(sorttype=_yappi.SORTTYPE_NCALL,
        sortorder=_yappi.SORTORDER_DESCENDING,
        limit=_yappi.SHOW_ALL):
    return _yappi.get_stats(sorttype, sortorder, limit)

def print_stats(sorttype=_yappi.SORTTYPE_NCALL,
        sortorder=_yappi.SORTORDER_DESCENDING,
        limit=_yappi.SHOW_ALL):
    li = get_stats(sorttype, sortorder, limit)
    for it in li: print(it)

def clear_stats():
    _yappi.clear_stats()

def main():
    import os, sys
    from optparse import OptionParser
    usage = "yappi.py [-b] [-s timing_sample_value] [scriptfile] args ..."
    parser = OptionParser(usage=usage)
    parser.allow_interspersed_args = False
    parser.add_option("-b", "--builtins",
                  action="store_true", dest="profile_builtins", default=False,
                  help="Profiles builtin functions when set. [default: False]")
    parser.add_option("-s", "--sample",
                  type="int", dest="timing_sample", default=1,
                  help="Profiles functions every sample call. [default: 1]")
    if not sys.argv[1:]:
        parser.print_usage()
        sys.exit(2)

    (options, args) = parser.parse_args()
    sys.argv[:] = args

    if (len(sys.argv) > 0):
        sys.path.insert(0, os.path.dirname(sys.argv[0]))
        start(options.profile_builtins, options.timing_sample)
        execfile(sys.argv[0])
        stop()
        print_stats() # TODO: accept params for this
    else:
        parser.print_usage()
    return parser

if __name__ == "__main__":
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.