From 09b185027f706073a6c09da3b1921417f8617f3c Mon Sep 17 00:00:00 2001 From: ezberaysegul <105494369+ezberaysegul@users.noreply.github.com> Date: Mon, 18 Nov 2024 19:40:42 +0300 Subject: [PATCH 1/2] Create decorators_aysegul_ezber.py --- Week04/decorators_aysegul_ezber.py | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Week04/decorators_aysegul_ezber.py diff --git a/Week04/decorators_aysegul_ezber.py b/Week04/decorators_aysegul_ezber.py new file mode 100644 index 00000000..9a9769ad --- /dev/null +++ b/Week04/decorators_aysegul_ezber.py @@ -0,0 +1,44 @@ +import time +import tracemalloc + +def track_performance(func): + """ + A decorator that tracks performance metrics of a function, such as + the number of invocations, total execution time, and peak memory usage. + + Attributes: + - call_count: Tracks how many times the decorated function is called. + - total_duration: Accumulates the total execution time for all calls. + - peak_memory: Accumulates the peak memory usage across all calls. + """ + setattr(track_performance, 'call_count', 0) + setattr(track_performance, 'total_duration', 0.0) + setattr(track_performance, 'peak_memory', 0.0) + + def wrapper(*args, **kwargs): + memory_tracker.start() # Start memory tracking + start_time = time.time() # Record the start time + result = func(*args, **kwargs) # Execute the original function + end_time = time.time() # Record the end time + current, peak = memory_tracker.get_traced_memory() # Get memory usage data + memory_tracker.stop() # Stop memory tracking + + # Update performance metrics + setattr(track_performance, 'call_count', getattr(track_performance, 'call_count') + 1) + setattr(track_performance, 'total_duration', getattr(track_performance, 'total_duration') + (end_time - start_time)) + setattr(track_performance, 'peak_memory', getattr(track_performance, 'peak_memory') + peak) + + return result + + return wrapper + +# Example usage +@track_performance +def sample_function(n): + """ + A simple function that calculates the sum of the first `n` integers. + """ + total = sum(range(n)) + time.sleep(0.5) # Simulate a delay + return total + From f248f2e1232506377186b69b09867e066743748d Mon Sep 17 00:00:00 2001 From: ezberaysegul <105494369+ezberaysegul@users.noreply.github.com> Date: Mon, 18 Nov 2024 19:43:13 +0300 Subject: [PATCH 2/2] Create decorators_aysegul_ezber.py --- Week04/decorators_aysegul_ezber.py | 60 ++++++++++-------------------- 1 file changed, 20 insertions(+), 40 deletions(-) diff --git a/Week04/decorators_aysegul_ezber.py b/Week04/decorators_aysegul_ezber.py index 9a9769ad..aa5dfe6d 100644 --- a/Week04/decorators_aysegul_ezber.py +++ b/Week04/decorators_aysegul_ezber.py @@ -1,44 +1,24 @@ import time import tracemalloc -def track_performance(func): - """ - A decorator that tracks performance metrics of a function, such as - the number of invocations, total execution time, and peak memory usage. - - Attributes: - - call_count: Tracks how many times the decorated function is called. - - total_duration: Accumulates the total execution time for all calls. - - peak_memory: Accumulates the peak memory usage across all calls. - """ - setattr(track_performance, 'call_count', 0) - setattr(track_performance, 'total_duration', 0.0) - setattr(track_performance, 'peak_memory', 0.0) - - def wrapper(*args, **kwargs): - memory_tracker.start() # Start memory tracking - start_time = time.time() # Record the start time - result = func(*args, **kwargs) # Execute the original function - end_time = time.time() # Record the end time - current, peak = memory_tracker.get_traced_memory() # Get memory usage data - memory_tracker.stop() # Stop memory tracking - - # Update performance metrics - setattr(track_performance, 'call_count', getattr(track_performance, 'call_count') + 1) - setattr(track_performance, 'total_duration', getattr(track_performance, 'total_duration') + (end_time - start_time)) - setattr(track_performance, 'peak_memory', getattr(track_performance, 'peak_memory') + peak) - +def performance(func): + + setattr(performance, 'counter', 0) + setattr(performance, 'total_time', 0.0) + setattr(performance, 'total_mem', 0.0) + + def inner(*args, **kwargs): + tracemalloc.start() + start_time = time.time() + result = func(*args, **kwargs) + end_time = time.time() + current, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + setattr(performance, 'counter', getattr(performance, 'counter') + 1) + setattr(performance, 'total_time', getattr(performance, 'total_time') + (end_time - start_time)) + setattr(performance, 'total_mem', getattr(performance, 'total_mem') + peak) + return result - - return wrapper - -# Example usage -@track_performance -def sample_function(n): - """ - A simple function that calculates the sum of the first `n` integers. - """ - total = sum(range(n)) - time.sleep(0.5) # Simulate a delay - return total - + + return inner