Skip to content

Commit db71411

Browse files
authored
Add performance tracking decorator for functions
This decorator tracks the execution time and memory usage of a function, printing the results after each call.
1 parent c088870 commit db71411

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import time
2+
import tracemalloc
3+
4+
def performance(fn):
5+
"""
6+
Decorator to measure performance metrics of a function.
7+
8+
This decorator tracks the execution time and memory usage of the decorated
9+
function, printing the results after each call.
10+
11+
:param fn: The function to be decorated.
12+
:returns: A wrapped function that tracks performance metrics.
13+
:raises Exception: If an error occurs during the execution of the decorated function.
14+
"""
15+
16+
setattr(performance, 'counter', 0)
17+
setattr(performance, 'total_time', 0.0)
18+
setattr(performance, 'total_mem', 0.0)
19+
20+
def _performance(*args, **kwargs):
21+
"""
22+
Wrapper function that measures execution time and memory usage.
23+
24+
:param args: Positional arguments to be passed to the decorated function.
25+
:param kwargs: Keyword arguments to be passed to the decorated function.
26+
"""
27+
counter = getattr(performance, 'counter')
28+
total_time = getattr(performance, 'total_time')
29+
total_mem = getattr(performance, 'total_mem')
30+
31+
counter += 1
32+
setattr(performance, 'counter', counter)
33+
34+
tracemalloc.start()
35+
start_time = time.time()
36+
37+
try:
38+
fn(*args, **kwargs)
39+
except Exception as e:
40+
print(f"Error occurred: {e}")
41+
finally:
42+
end_time = time.time()
43+
current, peak = tracemalloc.get_traced_memory()
44+
tracemalloc.stop()
45+
46+
elapsed_time = end_time - start_time
47+
memory_usage = peak
48+
49+
total_time += elapsed_time
50+
total_mem += memory_usage
51+
52+
setattr(performance, 'total_time', total_time)
53+
setattr(performance, 'total_mem', total_mem)
54+
55+
print_results(fn.__name__, elapsed_time, memory_usage, counter, total_time, total_mem)
56+
57+
return _performance
58+
59+
def print_results(func_name, elapsed_time, memory_usage, counter, total_time, total_mem):
60+
"""
61+
Display the performance results of the decorated function.
62+
63+
This function prints the name of the function being measured, the elapsed time,
64+
memory usage, and the total statistics over all calls.
65+
66+
:param func_name: The name of the function being measured.
67+
:param elapsed_time: The time taken for the function to execute, in seconds.
68+
:param memory_usage: The peak memory usage during the function execution, in bytes.
69+
:param counter: Number of times the decorated function has been called.
70+
:param total_time: Total execution time of the decorated function.
71+
:param total_mem: Total peak memory usage of the decorated function.
72+
"""
73+
results = (
74+
f"Function Name: {func_name}\n"
75+
f"Number of Calls: {counter}\n"
76+
f"Elapsed Time: {elapsed_time:.6f} seconds\n"
77+
f"Memory Usage: {memory_usage / 1024:.2f} KB\n"
78+
f"Total Time: {total_time:.6f} seconds\n"
79+
f"Total Memory Usage: {total_mem / 1024:.2f} KB\n"
80+
)
81+
print(results)

0 commit comments

Comments
 (0)