-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIPPerfMonitor.py
61 lines (41 loc) · 1.8 KB
/
IPPerfMonitor.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
# coding: utf-8
"""
This code is part of the course 'Introduction to robot path planning' (Author: Bjoern Hein). It is based on the slides given during the course, so please **read the information in theses slides first**
License is based on Creative Commons: Attribution-NonCommercial 4.0 International (CC BY-NC 4.0) (pls. check: http://creativecommons.org/licenses/by-nc/4.0/)
"""
import pandas
import time
class IPPerfMonitor(object):
"Decorator that keeps track of the number of times a function is called."
__instances = {}
def __init__(self, f):
self.__f = f
self.data = []
IPPerfMonitor.__instances[f] = self
def __call__(self, *args, **kwargs):
starttime = time.time()
ret = self.__f(*args, **kwargs)
endtime = time.time()
element = {'args': args, 'kwargs': kwargs, "retVal": ret, "time": endtime-starttime}
self.data.append(element)
return ret
def _showargs(self, *fargs, **kw):
print("T: enter {} with args={}, kw={}".format(self.__f.__name__, str(fargs), str(kw)))
def __get__(self, instance, owner):
from functools import partial
return partial(self.__call__, instance)
@staticmethod
def dataFrame():
"Return a dict of {function: # of calls} for all registered functions."
result = []
for f in IPPerfMonitor.__instances:
for dataElement in IPPerfMonitor.__instances[f].data:
context = dict({"name": f.__name__})
context.update(dataElement)
result.append(context)
return pandas.DataFrame.from_dict(result)
@staticmethod
def clearData():
"Clear data"
for f in IPPerfMonitor.__instances:
del IPPerfMonitor.__instances[f].data[:]