Skip to content

Commit

Permalink
feat(2024): add CPU and Memory usage profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
rpidanny committed Dec 10, 2024
1 parent 7e91077 commit c3e76ae
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion 2024/Day00/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solutions import part1, part2

from utils.inputs import get_inputs
from utils.timings import profile_run
from utils.profiling import profile_run

if __name__ == "__main__":
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt"
Expand Down
2 changes: 1 addition & 1 deletion 2024/Day01/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solutions import part1, part2

from utils.inputs import get_inputs
from utils.timings import profile_run
from utils.profiling import profile_run

if __name__ == "__main__":
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt"
Expand Down
2 changes: 1 addition & 1 deletion 2024/Day02/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solutions import part1, part2

from utils.inputs import get_inputs
from utils.timings import profile_run
from utils.profiling import profile_run

if __name__ == "__main__":
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt"
Expand Down
2 changes: 1 addition & 1 deletion 2024/Day03/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solutions import part1, part2

from utils.inputs import get_inputs
from utils.timings import profile_run
from utils.profiling import profile_run

if __name__ == "__main__":
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt"
Expand Down
2 changes: 1 addition & 1 deletion 2024/Day04/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solutions import part1, part2

from utils.inputs import get_inputs
from utils.timings import profile_run
from utils.profiling import profile_run

if __name__ == "__main__":
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt"
Expand Down
2 changes: 1 addition & 1 deletion 2024/Day05/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solutions import part1, part2

from utils.inputs import get_inputs
from utils.timings import profile_run
from utils.profiling import profile_run

if __name__ == "__main__":
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt"
Expand Down
2 changes: 1 addition & 1 deletion 2024/Day06/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solutions import part1, part2

from utils.inputs import get_inputs
from utils.timings import profile_run
from utils.profiling import profile_run

if __name__ == "__main__":
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt"
Expand Down
2 changes: 1 addition & 1 deletion 2024/Day07/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solutions import part1, part2

from utils.inputs import get_inputs
from utils.timings import profile_run
from utils.profiling import profile_run

if __name__ == "__main__":
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt"
Expand Down
2 changes: 1 addition & 1 deletion 2024/Day08/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solutions import part1, part2

from utils.inputs import get_inputs
from utils.timings import profile_run
from utils.profiling import profile_run

if __name__ == "__main__":
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt"
Expand Down
2 changes: 1 addition & 1 deletion 2024/Day09/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solutions import part1, part2

from utils.inputs import get_inputs
from utils.timings import profile_run
from utils.profiling import profile_run

if __name__ == "__main__":
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt"
Expand Down
2 changes: 1 addition & 1 deletion 2024/Day10/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solutions import part1, part2

from utils.inputs import get_inputs
from utils.timings import profile_run
from utils.profiling import profile_run

if __name__ == "__main__":
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt"
Expand Down
15 changes: 14 additions & 1 deletion 2024/utils/timings.py → 2024/utils/profiling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from time import perf_counter
from typing import Callable, Tuple, TypeVar

import psutil
from termcolor import colored

T = TypeVar("T")
Expand All @@ -18,13 +20,24 @@ def format_duration(milliseconds: float) -> str:


def profile_run(name: str, func: Callable[[], T]) -> Tuple[T, float]:
process = psutil.Process(os.getpid()) # Get the current process
start_cpu = process.cpu_percent(interval=None) # Initial CPU usage
start_memory = process.memory_info().rss / (1024 * 1024) # Initial memory in MB

start = perf_counter()
result = func()
end = perf_counter()

end_cpu = process.cpu_percent(interval=None) # Final CPU usage
end_memory = process.memory_info().rss / (1024 * 1024) # Final memory in MB

duration = (end - start) * 1000
cpu_usage = end_cpu - start_cpu # CPU usage percentage
memory_change = end_memory - start_memory # Memory change in MB

print(
f"{colored(name, 'magenta')}: {colored(result, 'green')} {colored(f'(executed in {format_duration(duration)})', 'light_grey', attrs=['dark'])}"
f"{colored(name, 'magenta')}: {colored(result, 'green')} "
f"{colored(f'(Time: {format_duration(duration)}, Memory: {memory_change:.2f}MB, CPU: {cpu_usage:.2f}%)', 'light_grey', attrs=['dark'])}"
)

return result, duration

0 comments on commit c3e76ae

Please sign in to comment.