1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/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()