diff --git a/.github/workflows/post-size-cmp.yml b/.github/workflows/post-size-cmp.yml index 57abec4ad..2168e07d7 100644 --- a/.github/workflows/post-size-cmp.yml +++ b/.github/workflows/post-size-cmp.yml @@ -23,53 +23,11 @@ jobs: name: size-cmp-info path: "size-cmp-info/" + - name: Download Repository + uses: actions/checkout@v2 + - name: Make pull request comment - run: | - from typing import Dict, List, Optional - - import os - import json - - with open("size-cmp-info/.SIZE_CMP_INFO") as f: - content = json.loads(f.read()) - - joined_sizes = content["sizes"] - issue_number = content["issue_number"] - - lines: List[str] = [] - - lines.append("### Size Comparison") - lines.append("") - lines.append("| examples | master (KB) | pull request (KB) | diff |") - lines.append("| --- | --- | --- | --- | ") - - for (i, sizes) in joined_sizes: - (master_size, pr_size) = sizes - diff: Optional[int] = None - - if master_size is not None and pr_size is not None: - diff = pr_size - master_size - - master_size_str = "N/A" if master_size is None else ( - f"{master_size / 1024:.3f}" if master_size != 0 else "0" - ) - pr_size_str = "N/A" if pr_size is None else ( - f"{pr_size / 1024:.3f}" if pr_size != 0 else "0" - ) - diff_str = "N/A" if diff is None else ( - f"{diff / 1024:.3f}" if diff < 0 else ( - f"+{diff / 1024:.3f}" if diff > 0 else "0" - ) - ) - lines.append(f"| {i} | {master_size_str} | {pr_size_str} | {diff_str} |") - - output = "\n".join(lines) - - with open(os.environ["GITHUB_ENV"], "a+") as f: - f.write(f"YEW_EXAMPLE_SIZES={json.dumps(output)}\n") - f.write(f"PR_NUMBER={issue_number}\n") - - shell: python3 {0} + run: python3 ci/make_example_size_cmt.py - name: Post Comment uses: actions/github-script@v6 diff --git a/ci/collect_sizes.py b/ci/collect_sizes.py index 66a5126ce..71688b5b9 100644 --- a/ci/collect_sizes.py +++ b/ci/collect_sizes.py @@ -1,40 +1,52 @@ from typing import Dict, List, Optional +from pathlib import Path import glob import os import json -def find_example_sizes(parent_dir: str) -> Dict[str, int]: + +def find_example_sizes(parent_dir: Path) -> Dict[str, int]: example_sizes: Dict[str, int] = {} - for example_dir in os.listdir(f"{parent_dir}/examples"): - path = f"{parent_dir}/examples/{example_dir}" + for example_dir in (parent_dir / "examples").iterdir(): - if not os.path.isdir(path): + if not example_dir.is_dir(): + print(f"{example_dir} is not a directory.") continue - matches = glob.glob(f"{parent_dir}/examples/{example_dir}/dist/index*.wasm") + total_size = 0 - if not matches: - continue + # For examples with multiple bundles, we add them together. + for bundle in (example_dir / "dist").glob(f"*.wasm"): + size = bundle.stat().st_size - path = matches[0] + print(f"{bundle} has a size of {size}.") - example_sizes[example_dir] = os.path.getsize(path) + total_size += size + + if total_size > 0: + example_sizes[example_dir.name] = total_size return example_sizes -master_sizes = find_example_sizes("yew-master") -pr_sizes = find_example_sizes("current-pr") -example_names = sorted(set([*master_sizes.keys(), *pr_sizes.keys()])) +def main() -> None: + master_sizes = find_example_sizes(Path("yew-master")) + pr_sizes = find_example_sizes(Path("current-pr")) -joined_sizes = [(i, [master_sizes.get(i), pr_sizes.get(i)]) for i in example_names] + example_names = sorted(set([*master_sizes.keys(), *pr_sizes.keys()])) -size_cmp_info = { - "sizes": joined_sizes, - "issue_number": os.environ["ISSUE_NUMBER"], -} + joined_sizes = [(i, [master_sizes.get(i), pr_sizes.get(i)]) for i in example_names] -with open(".SIZE_CMP_INFO", "w+") as f: - f.write(json.dumps(size_cmp_info)) + size_cmp_info = { + "sizes": joined_sizes, + "issue_number": os.environ["ISSUE_NUMBER"], + } + + with open(".SIZE_CMP_INFO", "w+") as f: + f.write(json.dumps(size_cmp_info, indent=4)) + + +if __name__ == "__main__": + main() diff --git a/ci/make_example_size_cmt.py b/ci/make_example_size_cmt.py new file mode 100644 index 000000000..28be311ee --- /dev/null +++ b/ci/make_example_size_cmt.py @@ -0,0 +1,88 @@ +from typing import Dict, List, Optional, Tuple + +import os +import json + + +header = "| examples | master (KB) | pull request (KB) | diff |" +sep = "| --- | --- | --- | --- | " + + +def format_size(size: Optional[int]) -> str: + if size is None: + return "N/A" + + if size == 0: + return "0" + + return f"{size / 1024:.3f}" + + +def format_diff_size( + master_size: Optional[int], pr_size: Optional[int] +) -> Tuple[str, bool]: + if master_size is None or pr_size is None: + return ("N/A", False) + + diff = pr_size - master_size + + if diff == 0: + return ("0", False) + + diff_percent = diff / master_size + + return (f"{diff / 1024:+.3f}({diff_percent:+.3%})", abs(diff_percent) > 0.01) + + +def main() -> None: + with open("size-cmp-info/.SIZE_CMP_INFO") as f: + content = json.loads(f.read()) + + joined_sizes = content["sizes"] + issue_number = content["issue_number"] + + lines: List[str] = [] + significant_lines: List[str] = [] + + lines.append("### Size Comparison") + lines.append("") + lines.append("
") + lines.append(header) + lines.append(sep) + + for (i, sizes) in joined_sizes: + (master_size, pr_size) = sizes + + master_size_str = format_size(master_size) + pr_size_str = format_size(pr_size) + + (diff_str, diff_significant) = format_diff_size(master_size, pr_size) + + line_str = f"| {i} | {master_size_str} | {pr_size_str} | {diff_str} |" + + lines.append(line_str) + + if diff_significant: + significant_lines.append(line_str) + + lines.append("") + lines.append("
") + + if significant_lines: + lines.append("") + lines.append("⚠️ The following examples have changed their size significantly:") + lines.append("") + + lines.append(header) + lines.append(sep) + lines.extend(significant_lines) + + output = "\n".join(lines) + + with open(os.environ["GITHUB_ENV"], "a+") as f: + f.write(f"YEW_EXAMPLE_SIZES={json.dumps(output)}\n") + f.write(f"PR_NUMBER={issue_number}\n") + + +if __name__ == "__main__": + main()