#!/usr/bin/python
|
# -*- coding: GBK -*-
|
#-------------------------------------------------------------------------------
|
#
|
#-------------------------------------------------------------------------------
|
#
|
|
#¹¦ÄÜͳ¼Æ
|
#²»¼ÓËø£¬²»Ö§³Ö¶àÏß³Ì
|
|
import timeit
|
|
class FuncGrade:
|
def __init__(self, name):
|
self.__name = name
|
self.__Count = 0
|
self.__totalCostTime = 0.0
|
self.__lastCostTime = 0.0
|
self.__timer = timeit.default_timer #¸ù¾Ýϵͳƽ̨ѡÔñµÄ×î¼Ñ¼ÆÊ±Æ÷
|
self.__funcCallStartTime = 0.0
|
|
def Start(self):
|
self.__funcCallStartTime = self.__timer()
|
|
def End(self):
|
self.__Count += 1
|
self.__lastCostTime = self.__timer() - self.__funcCallStartTime
|
self.__totalCostTime += self.__lastCostTime
|
|
def GetTotalCalls(self):
|
return self.__Count
|
|
def GetTotalCostTime(self):
|
return self.__totalCostTime
|
|
def GetLastCostTime(self):
|
return self.__lastCostTime
|
|
def GetAvgCostTime(self):
|
if self.__Count == 0:
|
return 0
|
return self.__totalCostTime / self.__Count
|
|
class FuncGradeManager:
|
def __init__(self, name):
|
self.__name = name
|
self.__funcGradeDict = {}
|
|
def GetFuncGrade(self, name):
|
if not self.__funcGradeDict.has_key(name):
|
self.__funcGradeDict[name] = FuncGrade(name)
|
return self.__funcGradeDict[name]
|
|
def GetFmtGradeInfo(self):
|
newLine = "\r\n"
|
fmtStr = "%20s\t%20s\t%10s\t%20s\t%20s\t%20s%s"
|
fmtGradeInfo = fmtStr%("ManagerName", "Name", "Calls", "TotalCostTime(secs)", "AvgCostTime(secs)",
|
"LastCostTime(secs)", newLine)
|
for k,v in self.__funcGradeDict.items():
|
fmtGradeInfo += fmtStr%(self.__name, k, v.GetTotalCalls(), v.GetTotalCostTime(), v.GetAvgCostTime(),
|
v.GetLastCostTime(), newLine)
|
return fmtGradeInfo
|
|
#__TestManager = FuncGradeManager("Test")
|
#from time import *
|
#
|
#def Func1():
|
# global __TestManager
|
#
|
# funcGrade = __TestManager.GetFuncGrade('Func1')
|
# funcGrade.Start()
|
#
|
# for i in range(10):
|
# print 'a'
|
# sleep(0.1)
|
#
|
# funcGrade.End()
|
#
|
#def Func2():
|
# global __TestManager
|
#
|
# funcGrade = __TestManager.GetFuncGrade('Func2')
|
# funcGrade.Start()
|
#
|
# for i in range(10):
|
# print 'b'
|
# sleep(0.001)
|
#
|
# funcGrade.End()
|
#
|
#def main():
|
# global __TestManager
|
#
|
# for i in range(10):
|
# Func1()
|
#
|
# for i in range(10):
|
# Func2()
|
#
|
# print __TestManager.GetFmtGradeInfo()
|
#
|
#if __name__ == "__main__":
|
# main()
|