-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.py
182 lines (144 loc) · 4.57 KB
/
stats.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# simple statistics module
import resource
def _systemtime ():
ru_self = resource.getrusage (resource.RUSAGE_SELF)
ru_ch = resource.getrusage (resource.RUSAGE_CHILDREN)
return ru_self.ru_utime + ru_ch.ru_utime
class Stopwatch:
""" A stop watch """
def __init__ (self):
self._started = 0
self._finished = -1
self._elapsed = 0
self.start ()
@property
def elapsed (self):
""" Returns time (in seconds) since the stopwatch has been started. """
if self._finished < self._started:
return self._elapsed + (_systemtime () - self._started)
return self._elapsed + (self._finished - self._started)
def start (self):
""" Starts or resumes the stopwatch """
# collect elapsed time so far
if self._finished >= self._started:
self._elapsed += (self._finished - self._started)
self._started = _systemtime ()
self._finished = -1
def stop (self):
""" Stops the stopwatch """
if self._finished < self._started:
self._finished = _systemtime ()
def reset (self):
""" Resets the stopwatch by erasing all elapsed time """
self._elapsed = 0
self._finished = -1
self.start ()
def __str__ (self):
""" Reports time in seconds up to two decimal places """
return "{0:.2f}".format (self.elapsed)
def lap (name):
class ContextManager (object):
def __init__ (self, name):
self._name = name
self._sw = Stopwatch ()
def __enter__ (self):
self._sw.reset ()
return None
def __exit__ (self, exc_type, exc_value, traceback):
print 'DONE', name, 'in', str(self._sw)
return False
return ContextManager (name)
_statistics = dict()
def get (key):
""" Gets a value from statistics table """
return _statistics.get (key)
def put (key, v):
""" Puts a value in statistics table """
_statistics[key] = v
def start (key):
""" Starts (or resumes) a named stopwatch """
sw = get (key)
if sw is None:
sw = Stopwatch ()
put (key, sw)
return sw
else:
sw.start ()
def stop (key):
""" Stops a named stopwatch """
sw = get (key)
if sw is not None: sw.stop ()
def count (key):
""" Increments a named counter """
c = get (key)
if c is None: put (key, 1)
else: put (key, c + 1)
def brunch_print ():
""" Prints the result in brunch format """
print '----------------------------------------------------------------------'
for k in sorted (_statistics.keys ()):
print 'BRUNCH_STAT {name} {value}'.format (name=k, value=_statistics [k])
print '----------------------------------------------------------------------'
def timer (key):
""" ContextManager to help measuring time.
with timer('myname') as t:
do_code_that_is_timed
"""
class TimerContextManager (object):
def __init__ (self, key):
self._key = key
def __enter__ (self):
start (key)
return None
def __exit__ (self, exc_type, exc_value, traceback):
stop (key)
return False
return TimerContextManager (key)
def block(mark):
class BlockMarkerContextManager (object):
def __init__ (self, mark):
self._mark = mark
def __enter__ (self):
print 'BEGIN:', mark
return None
def __exit__ (self, exc_type, exc_value, traceback):
print 'END:', mark
return False
return BlockMarkerContextManager (mark)
def count_stats (f):
def counted_func (*args, **kwds):
count (f.__module__ + "." + f.__name__ + ".cnt")
return f(*args, **kwds)
counted_func.__name__ = f.__name__
counted_func.__doc__ = f.__doc__
return counted_func
def time_stats (f):
""" Function decorator to time a function
@time_stats
def foo (): pass
"""
def timed_func (*args, **kwds):
count (f.__module__ + "." + f.__name__ + ".cnt")
with timer (f.__module__ + '.' + f.__name__):
return f(*args, **kwds)
timed_func.__name__ = f.__name__
timed_func.__doc__ = f.__doc__
return timed_func
@time_stats
def _test_function ():
c = 0
while c < 100000000: c += 1
if __name__ == '__main__':
import time
c= 0
count ('tick')
with timer ('timer') as t:
while c < 10000000: c += 1
brunch_print ()
c = 0
count ('tick')
with timer ('timer') as t:
while c < 10000000: c += 1
brunch_print ()
_test_function ()
brunch_print ()