Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions scripts/memory_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
import subprocess
import sys
import tempfile
from collections.abc import Mapping
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Self
from typing import Any, Final, Self

# Known projects with their Git URLs for memory testing.
KNOWN_PROJECTS: dict[str, str] = {
KNOWN_PROJECTS: Final[Mapping[str, str]] = {
"flake8": "https://github.com/PyCQA/flake8",
"sphinx": "https://github.com/sphinx-doc/sphinx",
"prefect": "https://github.com/PrefectHQ/prefect",
Expand Down Expand Up @@ -103,7 +104,7 @@ def format_diff(*, old_bytes: int, new_bytes: int) -> str:
if old_bytes == 0:
return f"{sign}{format_bytes(diff)} (new)"

return f"{sign}{diff / old_bytes:.1%} ({format_bytes(abs(diff))})"
return f"{sign}{diff / old_bytes:.2%} ({format_bytes(abs(diff))})"


def format_outcome(*, old_bytes: int, new_bytes: int) -> str:
Expand All @@ -114,7 +115,7 @@ def format_outcome(*, old_bytes: int, new_bytes: int) -> str:
elif diff < 0:
return "⬇️"
else:
return "☑️"
return ""


def load_reports_from_directory(directory: Path) -> dict[str, MemoryReport]:
Expand Down Expand Up @@ -155,7 +156,7 @@ def diff_items(


# Maximum number of changed items to show per category in the detailed breakdown
MAX_CHANGED_ITEMS = 15
MAX_CHANGED_ITEMS: Final = 15


def render_summary(projects: list[ProjectComparison]) -> str:
Expand All @@ -165,8 +166,9 @@ def render_summary(projects: list[ProjectComparison]) -> str:

projects.sort(key=lambda p: p.total_diff_bytes, reverse=True)

any_increased = any(p.total_diff_bytes > 0 for p in projects)
any_decreased = any(p.total_diff_bytes < 0 for p in projects)
# Suppress the memory report if no project had any top-line changes >10KB
any_increased = any(p.total_diff_bytes > 10_000 for p in projects)
any_decreased = any(p.total_diff_bytes < -10_000 for p in projects)
any_changed = any_increased or any_decreased

lines = ["## Memory usage report", ""]
Expand Down Expand Up @@ -246,7 +248,7 @@ def render_summary(projects: list[ProjectComparison]) -> str:
]
)
else:
lines.append("Memory usage unchanged :white_check_mark:")
lines.append("Memory usage unchanged ")

return "\n".join(lines)

Expand Down