Skip to content

Commit 0027d90

Browse files
besobeso
authored andcommitted
Add Memory Usage Logging Functionality
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.
1 parent c693c97 commit 0027d90

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import psutil
2+
import time
3+
4+
def memory_usage_logger(func):
5+
"""
6+
A decorator to log memory usage before and after a function call.
7+
8+
:param func: The function to be logged.
9+
:return: The result of the function call.
10+
"""
11+
def wrapper(*args, **kwargs):
12+
# Log memory usage before function call
13+
process = psutil.Process(os.getpid())
14+
memory_before = process.memory_info().rss
15+
16+
# Call the function
17+
result = func(*args, **kwargs)
18+
19+
# Log memory usage after function call
20+
memory_after = process.memory_info().rss
21+
22+
# Calculate memory usage difference
23+
memory_usage_diff = memory_after - memory_before
24+
25+
# Log memory usage difference
26+
print(f"Memory usage difference for {func.__name__}: {memory_usage_diff} bytes")
27+
28+
return result
29+
30+
return wrapper
31+
32+
@memory_usage_logger
33+
def function_to_log(arg1, arg2):
34+
"""
35+
An example function to be logged for memory usage.
36+
37+
:param arg1: The first argument.
38+
:param arg2: The second argument.
39+
:return: None
40+
"""
41+
time.sleep(1) # Simulate function execution time
42+
print(f"Function {function_to_log.__name__} executed with arguments: {arg1}, {arg2}")
43+
44+
if __name__ == "__main__":
45+
function_to_log("argument1", "argument2")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import psutil
2+
import time
3+
4+
def memory_usage_logger(func):
5+
"""
6+
Logs memory usage before and after calling the provided function.
7+
8+
:param func: The function to be logged.
9+
:return: The result of the provided function.
10+
"""
11+
def wrapper(*args, **kwargs):
12+
# Log memory usage before calling the function
13+
process = psutil.Process(os.getpid())
14+
memory_before = process.memory_info().rss / (1024 * 1024)
15+
16+
# Call the function
17+
result = func(*args, **kwargs)
18+
19+
# Log memory usage after calling the function
20+
memory_after = process.memory_info().rss / (1024 * 1024)
21+
print(f"Memory usage before: {memory_before:.2f} MB")
22+
print(f"Memory usage after: {memory_after:.2f} MB")
23+
24+
return result
25+
26+
return wrapper

0 commit comments

Comments
 (0)