[LDTP-Dev] Patch to capture memory and cpu stats
Subodh Soni
ssoni at novell.com
Wed Sep 28 00:12:10 PDT 2005
Hi All,
I am sending a patch for ldtputils.py that implements the memory and CPU
utilization statistics capturing. It uses the opensource library
pystatgrab (http://www.i-scream.org/pystatgrab/) as its compatible with
multiple unices on which ldtp development is being carried out (namely
FreeBSD and Solaris)
Thanks & Regards
Subodh
-------------- next part --------------
Index: ldtputils.py
===================================================================
RCS file: /cvs/pyldtp/ldtputils.py,v
retrieving revision 1.24
diff -u -p -r1.24 ldtputils.py
--- ldtputils.py 26 Sep 2005 22:13:10 -0000 1.24
+++ ldtputils.py 28 Sep 2005 14:35:36 -0000
@@ -29,6 +29,10 @@ try:
import ImageChops, Image
except ImportError:
ldtp.log ('Python-Imaging package not installed', 'error')
+try:
+ from statgrab import *
+except ImportError:
+ ldtp.log ('libstatgrab package not installed', 'error')
def tuplelist2list (lst):
d = []
@@ -254,3 +258,34 @@ class LdtpDataFileParser:
for data in dataelements.getElementsByTagName (tagname):
self.taglist.append (self.getText (data.childNodes))
return self.taglist
+
+# Capturing Memory and CPU Utilization statistics for an application and its related processes
+# EXAMPLE USAGE:
+# xstats = pstats('evolution', 2)
+# Start Logging by calling start
+# xstats.start()
+# Stop the process statistics gathering thread by calling the stopstats method
+# xstats.stopstats()
+
+class pstats(threading.Thread):
+ def __init__ (self, appname, inter):
+ threading.Thread.__init__(self)
+ self.processname = appname
+ self.interval = inter
+ self.stop = 0
+ def run (self):
+ while (self.stop == 0):
+ for i in sg_get_process_stats():
+ result = re.search (self.processname, str(i['process_name']))
+ if (result):
+ title = str(i['proctitle'])
+ proctitle= re.split("\s", title)
+ procname = re.split("\/", proctitle[0])
+ # Put the stats into ldtp log file
+ ldtp.log (procname[-1] + '-' + str (i['proc_resident']/(1024*1024)) + 'M', 'meminfo')
+ ldtp.log (procname[-1] + '-' + str (round(i['cpu_percent'], 2)), 'cpuinfo')
+ # Wait for interval seconds before gathering stats again
+ time.sleep (self.interval)
+
+ def stopstats(self):
+ self.stop = 1
More information about the Ldtp-dev
mailing list