Skip to content

Commit 9ea6f9f

Browse files
besobeso
authored andcommitted
Add Memory Usage Logging Functionality
Implements koii-network#12905 Implements koii-network#12876 Implements koii-network#12871 Implements koii-network#12833 Implements koii-network#12816 Implements koii-network#12811 Implements koii-network#12791 Implements koii-network#12775 Implements koii-network#12711 Implements koii-network#12626 # Add Memory Usage Logging Functionality ## Task Write a function to log memory usage before and after a function call. ## Acceptance Criteria All tests must pass. ## Summary of Changes Implemented a new utility function to log memory usage before and after function calls. This will help in tracking and analyzing memory consumption of critical functions in the application. ## Test Cases - Verify the memory logging function captures memory usage correctly - Ensure memory logging does not significantly impact function performance - Check that memory logging works for different types of functions - Validate memory usage logging for functions with varying execution times This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai.
1 parent c693c97 commit 9ea6f9f

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import psutil
2+
import time
3+
4+
def memory_logger(func):
5+
"""
6+
A decorator to log memory usage before and after a function call.
7+
8+
Args:
9+
func: The function to decorate.
10+
11+
Returns:
12+
The decorated function with memory logging functionality.
13+
14+
"""
15+
def wrapper(*args, **kwargs):
16+
# Get the current memory usage
17+
process = psutil.Process(os.getpid())
18+
memory_before = process.memory_info().rss
19+
20+
# Call the function
21+
start_time = time.time()
22+
result = func(*args, **kwargs)
23+
end_time = time.time()
24+
25+
# Get the memory usage after the function call
26+
memory_after = process.memory_info().rss
27+
28+
# Calculate and log the memory difference
29+
memory_difference = memory_after - memory_before
30+
print(f"Function {func.__name__} memory usage: {memory_difference} bytes")
31+
32+
# Print the function execution time
33+
print(f"Function {func.__name__} execution time: {end_time - start_time:.4f} seconds")
34+
35+
return result
36+
37+
return wrapper
38+
39+
40+
if __name__ == "__main__":
41+
@memory_logger
42+
def test_function():
43+
time.sleep(1)
44+
45+
46+
@memory_logger
47+
def memory_intensive_function():
48+
large_list = [i for i in range(1000000)]
49+
50+
51+
test_function()
52+
memory_intensive_function()

prometheus/utils/memory_logger.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import psutil
2+
import time
3+
4+
def memory_logger(func):
5+
"""
6+
A decorator to log memory usage before and after a function call.
7+
8+
Args:
9+
func: The function to log memory usage for.
10+
11+
Returns:
12+
The function with memory logging added.
13+
14+
"""
15+
def wrapper(*args, **kwargs):
16+
# Get the current memory usage
17+
process = psutil.Process(os.getpid())
18+
memory_before = process.memory_info().rss
19+
20+
# Call the function
21+
start_time = time.time()
22+
result = func(*args, **kwargs)
23+
end_time = time.time()
24+
25+
# Get the memory usage after the function call
26+
memory_after = process.memory_info().rss
27+
28+
# Calculate the difference between the initial and final memory usage
29+
memory_used = memory_after - memory_before
30+
31+
# Log the memory usage
32+
print(f"Function {func.__name__} took {end_time - start_time:.6f} seconds "
33+
f"and used {memory_used:,} bytes of memory")
34+
35+
return result
36+
37+
return wrapper

0 commit comments

Comments
 (0)