Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Yappi for profiling

When I am using cProfiler, I get the following line:

 ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     39   12.486    0.320   12.486    0.320 {method 'acquire' of 'thread.lock' objects}

I understood that yappi is the way to go.

So I am writing:

yappi.get_func_stats().print_all()

and I get too many lines to read.

How can I retrieve only the 10 most ones that consume the most time?

Equivalent to:

p.sort_stats('time').print_stats(10)

I basically want to know what consumes the most amount of time.

I do run threads in my code with ThreadPoolExecutor

like image 885
Dejell Avatar asked Oct 30 '22 12:10

Dejell


1 Answers

You can only modify the sorting if you want to limit the result you'll have to modify the print_all method

For sorting stats

import sys
from yappi import get_func_stats, COLUMNS_FUNCSTATS, COLUMNS_THREADSTATS
# Stats sorted by total time
stats = get_func_stats.sort(
        sort_type='totaltime', sort_order='desc') 
# returns all stats with sorting applied 
print_all(stats, sys.stdout, limit=10)

Modified print

import os
def print_all(stats, out, limit=None):
    if stats.empty():
        return
    sizes = [36, 5, 8, 8, 8]
    columns = dict(zip(range(len(COLUMNS_FUNCSTATS)), zip(COLUMNS_FUNCSTATS, sizes)))
    show_stats = stats
    if limit:
        show_stats = stats[:limit]
    out.write(os.linesep)
    # write out the headers for the func_stats
    # write out stats with exclusions applied.
    # for stat in show_stats:
    #    stat._print(out, columns)  
like image 189
jackotonye Avatar answered Nov 15 '22 05:11

jackotonye