3/2/2018 - 7:09 PM

this takes the resource library and uses it to give you perspective on whats changing between different parts of the library

heres the table flr resource.getrusage()

Index	Field	Resource
0	ru_utime	time in user mode (float)
1	ru_stime	time in system mode (float)
2	ru_maxrss	maximum resident set size
3	ru_ixrss	shared memory size
4	ru_idrss	unshared memory size
5	ru_isrss	unshared stack size
6	ru_minflt	page faults not requiring I/O
7	ru_majflt	page faults requiring I/O
8	ru_nswap	number of swap outs
9	ru_inblock	block input operations
10	ru_oublock	block output operations
11	ru_msgsnd	messages sent
12	ru_msgrcv	messages received
13	ru_nsignals	signals received
14	ru_nvcsw	voluntary context switches
15	ru_nivcsw	involuntary context switches

from resource import getrusage
from functools import partial
from generators import started

def usage(_usage=partial(getrusage, 0)):
    '''returns a dict of the current system usage'''
    r = _usage()
    return {a:getattr(r,a) for a in dir(r) if a.startswith('ru_')}

def subtract_dicts(a,b):
    ''' this returns the difference between all similar keys of two dictionaries '''
    return {k:a[k]-b[k] for k in a if k in b}

def windowed_usage():
    ''' this generator yields back the system usage
        between each time you iterate on it '''
    # I know the dels are a mess, but this specific
    # order seems to the optimal way to set things up
    # to minimize variability between measurements.
    # To reduce the opportunity for gc to kick in and
    # mess up the timing, refs are deleted as soon as
    # they're not needed anymore. It might seem a
    # little redundent but as I added dels, the
    # measurements actually did level out.
    while 1:
        # get the current system usage
        # diff that usage and the previous
        del previous_usage
        # measure the usage used to get that diff
        # diff the measuring usage with the first measurement
        del usage_now
        del this_usage
        # subtract how much usage measuring costs
        del this_diff
        # set previous to the usage after all this measuring
        # send back the result
        yield usage_diff
        del usage_diff


from pprint import pprint

pprint(subtract_dicts(b, a))
for i in range(5):