log and figure out what bits are slow and optimize them.

sajuptpm sajuptpm at gmail.com
Sat Feb 11 02:53:42 EST 2012


I decided to create a decorator like.

import cProfile
def debug_time(method):
    def timed(*args, **kw):
        prof = cProfile.Profile()
        prof.enable(subcalls=False, builtins=False)
        result = prof.runcall(method, *args, **kw)
        #prof.print_stats()
        msg = "\n\n\n\n#######################################"
        msg += "\n\nURL : %s" %(tg.request.url)
        msg += "\nMethod: %r" %(method.__name__)
        print "--ddd--------", type(prof.getstats())
        msg += "\n\nStatus : %s" %(prof.print_stats())
        msg += "\n\n#######################################"
        print msg
        LOGGER.info(msg)
        return result
    return timed

Ref : http://stackoverflow.com/questions/5375624/a-decorator-that-profiles-a-method-call-and-logs-the-profiling-result


I want to log it in existing log file in my project,
so i tried prof.print_stats() and prof.getstats(). prof.getstats()
will need extra loop for fetch data.
prof.print_stats() will log library calls also.

Please suggest a better way to log profiler output.





More information about the Python-list mailing list