Perf Monitoring - mikejl/research GitHub Wiki

Code Wrapper for monitoring specific parts of the code


# perf wrapper start (i)pr where i=function #
bpr = cProfile.Profile()
bpr.enable()  #start

#< .. code to monitor here ..> #

bpr.disable() #stop
boolcount = db.booleans.find().count()
s = StringIO.StringIO()
sortby = 'calls'  
ps = pstats.Stats(bpr, stream=s).sort_stats(sortby).strip_dirs()
ps.print_stats()
bfpPerfs = s.getvalue()

bfpPerfs1 = bfpPerfs.lstrip()
perfline = bfpPerfs1.splitlines()
function_name = sys._getframe().f_code.co_name
outFileName = system+"-"+function_name+"-"+"test"+testnum+".csv"
with open(outFileName, "wb") as f:
writer = csv.writer(f, delimiter=',', quotechar='|')
for line in perfline:
    linepart = line.split()
    writer.writerow(linepart)    
# raw file
outProfileName = system+"-"+function_name+"-"+"test"+testnum+".profile"
ps.dump_stats(outProfileName)
# Db	    
db = client.prefdata
print "Store cProfile results to perfdata dB?"
YN=raw_input("Y/N: ")
if YN == "Y":
    docinsert = {"Sys": system, "testnum": testnum, "boolscount": boolcount, "boolsfp": bfpPerfs, "date": datetime.datetime.utcnow()}
    print "Saving..."
    db.prefdata.insert(docinsert)
# perf wrapper end #