-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPerfTimer.py
297 lines (240 loc) · 9.34 KB
/
PerfTimer.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import time, types, os, re
# import timeit
from collections import defaultdict
from superglobals import *
from underscoretest import _
perf_debug = getglobal('perf_debug', False)
perf_timer_enabled = False
_file = os.path.abspath(__file__)
def refresh_perftimer():
execfile(_file)
def perf_timed(*args, **kwargs):
"""
@perf_timed
def func():
do_stuff()
"""
if perf_timer_enabled:
def decorate(func):
# for k in kwargs: setattr(func, k, kwargs[k])
# setattr(func, '__static_vars__', kwargs)
# pph(inspect.stack())
stack = inspect.stack()
outer_name = stack[1].function
if outer_name != '<module>':
prefix = outer_name
else:
prefix = ".".join(_.initial(os.path.basename(stack[1].filename).split('.')))
name = "{}.{}".format(prefix, func.__name__)
setglobal("stack", inspect.stack())
# print("[perftimer] binding as {}".format(name))
return PerfTimer.bind(func, name)
else:
def decorate(func):
return func
return decorate
class PerfTimer(object):
# _start_times = getglobal('_perftimer._start_times', type, dict, _set=True)
# _stop_times = getglobal('_perftimer._stop_times', type, dict, _set=True)
subtract = False
_counts = getglobal('_perftimer._count', defaultdict(list), defaultdict, _set=True)
_depth = []
# @property
# def start_times(self):
# return type(self)._start_times
# @start_times.setter
# def start_times(self,val):
# type(self)._start_times = val
# @property
# def stop_times(self):
# return type(self)._stop_times
# @stop_times.setter
# def stop_times(self,val):
# type(self)._stop_times = val
@property
def counts(self):
return type(self)._counts
@counts.setter
def counts(self,val):
type(self)._counts = val
@property
def depth(self):
return type(self)._depth
@depth.setter
def depth(self,val):
type(self)._depth = val
def __init__(self, name):
# self.timer = timeit.default_timer
self.timer = time.time_ns
self.name = name
self.start_times = []
self.stop_times = []
self.parent = self.depth[-1] if self.depth else None
def start(self):
# dprint("[start] self.name")
if debug: PerfTimer._name = self.name
self.start_times.append(self.timer())
return self
def stop(self):
self.stop_times.append(self.timer())
_zipped = zip(self.stop_times, self.start_times)
self.counts[self.name].append(
sum([e - s for e, s in _zipped])
)
return self
def pause(self):
if self.subtract:
self.stop_times.append(self.timer())
return self
def resume(self):
if self.subtract:
self.start_times.append(self.timer())
return self
def __enter__(self):
if not perf_timer_enabled:
return self
if self.parent:
self.parent.pause()
self.depth.append(self)
self.start()
return self
def __exit__(self, exc_type, exc_value, traceback):
if not perf_timer_enabled:
return
self.stop()
if self.parent:
self.parent.resume()
assert self.depth.pop() == self
@classmethod
def clear(cls):
cls._counts.clear()
cls._depth.clear()
@classmethod
def avg(cls, match='', count=40):
results = []
total_time = 0
for name, counts in cls._counts.items():
if not match or re.match(match, name):
length = len(counts)
if length:
total = sum(counts) / (10 ** 6)
total_time += total
avg = total / length
results.append((avg, total, length, "{:60} {:14,.2f} {:14,.3f} {:11}".format(name, total, avg, length)))
group_results = []
if results:
group_results.append("---------------------------------------[BY AVG]-----------------------------------------------")
for result in _.head(_.reverse(_.sortBy(results, lambda v, *a: v[0])), count >> 1):
group_results.append(result[-1])
group_results.append("--------------------------------------[BY TOTAL]----------------------------------------------")
for result in _.head(_.reverse(_.sortBy(results, lambda v, *a: v[1])), count >> 1):
group_results.append(result[-1])
# group_results.append("--------------------------------------[BY COUNT]----------------------------------------------")
# for result in _.head(_.reverse(_.sortBy(results, lambda v, *a: v[2])), count >> 1):
# group_results.append(result[-1])
group_results.append("--------------------------------------[TOTAL TIME]--------------------------------------------")
group_results.append("{:.2f} seconds".format(total_time / (10 ** 3)))
# print("\n".join(_.uniq(group_results)))
print("\n".join((group_results)))
# print("{}: {}ms total".format(name, sum(cls.counts[name])))
# print("")
# for name, counts in cls.counts.items():
# time += sum(counts)
# print("{}: {}ms".format("all", time))
@classmethod
def bind(cls, func, name=None):
"""
bind a function (or list of functions) to PerfTimer, return
bound versions
"""
if not perf_timer_enabled:
return func
if isinstance(func, list):
return [cls.bind(x, name=name) for x in func]
if func.__name__ == 'bound':
return func
def bound(*args, **kwargs):
with PerfTimer(func.__name__ if name is None else name):
return func(*args, **kwargs)
for k, v in getattr(func, '__static_vars__', {}).items():
setattr(bound, k, v)
return bound
@classmethod
def bindmethods(cls, instance, pick=None, omit=None):
"""
binds class (or an instance of a class) methods to PerfTimer
"""
if not perf_timer_enabled:
return
methods = [name for name in dir(instance)
if not name.startswith('_')
and isinstance(getattr(instance, name), types.MethodType)
and getattr(instance, name).__name__ != 'bound'
]
if pick:
methods = _.pick(methods, *pick)
if omit:
methods = _.omit(methods, *omit)
# except AttributeError:
# dprint("[call_everything] methods")
# printi("[bindmethods] methods:{}".format(methods))
instance_name = getattr(instance, '__name__') if hasattr(instance, '__name__') else getattr(instance, '__class__').__name__
# dprint("[bindmethods] instance_name")
if perf_debug: print("[bindmethods] instance_name:{}".format(instance_name))
for name in methods:
method = getattr(instance, name)
if perf_debug: print("[bindmethods] {}.{}".format(instance_name, name))
setattr(instance, name, cls.bind(method, "{}.{}".format(instance_name, name)))
@classmethod
def binditems(cls, instance, funcs, name=''):
"""
binds items in a dict-like object (e.g. locals()) to PerfTimer
"""
if not perf_timer_enabled:
return
for k, v in instance.items():
try:
if v in funcs:
if isinstance(v, types.FunctionType):
if v.__name__ != 'bound':
# dprint("[binditems] v.__name__")
if perf_debug: print("[binditems] {}.{}".format(name, k))
instance[k] = cls.bind(v, "{}.{}".format(name, k))
except AttributeError as e:
# dprint("[binditems] k, v")
print("[binditems] AttributeError: k:{}, v:{}".format(k, v))
raise
@classmethod
def bindglobalfunctions(cls, *names, pick=None, omit=None):
if not perf_timer_enabled:
return
names = _.flatten(names)
methods = _.filter([getglobal(name, None) for name in names if not name.startswith('_') and isinstance(getglobal(name, None), types.FunctionType)])
if pick:
methods = _.pick(methods, *pick)
if omit:
methods = _.omit(methods, *omit)
# except AttributeError:
# dprint("[call_everything] methods")
# printi("[bindmethods] methods:{}".format(methods))
for name in methods:
setglobal(name, cls.bind(getglobal(name), name))
class perf_timer_test_class(object):
@perf_timed()
def perftimer_test():
for r in range(8 * 8 * 8): pass
print("done")
def __init__():
pass
def perftimer_outer_test():
@perf_timed()
@static_vars(test=2)
def perftimer_test_inner():
for r in range(8 * 8 * 8): pass
print("done -{}".format(perftimer_test_inner.test))
perftimer_test_inner()
@perf_timed()
@static_vars(test=2)
def perftimer_test():
for r in range(8 * 8 * 8): pass
print("done -{}".format(perftimer_test_inner.test))