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}
@started
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.
previous_usage=usage()
while 1:
# get the current system usage
usage_now=usage()
# diff that usage and the previous
usage_diff=subtract_dicts(usage_now,previous_usage)
del previous_usage
# measure the usage used to get that diff
this_usage=usage()
# diff the measuring usage with the first measurement
this_diff=subtract_dicts(this_usage,usage_now)
del usage_now
del this_usage
# subtract how much usage measuring costs
usage_diff=subtract_dicts(usage_diff,this_diff)
del this_diff
# set previous to the usage after all this measuring
previous_usage=usage()
# send back the result
yield usage_diff
del usage_diff
g=windowed_usage()
from pprint import pprint
a=usage()
pprint(a)
b=usage()
pprint(b)
pprint(subtract_dicts(b, a))
for i in range(5):
print(next(g))