#!c:\python20\python
# You'll need Python 2.0 to run os.popen3
# Script for timing command response.
# This can be used to time p4 commands or any OS command.
# Gerry Thompson Python Class Perforce Software 2004
# Version 1.0
import os, re, string, smtplib, sys
p4user = 'bruno' # Perforce user name to use.
p4client = 'bruno_ws' # client name
p4path = 'p4' # Path to p4 executable.
admin = 'admin@company.com' # 'From' address for email.
to = 'bruno@company.com' # Your email address.
mailhost = 'company.com' # SMTP mail host name.
p4 = p4path + ' -u '+ p4user + ' -c' + p4client
def sendmail(fromaddress, toaddress, message):
from smtplib import SMTP
fullmessage = 'From: '+ fromaddress + '\r\nTo: '+ toaddress +\
'\r\nSubject: ' + message
server = SMTP(mailhost) # These two lines can be made into one.
server.sendmail(fromaddress,toaddress, fullmessage)
server.quit()
def timecommand(command):
message = 'Here is how long it takes to run p4' + command + '\n' + '\n'
geterror = re.compile('^error: ')
for line in os.popen(p4 + ' info', 'r').readlines():
message = message + line
from time import localtime, strftime, time
start = strftime("%a, %d %b %Y %H:%M:%S ", localtime(time()))
message = message + 'Start time is: ' + start +'\n'
starttotalseconds = time()
for line in os.popen(p4 + command, 'r').readlines():
# This check adds some overhead to our process time.
if geterror.search(line):
print "There is an error running p4",command
sys.exit(1)
endtotalseconds = time()
end = strftime("%a, %d %b %Y %H:%M:%S ", localtime(time()))
finalseconds = endtotalseconds - starttotalseconds
print 'Start time: ', start
print 'End time: ', end
short = "This operation took: " + str(finalseconds) + ' seconds.'
print short
message = message + 'End time is: ' + end + '\n'
message = message + short
sendmail(admin, to, message)
def genericommand(command):
message = 'Here is how long it takes to run ' + command + '\n' + '\n'
from time import localtime, strftime, time
start = strftime("%a, %d %b %Y %H:%M:%S ", localtime(time()))
message = message + 'Start time is: ' + start +'\n'
starttotalseconds = time()
i,o,e = os.popen3(command, 'b')
error = e.read()
if error != '':
print 'Error running your command: ', error
sys.exit(1)
endtotalseconds = time()
end = strftime("%a, %d %b %Y %H:%M:%S ", localtime(time()))
finalseconds = endtotalseconds - starttotalseconds
print 'Start time: ', start
print 'End time: ', end
short = "This operation took: "+ str(finalseconds) + ' seconds.'
print short
message = message + 'End time is: ' + end + '\n'
message = message + short
sendmail(admin, to, message)
if __name__ == '__main__':
# portname = raw_input('Enter a port to test, such as company:20026 ')
# p4 = p4 + ' -p' + portname
choice = raw_input('Enter p4 if you wish to test a Perforce command. ')
if choice == 'p4':
addstring = ' -s '
command = raw_input('Enter a command to test, such as sync -f ')
command = addstring + command
timecommand(command)
else:
command = raw_input('Enter an OS command to test. ')
genericommand(command)