-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track crate dependency count over time (#5228)
### What * Part of #4788 * Closes #5101 ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested the web demo (if applicable): * Using newly built examples: [app.rerun.io](https://app.rerun.io/pr/5228/index.html) * Using examples from latest `main` build: [app.rerun.io](https://app.rerun.io/pr/5228/index.html?manifest_url=https://app.rerun.io/version/main/examples_manifest.json) * Using full set of examples from `nightly` build: [app.rerun.io](https://app.rerun.io/pr/5228/index.html?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json) * [x] The PR title and labels are set such as to maximize their usefulness for the next release's CHANGELOG * [x] If applicable, add a new check to the [release checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)! - [PR Build Summary](https://build.rerun.io/pr/5228) - [Docs preview](https://rerun.io/preview/0223b21104bad205cfc5004aa33386d9b52fd603/docs) <!--DOCS-PREVIEW--> - [Examples preview](https://rerun.io/preview/0223b21104bad205cfc5004aa33386d9b52fd603/examples) <!--EXAMPLES-PREVIEW--> - [Recent benchmark results](https://build.rerun.io/graphs/crates.html) - [Wasm size tracking](https://build.rerun.io/graphs/sizes.html) --------- Co-authored-by: jprochazk <[email protected]>
- Loading branch information
Showing
5 changed files
with
247 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
Measure sizes of a list of files. | ||
This produces the format for use in https://github.com/benchmark-action/github-action-benchmark. | ||
Use the script: | ||
python3 scripts/ci/count_bytes.py --help | ||
python3 scripts/ci/count_bytes.py \ | ||
"Wasm":web_viewer/re_viewer_bg.wasm | ||
python3 scripts/ci/count_bytes.py --format=github \ | ||
"Wasm":web_viewer/re_viewer_bg.wasm | ||
""" | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import json | ||
import os.path | ||
import sys | ||
from enum import Enum | ||
from typing import Any | ||
|
||
|
||
def get_unit(size: int | float) -> str: | ||
UNITS = ["B", "kiB", "MiB", "GiB", "TiB"] | ||
|
||
unit_index = 0 | ||
while size > 1024: | ||
size /= 1024 | ||
unit_index += 1 | ||
|
||
return UNITS[unit_index] | ||
|
||
|
||
DIVISORS = { | ||
"B": 1, | ||
"kiB": 1024, | ||
"MiB": 1024 * 1024, | ||
"GiB": 1024 * 1024 * 1024, | ||
"TiB": 1024 * 1024 * 1024 * 1024, | ||
} | ||
|
||
|
||
def get_divisor(unit: str) -> int: | ||
return DIVISORS[unit] | ||
|
||
|
||
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] | ||
separator = "|" + "|".join("-" * (width + 2) for width in column_widths) | ||
header_row = "|".join(f" {key.center(width)} " for key, width in zip(keys, column_widths)) | ||
|
||
table = f"|{header_row}|\n{separator}|\n" | ||
for row in data: | ||
row_str = "|".join(f" {str(row.get(key, '')).ljust(width)} " for key, width in zip(keys, column_widths)) | ||
table += f"|{row_str}|\n" | ||
|
||
return table | ||
|
||
|
||
def render_table_rows(rows: list[Any], headers: list[str]) -> str: | ||
column_widths = [max(len(str(item)) for item in col) for col in zip(*([tuple(headers)] + rows))] | ||
separator = "|" + "|".join("-" * (width + 2) for width in column_widths) | ||
header_row = "|".join(f" {header.center(width)} " for header, width in zip(headers, column_widths)) | ||
|
||
table = f"|{header_row}|\n{separator}|\n" | ||
for row in rows: | ||
row_str = "|".join(f" {str(item).ljust(width)} " for item, width in zip(row, column_widths)) | ||
table += f"|{row_str}|\n" | ||
|
||
return table | ||
|
||
|
||
class Format(Enum): | ||
JSON = "json" | ||
GITHUB = "github" | ||
|
||
def render(self, data: list[dict[str, str]]) -> str: | ||
if self is Format.JSON: | ||
return json.dumps(data) | ||
if self is Format.GITHUB: | ||
return render_table_dict(data) | ||
|
||
|
||
def measure(files: list[str], format: Format) -> None: | ||
output: list[dict[str, str]] = [] | ||
for arg in files: | ||
parts = arg.split(":") | ||
name = parts[0] | ||
file = parts[1] | ||
size = os.path.getsize(file) | ||
unit = parts[2] if len(parts) > 2 else get_unit(size) | ||
div = get_divisor(unit) | ||
|
||
output.append( | ||
{ | ||
"name": name, | ||
"value": str(round(size / div, 2)), | ||
"unit": unit, | ||
} | ||
) | ||
|
||
sys.stdout.write(format.render(output)) | ||
sys.stdout.flush() | ||
|
||
|
||
def percentage(value: str) -> int: | ||
value = value.replace("%", "") | ||
return int(value) | ||
|
||
|
||
def main() -> None: | ||
parser = argparse.ArgumentParser(description="Generate a PR summary page") | ||
parser.add_argument( | ||
"--format", | ||
type=Format, | ||
choices=list(Format), | ||
default=Format.JSON, | ||
help="Format to render", | ||
) | ||
parser.add_argument("files", nargs="*", help="Entries to measure. Format: name:path[:unit]") | ||
|
||
args = parser.parse_args() | ||
measure(args.files, args.format) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.