File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ import time
2+ import logging
3+ import tracemalloc
4+ from functools import wraps
5+
6+
7+ def track_performance (func ):
8+ """
9+ Decorator to track runtime and memory usage of functions.
10+ """
11+
12+ @wraps (func )
13+ def wrapper (* args , ** kwargs ):
14+
15+ logging .info (f"Starting: { func .__name__ } " )
16+
17+ # Start timing
18+ start_time = time .time ()
19+
20+ # Start memory tracking
21+ tracemalloc .start ()
22+
23+ result = func (* args , ** kwargs )
24+
25+ current , peak = tracemalloc .get_traced_memory ()
26+ tracemalloc .stop ()
27+
28+ elapsed = time .time () - start_time
29+
30+ logging .info (
31+ f"Completed: { func .__name__ } | "
32+ f"Runtime: { elapsed :.2f} s | "
33+ f"Peak Memory: { peak / 10 ** 6 :.2f} MB"
34+ )
35+
36+ return result
37+
38+ return wrapper
You can’t perform that action at this time.
0 commit comments