<s> import resource <EOL> from resource import RUSAGE_SELF , RUSAGE_CHILDREN <EOL> import psutil <EOL> from nose . plugins . base import Plugin <EOL> import sys , os <EOL> debug = sys . stderr <EOL> class MemProfile ( Plugin ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> def options ( self , parser , env ) : <EOL> """<STR_LIT>""" <EOL> super ( MemProfile , self ) . options ( parser , env ) <EOL> def configure ( self , options , config ) : <EOL> """<STR_LIT>""" <EOL> super ( MemProfile , self ) . configure ( options , config ) <EOL> def begin ( self ) : <EOL> self . _profiled_tests = [ ] <EOL> self . b_vm = None <EOL> self . b_sru_maxrss = <NUM_LIT:0> <EOL> self . b_cru_maxrss = <NUM_LIT:0> <EOL> self . proc = psutil . Process ( os . getpid ( ) ) <EOL> self . children = "<STR_LIT>" <EOL> def startTest ( self , test ) : <EOL> self . b_vm = psutil . virtual_memory ( ) <EOL> self . b_sru_maxrss = resource . getrusage ( RUSAGE_SELF ) . ru_maxrss <EOL> self . b_cru_maxrss = resource . getrusage ( RUSAGE_CHILDREN ) . ru_maxrss <EOL> debug . write ( "<STR_LIT>" % ( self . b_sru_maxrss , self . b_cru_maxrss , str ( self . b_vm ) ) ) <EOL> def afterTest ( self , test ) : <EOL> a_vm = psutil . virtual_memory ( ) <EOL> a_sru_maxrss = resource . getrusage ( RUSAGE_SELF ) . ru_maxrss <EOL> a_cru_maxrss = resource . getrusage ( RUSAGE_CHILDREN ) . ru_maxrss <EOL> children = self . proc . get_children ( ) <EOL> num_children = len ( children ) <EOL> child_names = [ child . name for child in children ] <EOL> self . children = "<STR_LIT>" % ( num_children , '<STR_LIT:U+002CU+0020>' . join ( child_names ) ) <EOL> self . _profiled_tests . append ( ( test . id ( ) , self . b_vm , self . b_sru_maxrss , <EOL> self . b_cru_maxrss , a_vm , a_sru_maxrss , a_cru_maxrss , self . children ) ) <EOL> debug . write ( "<STR_LIT>" % ( a_sru_maxrss , a_cru_maxrss , str ( a_vm ) ) ) <EOL> debug . write ( "<STR_LIT>" % self . children ) <EOL> def report ( self , stream ) : <EOL> table = [ ] <EOL> for test , b_vm , b_sru_maxrss , b_cru_maxrss , a_vm , a_sru_maxrss , a_cru_maxrss , children in self . _profiled_tests : <EOL> ptid = "<STR_LIT:.>" . join ( test . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:2> : ] ) <EOL> table . append ( [ ptid , "<STR_LIT>" ] ) <EOL> table . append ( [ "<STR_LIT>" , "<STR_LIT>" % ( b_sru_maxrss , b_cru_maxrss ) ] ) <EOL> table . append ( [ "<STR_LIT>" , "<STR_LIT:%s>" % str ( b_vm ) ] ) <EOL> table . append ( [ "<STR_LIT>" , "<STR_LIT>" % ( a_sru_maxrss , a_cru_maxrss ) ] ) <EOL> table . append ( [ "<STR_LIT>" , "<STR_LIT:%s>" % str ( a_vm ) ] ) <EOL> table . append ( [ "<STR_LIT>" , "<STR_LIT>" % ( str ( a_sru_maxrss - b_sru_maxrss ) , <EOL> str ( a_cru_maxrss - b_cru_maxrss ) ) ] ) <EOL> table . append ( [ "<STR_LIT>" , "<STR_LIT:%s>" % children ] ) <EOL> table . append ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> table . insert ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> widths = [ max ( [ len ( row [ x ] ) for row in table ] ) for x in xrange ( len ( table [ <NUM_LIT:0> ] ) ) ] <EOL> fmt_out = [ "<STR_LIT:U+0020>" . join ( [ x . ljust ( widths [ i ] ) for i , x in enumerate ( row ) ] ) for row in table ] <EOL> fmt_out . insert ( <NUM_LIT:1> , "<STR_LIT:U+0020>" . join ( [ '<STR_LIT>' . ljust ( widths [ i ] , '<STR_LIT:=>' ) for i in xrange ( len ( widths ) ) ] ) ) <EOL> stream . write ( "<STR_LIT>" ) <EOL> stream . write ( "<STR_LIT:\n>" . join ( fmt_out ) ) <EOL> stream . write ( "<STR_LIT:\n>" ) </s>
