Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve size tracking table #3117

Merged
merged 11 commits into from
Aug 29, 2023
8 changes: 7 additions & 1 deletion .github/workflows/reusable_track_size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ jobs:

echo "$data" > "/tmp/data.json"

comparison=$(python3 scripts/ci/sizes.py compare --threshold=5 "/tmp/prev.json" "/tmp/data.json")
comparison=$(
python3 scripts/ci/sizes.py compare \
--threshold=5 \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that sizes.py --help explains the unit of threshold, but that helps very little when reading this file 🤷‍♂️
Not a blocker, just trying to explain my reasoning a bit further.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but I also think that 0.05 is as ambigous as 5 if there's no unit :/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe - but 0.05 is less likely to be of unit Bytes at least :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--threshold-percent would be very clear in any case

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do crimes instead:

comparison=$(
  python3 scripts/ci/sizes.py compare \
    --threshold=5% \
    "prev.json" "data.json"
)

The % is optional

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, now we're talking 😆

--before-header=${{ (inputs.PR_NUMBER && github.event.pull_request.base.ref) || 'Before' }} \
--after-header=${{ github.ref_name }} \
"/tmp/prev.json" "/tmp/data.json"
)
echo "$comparison"

{
Expand Down
61 changes: 41 additions & 20 deletions scripts/ci/sizes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ def get_divisor(unit: str) -> int:
return DIVISORS[unit]


def cell(value: float, div: float) -> str:
return f"{value / div:.2f}"


def render_table_dict(data: list[dict[str, str]]) -> str:
keys = data[0].keys()
column_widths = [max(len(key), max(len(str(row[key])) for row in data)) for key in keys]
Expand Down Expand Up @@ -95,7 +91,13 @@ def render(self, data: list[dict[str, str]]) -> str:
return render_table_dict(data)


def compare(previous_path: str, current_path: str, threshold: float) -> None:
def compare(
previous_path: str,
current_path: str,
threshold_pct: float,
before_header: str,
after_header: str,
) -> None:
previous = json.loads(Path(previous_path).read_text())
current = json.loads(Path(current_path).read_text())

Expand All @@ -109,28 +111,27 @@ def compare(previous_path: str, current_path: str, threshold: float) -> None:
entries[name] = {}
entries[name]["previous"] = entry

headers = ["Name", "Previous", "Current", "Change"]
headers = ["Name", before_header, after_header, "Change"]
rows: list[tuple[str, str, str, str]] = []
for name, entry in entries.items():
if "previous" in entry and "current" in entry:
previous = float(entry["previous"]["value"]) * DIVISORS[entry["previous"]["unit"]]
current = float(entry["current"]["value"]) * DIVISORS[entry["current"]["unit"]]

min_change = previous * (threshold / 100)

unit = get_unit(min(previous, current))
previous_bytes = float(entry["previous"]["value"]) * DIVISORS[entry["previous"]["unit"]]
current_bytes = float(entry["current"]["value"]) * DIVISORS[entry["current"]["unit"]]
unit = get_unit(min(previous_bytes, current_bytes))
div = get_divisor(unit)

change = ((previous / current) * 100) - 100
sign = "+" if change > 0 else ""

if abs(current - previous) >= min_change:
abs_diff_bytes = abs(current_bytes - previous_bytes)
min_diff_bytes = previous_bytes * (threshold_pct / 100)
if abs_diff_bytes >= min_diff_bytes:
previous = previous_bytes / div
current = current_bytes / div
change_pct = ((current_bytes - previous_bytes) / previous_bytes) * 100
rows.append(
(
name,
f"{cell(previous, div)} {unit}",
f"{cell(current, div)} {unit}",
f"{sign}{change:.2f}%",
f"{previous:.2f} {unit}",
f"{current:.2f} {unit}",
f"{change_pct:+.2f}%",
)
)
elif "current" in entry:
Expand Down Expand Up @@ -186,6 +187,20 @@ def main() -> None:
default=20,
help="Only print row if value is N%% larger or smaller",
)
compare_parser.add_argument(
"--before-header",
type=str,
required=False,
default="Before",
help="Header for before column",
)
compare_parser.add_argument(
"--after-header",
type=str,
required=False,
default="After",
help="Header for after column",
)

measure_parser = cmds_parser.add_parser("measure", help="Measure sizes")
measure_parser.add_argument(
Expand All @@ -200,7 +215,13 @@ def main() -> None:
args = parser.parse_args()

if args.cmd == "compare":
compare(args.before, args.after, args.threshold)
compare(
args.before,
args.after,
args.threshold,
args.before_header,
args.after_header,
)
elif args.cmd == "measure":
measure(args.files, args.format)

Expand Down