Spaces:
Runtime error
Runtime error
add time tracker
Browse files
dashboard_utils/time_tracker.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from time import time
|
2 |
+
from functools import wraps
|
3 |
+
|
4 |
+
def simple_time_tracker(log_fun):
|
5 |
+
def _simple_time_tracker(fn):
|
6 |
+
@wraps(fn)
|
7 |
+
def wrapped_fn(*args, **kwargs):
|
8 |
+
start_time = time()
|
9 |
+
|
10 |
+
try:
|
11 |
+
result = fn(*args, **kwargs)
|
12 |
+
finally:
|
13 |
+
elapsed_time = time() - start_time
|
14 |
+
|
15 |
+
# log the result
|
16 |
+
log_fun({
|
17 |
+
'function_name': fn.__name__,
|
18 |
+
'total_time': elapsed_time,
|
19 |
+
})
|
20 |
+
|
21 |
+
return result
|
22 |
+
|
23 |
+
return wrapped_fn
|
24 |
+
return _simple_time_tracker
|
25 |
+
|
26 |
+
|
27 |
+
def _log(message):
|
28 |
+
print('[SimpleTimeTracker] {function_name} {total_time:.3f}'.format(**message))
|