Start monitoring the Memory usage and CPU utilization of all the processes of the given application at specific time interval and log them to the python logger.
If I want the resource usage of all processes related to gedit to be logged every 2 seconds, the following statements need to be incorporated in the test script
import os
import logging
import datetime
import logging.handlers
from ldtp import *
class LdtpCustomLog(logging.Handler):
"""
Custom LDTP log, inherit logging.Handler and implement
required API
"""
def __init__(self, name, logfilename):
# Call base handler
logging.Handler.__init__(self)
self.name = name
self.fp = open(os.path.expanduser(logfilename), 'w')
def __del__(self):
if self.fp:
self.fp.close()
def emit(self, record):
# Get the message and add to the list
# Later the list element can be poped out
if re.match('MEMINFO', record.levelname, re.I):
level = 'Memory'
elif re.match('CPUINFO', record.levelname, re.I):
level = 'CPU'
else:
level = ''
message = re.split(' - ', record.getMessage(), 1)
if self.fp:
self.fp.write('%s,%s,%s,%s\n' %(datetime.datetime.now().isoformat(), level,
message[1], self.name))
# Add LdtpCustomLog handler
logging.handlers.LdtpCustomLog = LdtpCustomLog
# Create instance of LdtpCustomLog handler
_custom_logger = logging.handlers.LdtpCustomLog('geditTesting', '~/tmp/actual.log')
# Set default log level as LDTP_LOG_MEMINFO
_custom_logger.setLevel(LDTP_LOG_MEMINFO)
addloghandler(_custom_logger)
os.system('mkdir -p ~/tmp')
startlog('~/tmp/test.log', False)
startprocessmonitor('gedit')
_custom_logger.name = 'launchapp'
launchapp('gedit')
if not waittillguiexist('*-gedit'):
_custom_logger.name = 'launchfailed'
raise AssertionError('Gedit window does not appear')
_custom_logger.name = 'settext'
settextvalue('*-gedit', 'txt1', 'ICOS 2010, Taiwan')
wait(1)
_custom_logger.name = 'gettext'
print gettextvalue('*-gedit', 'txt1')
wait(1)
_custom_logger.name = 'newtab'
selectmenuitem('*-gedit', 'mnuFile;mnuNew')
wait(1)
click('*-gedit', 'btnOpen')
_custom_logger.name = 'dialog'
if not waittillguiexist('dlgOpenFiles'):
raise AssertionError('Open Files window does not appear')
if not verifytoggled('dlgOpenFiles', 'tbtnTypeafilename'):
click('dlgOpenFiles', 'tbtnTypeafilename')
wait(1)
settextvalue('dlgOpenFiles', 'txtLocation', '/tmp/test.py')
click('dlgOpenFiles', 'btnOpen')
if not waittillguinotexist('dlgOpenFiles'):
raise AssertionError('Open Files window still exist !')
selectmenuitem('*-gedit', 'mnuFile;mnuQuit')
waittillguiexist('Question')
click('Question', 'Close without Saving')
waittillguinotexist('Question')
waittillguinotexist('*-gedit')
_custom_logger.name = 'exit'
stopprocessmonitor('gedit')
stoplog()
removeloghandler(_custom_logger)