def _get_all_yappi_stats(): res = [] def handle(data): avg = data[2]/data[1] stat = YappiStat(*(data+(avg,))) res.append(stat) yappi.enum_stats(handle) return res
def dump_cpu_usage(): """ This is a function that prints the memory usage of w3af in real time. :author: Andres Riancho ([email protected]) """ if not DEBUG_CPU_USAGE: return else: # Where data is stored, entries look like: # ('/home/dz0/workspace/w3af/plugins/infrastructure/afd.py.get_options:183', # 1, 3.1150000000000002e-06, 2.4320000000000002e-06) entries = [] # Load the data into entries yappi.enum_stats(entries.append) # Order it, put the function that takes more time in the first # position so we can understand why it is consuming so much time def sort_tsub(a, b): return cmp(a[2], b[2]) entries.sort(sort_tsub) entries.reverse() # Print the information in an "easy to read" way pp = pprint.PrettyPrinter(indent=4) data = pp.pformat(entries[:TOP_N_FUNCTIONS]) om.out.debug("CPU usage information:\n" + data) # Filtered information example filter_string = "sqli.py" filtered = [x for x in entries if filter_string in x[0]] data = pp.pformat(filtered) fmt = 'CPU usage for "%s":\n%s' om.out.debug(fmt % (filter_string, data))
def dump_cpu_usage(): """ This is a function that prints the memory usage of w3af in real time. :author: Andres Riancho ([email protected]) """ if not DEBUG_CPU_USAGE: return else: # Where data is stored, entries look like: # ('/home/dz0/workspace/w3af/plugins/infrastructure/afd.py.get_options:183', # 1, 3.1150000000000002e-06, 2.4320000000000002e-06) entries = [] # Load the data into entries yappi.enum_stats(entries.append) # Order it, put the function that takes more time in the first # position so we can understand why it is consuming so much time def sort_tsub(a, b): return cmp(a[2], b[2]) entries.sort(sort_tsub) entries.reverse() # Print the information in an "easy to read" way pp = pprint.PrettyPrinter(indent=4) data = pp.pformat(entries[:TOP_N_FUNCTIONS]) om.out.debug('CPU usage information:\n' + data) # Filtered information example filter_string = 'sqli.py' filtered = [x for x in entries if filter_string in x[0]] data = pp.pformat(filtered) fmt = 'CPU usage for "%s":\n%s' om.out.debug(fmt % (filter_string, data))
def enum_stats(self, fenum): return yappi.enum_stats(fenum)
g.stop += 1 foo2() foo() def averylongfunctionname_will_be_just_like_this(): pass def estat(entry): print '%s %i %0.7f %0.7f' % (entry[0], entry[1], entry[2], entry[3]) cProfile.run('foo()', 'fooprof') import pstats p = pstats.Stats('fooprof') p.strip_dirs().sort_stats(-1).print_stats() yappi.start(True) averylongfunctionname_will_be_just_like_this() g.stop = 0 foo() print "name callcnt #ttot #tsub" print "----------------------------------------------" yappi.enum_stats(estat) averylongfunctionname_will_be_just_like_this() yappi.print_stats() yappi.stop() yappi.clear_stats()
import yappi import cProfile def a_very_long_function_name(): pass def state(entry): print entry def foo(): time.sleep(1) cProfile.run('foo()', 'fooprof') import pstats p = pstats.Stats('fooprof') p.strip_dirs().sort_stats(-1).print_stats() yappi.start(True) foo() a_very_long_function_name() yappi.stop() yappi.enum_stats(state) li = yappi.get_stats(1, 1, 8) for it in li: print it yappi.clear_stats()
foo3() def foo(): if g.stop == 140: return g.stop += 1 foo2() foo() def averylongfunctionname_will_be_just_like_this(): pass def estat(entry): print '%s %i %0.7f %0.7f' % (entry[0], entry[1], entry[2], entry[3]) cProfile.run('foo()', 'fooprof') import pstats p = pstats.Stats('fooprof') p.strip_dirs().sort_stats(-1).print_stats() yappi.start(True) averylongfunctionname_will_be_just_like_this() g.stop = 0 foo() print "name callcnt #ttot #tsub" print "----------------------------------------------" yappi.enum_stats(estat) averylongfunctionname_will_be_just_like_this() yappi.print_stats() yappi.stop() yappi.clear_stats()
import yappi def es(entry): print "%.20s, %ld, %0.4f, %0.4f" % (entry[0], entry[1], entry[2], entry[3]) def foo(): pass yappi.start() foo() yappi.enum_stats(es) yappi.print_stats()