diff --git a/prometheus/functions/function_file.py b/prometheus/functions/function_file.py new file mode 100644 index 0000000000..c673be4af3 --- /dev/null +++ b/prometheus/functions/function_file.py @@ -0,0 +1,52 @@ +import psutil +import time + +def memory_logger(func): + """ + A decorator to log memory usage before and after a function call. + + Args: + func: The function to decorate. + + Returns: + The decorated function with memory logging functionality. + + """ + def wrapper(*args, **kwargs): + # Get the current memory usage + process = psutil.Process(os.getpid()) + memory_before = process.memory_info().rss + + # Call the function + start_time = time.time() + result = func(*args, **kwargs) + end_time = time.time() + + # Get the memory usage after the function call + memory_after = process.memory_info().rss + + # Calculate and log the memory difference + memory_difference = memory_after - memory_before + print(f"Function {func.__name__} memory usage: {memory_difference} bytes") + + # Print the function execution time + print(f"Function {func.__name__} execution time: {end_time - start_time:.4f} seconds") + + return result + + return wrapper + + +if __name__ == "__main__": + @memory_logger + def test_function(): + time.sleep(1) + + + @memory_logger + def memory_intensive_function(): + large_list = [i for i in range(1000000)] + + + test_function() + memory_intensive_function() \ No newline at end of file diff --git a/prometheus/utils/memory_logger.py b/prometheus/utils/memory_logger.py new file mode 100644 index 0000000000..566bd7d0ec --- /dev/null +++ b/prometheus/utils/memory_logger.py @@ -0,0 +1,37 @@ +import psutil +import time + +def memory_logger(func): + """ + A decorator to log memory usage before and after a function call. + + Args: + func: The function to log memory usage for. + + Returns: + The function with memory logging added. + + """ + def wrapper(*args, **kwargs): + # Get the current memory usage + process = psutil.Process(os.getpid()) + memory_before = process.memory_info().rss + + # Call the function + start_time = time.time() + result = func(*args, **kwargs) + end_time = time.time() + + # Get the memory usage after the function call + memory_after = process.memory_info().rss + + # Calculate the difference between the initial and final memory usage + memory_used = memory_after - memory_before + + # Log the memory usage + print(f"Function {func.__name__} took {end_time - start_time:.6f} seconds " + f"and used {memory_used:,} bytes of memory") + + return result + + return wrapper \ No newline at end of file