Repository navigation
Expand file tree
/
Copy pathexecution_timer.py
More file actions
160 lines (117 loc) · 4.46 KB
/
Copy pathexecution_timer.py
File metadata and controls
160 lines (117 loc) · 4.46 KB
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
import time
class ExecutionTimer:
"""
ExecutionTimer class
Debug your code by logging it's performance
Initialize at start of script
> ExecutionTimer(<path_to_logfile.csv>, {<delimiter>})
Start timer
> ExecutionTimer.start(<action>[, <category>])
End timer
> ExecutionTimer.end(<action>[, <comment>])
End of script
> ExecutionTimer.complete()
The result of this class is a CSV file with rows for each action
"""
class __ExecutionTimer:
action_stack = {}
delimiter = ','
log_file = None
exec_start = None
file_hander = None
def __init__(self, log_file, delimiter=','):
self.log_file = log_file
self.delimiter = delimiter
def __str__(self):
return repr(self) + self.log_file
# Write a line at the beginning of file
def start(self):
self.exec_start = int(round(time.time() * 1000))
line = self.delimiter.join(['"Start execution"', '', '0', '0', ''])
self.writeLog(line)
# Write a line at the end of the file with complete exec time
def complete(self):
exec_end = int(round(time.time() * 1000)) - self.exec_start
# Write a line at the end of tile?
line = self.delimiter.join(['"End execution"', '', '0', str(exec_end), ''])
self.writeLog(line)
# terminate file_hander.
self.file_hander.close()
def addAction(self, action, category):
if action in self.action_stack:
# An action by this name is already ongoing.
# End it, so that we can safely start a new one.
# This is technically an error by the user, but we could call it a feature
self.endAction(action)
self.action_stack[action] = {
'action': action,
'category': category if category else '',
'time': int(round(time.time() * 1000)),
'exec_time': 0,
'comment': ''
}
def endAction(self, action, comment):
if not action in self.action_stack:
return False
data = self.action_stack[action]
self.action_stack.pop(action, None)
# Get end time
data['time'] = int(round(time.time() * 1000)) - data['time']
data['exec_time'] = int(round(time.time() * 1000)) - self.exec_start
data['comment'] = comment
# Sort fields
line = self.delimiter.join(str(val) for val in data.values())
self.writeLog(line)
def writeLog(self, line):
# Write data to log
if not self.file_hander:
# create file handler
self.file_hander = open(self.log_file, "w+")
self.file_hander.write(line +"\n")
return True
"""
In the root class we instantiate the private subclass.
We also expose a few methods to the ouside code.
"""
instance = None
def __init__(self, log_file=None, delimiter=',', is_enabled = True):
if not is_enabled:
ExecutionTimer.instance = None
return False
if not ExecutionTimer.instance:
ExecutionTimer.instance = ExecutionTimer.__ExecutionTimer(log_file, delimiter)
ExecutionTimer.instance.start()
def __getattr__(self, name):
return getattr(self.instance, name)
def start(action, category=None):
"""
Start timeing an action
Parameters
----------
action: string
Name of action to be timed
category: string [optinal]
Category of the action
"""
if ExecutionTimer.instance:
ExecutionTimer.instance.addAction(action, category)
def end(action, comment=''):
"""
End the timer for an action
Parameters
----------
action: string
Name of an action that was previously started
comment: string [optinal]
Add a free format comment column to log file
"""
if ExecutionTimer.instance:
ExecutionTimer.instance.endAction(action, comment)
def complete():
"""
End the execution timer.
Adds a row to the end of the log with the completion time
of the whole process.
"""
if ExecutionTimer.instance:
ExecutionTimer.instance.complete()