Files
romm/backend/tools/measure_dump_cache.py
Sam Dornan 130ea73c23 perf(metadata): compress the cache-only metadata dump records (#4426)
* perf(metadata): compress the cache-only metadata dump records

The LaunchBox and Switch TitleDB stores are read by exact key and never
scanned, so each value can be compressed on its own. On a full import of
both real dumps into the bundled valkey, `used_memory` drops from 539.1MB
to 335.9MB and RSS from 553.3MB to 350.2MB, a 38% saving.

Values are zstd level 3, which costs 3-6us per record read, less than the
`json.loads` that follows it. A payload under 256 bytes is stored as plain
JSON: below that a frame header and a poor ratio on a short payload cost
more than they save, which is what leaves the id-valued title and product
id indexes untouched. `tools/measure_dump_cache.py --sweep` finds the real
records flat between 64 and 256 bytes.

Reads detect the zstd frame rather than trusting the key, so a store
written by an earlier import keeps answering until the next update
rewrites it. No re-import is required to upgrade.

A zstd frame is not UTF-8, so the dump keys move to a cache client that
leaves values as bytes. `async_cache` still serves everything else,
including the existence checks on these keys.

The new tool measures per-store size with `MEMORY USAGE ... SAMPLES 0`,
which walks every field. The default of five samples extrapolates, and on
a hash of this size it is wrong by enough that per-key figures stop
adding up to `used_memory`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQNNQHk7uyS3eoB9yAxoM9

* test(metadata): assert the writer stores through the dump codec

`test_values_are_json_encoded` pinned the exact string `json.dumps`
produced with default separators. The writer now hands the pipeline
compact bytes from `encode`, so the test asserts the contract that
matters instead: the value is bytes, and it decodes back to the record.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQNNQHk7uyS3eoB9yAxoM9

* fix(tools): stop the dump measurement wiping the whole cache server

`flushall` between passes would erase every database on whatever
`--redis-url` names, and RomM's sessions and RQ queues share that
database with the dump stores, so pointing the tool at a live instance
cost far more than the stores it meant to reset.

It now deletes only the ten keys it writes, and refuses a server holding
anything else unless forced, since a running RomM would have to
re-import what it drops. Each pass records what the server uses without
those stores and reports figures net of it, so a non-empty server still
measures what the dumps themselves cost.

Reported by Greptile on #4426.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQNNQHk7uyS3eoB9yAxoM9

* fix(tools): report field counts and bytes off the stores, not the writes

Two accounting errors in the measurement tool, both reported by Copilot
on #4426.

Field counts and byte totals were tallied while writing, so they counted
write attempts. `HSET` overwrites, and the dump repeats a title on one
platform and an alternate name across platforms, so 8,997 of the writes
landed on a field another record already held: the alternate name store
reported 70,297 fields against 62,384 actually stored. Both figures now
come from `HLEN` and a scan of each hash, which cannot drift from what
the store holds.

`used_memory_rss` was offset by a `used_memory` baseline, mixing two
metrics. Netting RSS turns out to be wrong however the baseline is
captured, because the pages a delete frees stay resident, which made the
encoded pass report -56.1MB. RSS is now reported absolute, and `--only`
loads a single encoding so it can be read off a server started fresh for
that pass: 555.8MB plain against 348.9MB encoded, measured that way.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQNNQHk7uyS3eoB9yAxoM9

* style(tools): cut the measurement tool's docstrings to the repo's limit

CLAUDE.md allows a docstring one sentence plus `Args:`/`Returns:`, and the
module docstring ran to four paragraphs. The non-obvious reasons move to
one-line comments at the code they explain, which is where a reader needs
them, and the operational detail was already carried by `--force` and
`--only`'s help text and by the refusal message itself.

Reported by Copilot on #4426.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQNNQHk7uyS3eoB9yAxoM9

* fix(tools): fold titles the way production does, and stop echoing the URL

Four more Copilot findings on #4426, three of them in the measurement tool
and one in the codec module.

The folded-title stand-in kept only ASCII alphanumerics, the exact thing
`fold_title`'s docstring warns against: it reduced `Astérix` to `astrix`
and `三國立志傳2` to `2`, and dropped titles that fold to nothing, so the
folded index measured smaller than it is. The tool now mirrors
`fold_title` character for character, verified against the real function
extracted from its module over 40,000 dump titles with no mismatch. The
store grows by 244 fields and 0.1MB, so the totals barely move.

The refusal path printed the target URL, which RomM builds with the
password inline, so it could have carried a credential into a CI log. It
now names the server generically and keeps the key count and the remedy.

`dump_cache`'s module docstring and its threshold comment both ran past
the one-to-two-line limit in CLAUDE.md. The benchmark numbers behind the
threshold live in the PR description, not the source.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQNNQHk7uyS3eoB9yAxoM9

* refactor(cache): give the dump store keys one home

The measurement tool carried its own copies of the ten store key strings,
against the single-source rule in CLAUDE.md. That matters more now the
tool deletes those keys: a production rename would have left it deleting
stale hashes, missing the new ones, and counting the real stores as
foreign data in its own safety guard.

The keys move to `handler/dump_cache.py`, which already holds the codec
that reads and writes them and depends on nothing but the cache client,
so the importers, the lookups and the tool all take them from there.
`DUMP_STORE_KEYS` is the list the tool measures and clears.

`base_handler` was reaching into `tasks/scheduled/update_switch_titledb`
for two constants, so a handler depended on a task module; that import is
gone. `launchbox_handler/types.py` keeps the initial-import flag, which
marks an import in progress rather than naming a store.

Measurement is unchanged: 538.0MB to 334.8MB, per-store figures identical.

Reported by Copilot on #4426.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQNNQHk7uyS3eoB9yAxoM9

* fix(tests): import the store keys from where the merge left them

Two test modules still took the keys from `handler.dump_cache`, which the
merge emptied of them, so collection failed with an ImportError on both
databases and mypy flagged nine missing attributes. They read them from
`launchbox_handler/types.py` and the TitleDB task now, as the code does.

Also reverts formatting the merge dragged into four files this change has
no business touching: running isort over everything the merge listed
reclassified `tests.*` and `startup` out of the block master keeps them
in, and black rewrote two `except (A, B)` clauses into the unparenthesized
form. Those files go back to master's copy.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQNNQHk7uyS3eoB9yAxoM9

* style(tests): restore both metadata test files to master's formatting

Running the formatters over everything the merge touched had reordered
imports trunk keeps in another block. Both files go back to master's copy,
with the compressed-store read test re-added by hand and its import placed
in the first-party block where the other `handler.*` imports sit.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQNNQHk7uyS3eoB9yAxoM9

* fix(tools): report bad input rather than dividing by zero

A zip with none of the four XML files loads no records, and every ratio
the report prints then divides by a zero total.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQNNQHk7uyS3eoB9yAxoM9

* refactor(tools): match the yields to their siblings, type the serializer

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQNNQHk7uyS3eoB9yAxoM9

* style: trim the comments and tighten the names review-polish flags

Cut the version notes to what version 2 is, drop the two comments that
repeated a neighbour, rename the tool's `measure` to `store_memory` beside
`store_stats`, and annotate the codec tests' parametrized arguments.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQNNQHk7uyS3eoB9yAxoM9

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-10 16:09:32 -05:00

499 lines
18 KiB
Python

"""Measure what the metadata dump stores cost in a real valkey, encoded or not.
Usage:
uv run tools/measure_dump_cache.py \
--metadata-zip Metadata.zip --titledb US.en.json \
--redis-url redis://localhost:6379
"""
import argparse
import json
import statistics
import sys
import time
import unicodedata
import zipfile
from collections.abc import Callable, Iterator
from pathlib import Path
from typing import Any, Final
import redis
from defusedxml import ElementTree as ET
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from handler.dump_cache import COMPRESS_MIN_BYTES, decode, encode # noqa: E402
# Mirrors LAUNCHBOX_METADATA_STORE.keys and SWITCH_TITLEDB_STORE.keys, whose
# modules need the app to import. The only keys this tool ever deletes.
LAUNCHBOX_PLATFORMS_KEY: Final = "romm:launchbox_platforms"
LAUNCHBOX_METADATA_DATABASE_ID_KEY: Final = "romm:launchbox_metadata_database_id"
LAUNCHBOX_METADATA_NAME_KEY: Final = "romm:launchbox_metadata_name"
LAUNCHBOX_METADATA_ALTERNATE_NAME_KEY: Final = "romm:launchbox_metadata_alternate_name"
LAUNCHBOX_METADATA_FOLDED_NAME_KEY: Final = "romm:launchbox_metadata_folded_name"
LAUNCHBOX_METADATA_IMAGE_KEY: Final = "romm:launchbox_metadata_image"
LAUNCHBOX_MAME_KEY: Final = "romm:launchbox_mame"
LAUNCHBOX_FILES_KEY: Final = "romm:launchbox_files"
SWITCH_TITLEDB_INDEX_KEY: Final = "romm:switch_titledb"
SWITCH_PRODUCT_ID_KEY: Final = "romm:switch_product_id"
DUMP_STORE_KEYS: Final[tuple[str, ...]] = (
LAUNCHBOX_PLATFORMS_KEY,
LAUNCHBOX_METADATA_DATABASE_ID_KEY,
LAUNCHBOX_METADATA_NAME_KEY,
LAUNCHBOX_METADATA_ALTERNATE_NAME_KEY,
LAUNCHBOX_METADATA_FOLDED_NAME_KEY,
LAUNCHBOX_METADATA_IMAGE_KEY,
LAUNCHBOX_MAME_KEY,
LAUNCHBOX_FILES_KEY,
SWITCH_TITLEDB_INDEX_KEY,
SWITCH_PRODUCT_ID_KEY,
)
# Mirrors the projections in `launchbox_handler/types.py`, whose module needs
# the app to import. Each store keeps only the fields its reader reads.
LAUNCHBOX_IMAGE_FIELDS: Final[frozenset[str]] = frozenset(
{"FileName", "Type", "Region"}
)
LAUNCHBOX_FILE_FIELDS: Final[frozenset[str]] = frozenset({"GameName"})
LAUNCHBOX_MAME_FIELDS: Final[frozenset[str]] = frozenset({"Name"})
WRITE_BATCH = 2000
Record = tuple[str, str, Any]
def fold_title(title: str) -> str:
"""Mirror of `launchbox_handler.utils.fold_title`, whose module needs the app."""
kept: list[str] = []
for char in unicodedata.normalize("NFKD", title.casefold()):
if unicodedata.category(char).startswith("M"):
if kept and "a" <= kept[-1] <= "z":
continue
kept.append(char)
elif char.isalnum():
kept.append(char)
return "".join(kept)
def element_to_dict(elem: Any, fields: frozenset[str] | None = None) -> dict[str, Any]:
return {
child.tag: child.text for child in elem if fields is None or child.tag in fields
}
def iter_elements(source: Any) -> Iterator[Any]:
"""Yield each top-level record, dropping it once the caller moves on."""
ctx = ET.iterparse(source, events=("start", "end"))
try:
_, root = next(iter(ctx))
except StopIteration:
return
depth = 0
for event, elem in ctx:
if event == "start":
depth += 1
continue
depth -= 1
if depth > 0:
continue
yield elem
root.clear()
# Mirrors the record shapes `tasks/scheduled/update_launchbox_metadata.py`
# writes, duplicated because a tool cannot import the task without the app.
def iter_launchbox(metadata_zip: Path) -> Iterator[Record]:
with zipfile.ZipFile(metadata_zip) as z:
names = z.namelist()
if "Platforms.xml" in names:
with z.open("Platforms.xml") as f:
for elem in iter_elements(f):
if elem.tag != "Platform":
continue
name = elem.find("Name")
if name is not None and name.text:
yield (
LAUNCHBOX_PLATFORMS_KEY,
name.text.strip(),
element_to_dict(elem),
)
if "Metadata.xml" in names:
with z.open("Metadata.xml") as f:
image_id: str | None = None
images: list[dict[str, Any]] = []
for elem in iter_elements(f):
if elem.tag == "Game":
id_elem = elem.find("DatabaseID")
database_id = (
id_elem.text.strip()
if id_elem is not None and id_elem.text
else None
)
if database_id:
yield (
LAUNCHBOX_METADATA_DATABASE_ID_KEY,
database_id,
element_to_dict(elem),
)
name_elem = elem.find("Name")
platform_elem = elem.find("Platform")
if (
database_id
and name_elem is not None
and name_elem.text
and platform_elem is not None
and platform_elem.text
):
platform = platform_elem.text.strip()
title = name_elem.text.strip()
yield (
LAUNCHBOX_METADATA_NAME_KEY,
f"{title.lower()}:{platform}",
database_id,
)
folded = fold_title(title)
if folded:
yield (
LAUNCHBOX_METADATA_FOLDED_NAME_KEY,
f"{folded}:{platform}",
database_id,
)
elif elem.tag == "GameAlternateName":
alt = (elem.findtext("AlternateName") or "").strip()
alt_id = (elem.findtext("DatabaseID") or "").strip()
if alt and alt_id:
yield (
LAUNCHBOX_METADATA_ALTERNATE_NAME_KEY,
alt.lower(),
alt_id,
)
elif elem.tag == "GameImage":
id_elem = elem.find("DatabaseID")
if id_elem is None or not id_elem.text:
continue
current = id_elem.text.strip()
if image_id is not None and current != image_id:
yield LAUNCHBOX_METADATA_IMAGE_KEY, image_id, images
images = []
image_id = current
images.append(element_to_dict(elem, LAUNCHBOX_IMAGE_FIELDS))
if image_id is not None:
yield LAUNCHBOX_METADATA_IMAGE_KEY, image_id, images
if "Mame.xml" in names:
with z.open("Mame.xml") as f:
for elem in iter_elements(f):
if elem.tag != "MameFile":
continue
fn = elem.find("FileName")
if fn is not None and fn.text:
yield (
LAUNCHBOX_MAME_KEY,
fn.text.strip(),
element_to_dict(elem, LAUNCHBOX_MAME_FIELDS),
)
if "Files.xml" in names:
with z.open("Files.xml") as f:
for elem in iter_elements(f):
if elem.tag != "File":
continue
fn = elem.find("FileName")
platform = elem.find("Platform")
if (
fn is not None
and fn.text
and platform is not None
and platform.text
):
field = f"{fn.text.strip().lower()}:{platform.text.strip()}"
yield (
LAUNCHBOX_FILES_KEY,
field,
element_to_dict(elem, LAUNCHBOX_FILE_FIELDS),
)
def iter_titledb(titledb: Path) -> Iterator[Record]:
data = json.loads(titledb.read_text())
relevant = {k: v for k, v in data.items() if k and v}
for title_id, entry in relevant.items():
yield SWITCH_TITLEDB_INDEX_KEY, title_id, entry
for title_id, entry in relevant.items():
product_id = entry.get("id")
if product_id:
yield SWITCH_PRODUCT_ID_KEY, product_id, title_id
def plain(value: Any) -> bytes:
"""Serialize a record as the uncompressed baseline the report compares to."""
return json.dumps(value).encode()
def load(
client: redis.Redis, records: Iterator[Record], serialize: Callable[[Any], bytes]
) -> None:
"""Write every record into the store its key names."""
pipe = client.pipeline(transaction=False)
queued = 0
for key, field, value in records:
pipe.hset(key, mapping={field: serialize(value)})
queued += 1
if queued >= WRITE_BATCH:
pipe.execute()
queued = 0
if queued:
pipe.execute()
def store_stats(client: redis.Redis) -> tuple[dict[str, int], dict[str, int]]:
"""Field count and total value bytes per store, read back off the hashes."""
# Not tallied while writing: the dump repeats a title on one platform and an
# alternate name across platforms, so ~9k records overwrite another's field.
counts: dict[str, int] = {}
value_bytes: dict[str, int] = {}
for key in DUMP_STORE_KEYS:
count = client.hlen(key)
if not count:
continue
counts[key] = count
value_bytes[key] = sum(
len(value) for _, value in client.hscan_iter(key, count=1000)
)
return counts, value_bytes
def foreign_keys(client: redis.Redis) -> int:
"""How many keys on the server are not this tool's own stores."""
owned = {key.encode() for key in DUMP_STORE_KEYS}
return sum(1 for key in client.scan_iter(count=1000) if key not in owned)
def clear_stores(client: redis.Redis) -> tuple[int, int]:
"""Drop only this tool's stores, returning `used_memory` and RSS without them."""
# Not `flushall`: the URL can point at a live RomM, whose sessions and RQ
# queues share the database with these stores.
client.delete(*DUMP_STORE_KEYS)
info = client.info("memory")
return int(info["used_memory"]), int(info["used_memory_rss"])
def store_memory(
client: redis.Redis, keys: list[str]
) -> tuple[dict[str, int], int, int]:
sizes = {}
for key in keys:
# SAMPLES 0 walks every field rather than extrapolating from five.
size = client.execute_command("MEMORY", "USAGE", key, "SAMPLES", 0)
if size:
sizes[key] = int(size)
info = client.info("memory")
return sizes, int(info["used_memory"]), int(info["used_memory_rss"])
def mb(value: float) -> str:
return f"{value / 1024 / 1024:>8.1f}M"
def sweep_threshold(metadata_zip: Path, limit: int) -> int:
"""Total stored bytes at each candidate COMPRESS_MIN_BYTES, on real records."""
import zstandard
from handler.dump_cache import COMPRESSION_LEVEL
compressor = zstandard.ZstdCompressor(level=COMPRESSION_LEVEL)
sample: list[bytes] = []
for _, _, value in iter_launchbox(metadata_zip):
sample.append(plain(value))
if len(sample) >= limit:
break
if not sample:
print("no records in the zip; check --metadata-zip", file=sys.stderr)
return 1
raw = sum(len(payload) for payload in sample)
print(f"\nthreshold sweep over {len(sample):,} real records")
print(f"{'min bytes':>10} {'stored':>10} {'ratio':>7} {'compressed':>11}")
for threshold in (0, 64, 128, 192, 256, 384, 512, 1024, 1 << 30):
total = 0
compressed = 0
for payload in sample:
if len(payload) >= threshold:
total += len(compressor.compress(payload))
compressed += 1
else:
total += len(payload)
label = "never" if threshold == 1 << 30 else f"{threshold}"
print(
f"{label:>10} {mb(total)} {raw / total:>6.2f}x "
f"{compressed / len(sample):>10.0%}"
)
return 0
def bench_decode(
client: redis.Redis, key: str, rounds: int = 5
) -> tuple[float, float] | None:
"""Median `decode` latency, and mean stored size, on real stored records."""
fields = client.hrandfield(key, 2000)
if not fields:
return None
blobs = [b for b in client.hmget(key, fields) if b]
if not blobs:
return None
times = []
for _ in range(rounds):
start = time.perf_counter()
for blob in blobs:
decode(blob)
times.append((time.perf_counter() - start) / len(blobs) * 1e6)
return statistics.median(times), statistics.mean(len(b) for b in blobs)
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--metadata-zip", type=Path, required=True)
parser.add_argument("--titledb", type=Path)
parser.add_argument("--redis-url", default="redis://localhost:6379")
parser.add_argument(
"--sweep",
type=int,
default=0,
metavar="N",
help="sweep the compression threshold over N records and exit",
)
parser.add_argument(
"--force",
action="store_true",
help="run even though the server holds data other than these stores",
)
parser.add_argument(
"--only",
choices=("plain", "encoded"),
help="load one encoding and report it, for reading RSS off a server "
"started fresh for this pass",
)
args = parser.parse_args()
if args.sweep:
return sweep_threshold(args.metadata_zip, args.sweep)
client = redis.Redis.from_url(args.redis_url)
foreign = foreign_keys(client)
if foreign and not args.force:
# The URL is not echoed back: RomM builds it with the password inline.
print(
f"The target server holds {foreign:,} key(s) outside the metadata "
"dump stores, so it looks like a live instance. This tool drops and "
"rebuilds the dump stores, which a running RomM would then have to "
"re-import. Point it at a disposable server, or pass --force.",
file=sys.stderr,
)
return 1
def records() -> Iterator[Record]:
yield from iter_launchbox(args.metadata_zip)
if args.titledb:
yield from iter_titledb(args.titledb)
benched = (
LAUNCHBOX_METADATA_DATABASE_ID_KEY,
LAUNCHBOX_METADATA_IMAGE_KEY,
SWITCH_TITLEDB_INDEX_KEY,
)
results = {}
passes = [("plain", plain), ("encoded", encode)]
if args.only:
passes = [p for p in passes if p[0] == args.only]
for label, serialize in passes:
base_used, _ = clear_stores(client)
start = time.perf_counter()
load(client, records(), serialize)
elapsed = time.perf_counter() - start
counts, value_bytes = store_stats(client)
sizes, used, rss = store_memory(client, list(counts))
# Net of anything else on the server. Only `used_memory` can be offset
# this way; RSS keeps the pages a delete frees, so it stays absolute.
used -= base_used
latency = {key: bench_decode(client, key) for key in benched}
results[label] = (counts, value_bytes, sizes, used, rss, latency)
print(
f"{label}: loaded in {elapsed:.0f}s, used_memory {mb(used)}, "
f"rss {mb(rss)}"
)
if args.only:
return 0
p_counts, p_values, p_sizes, p_used, p_rss, p_latency = results["plain"]
_, e_values, e_sizes, e_used, e_rss, e_latency = results["encoded"]
if not p_sizes:
print("no records loaded; check --metadata-zip", file=sys.stderr)
return 1
print(f"\nCOMPRESS_MIN_BYTES = {COMPRESS_MIN_BYTES}\n")
header = f"{'store':<40} {'fields':>10} {'plain':>9} {'encoded':>9} {'saved':>7}"
print(header)
print("-" * len(header))
for key in sorted(p_sizes, key=lambda k: -p_sizes[k]):
plain_size = p_sizes[key]
enc_size = e_sizes.get(key, 0)
saved = 1 - enc_size / plain_size if plain_size else 0
print(
f"{key:<40} {p_counts[key]:>10,} {mb(plain_size)} {mb(enc_size)} "
f"{saved:>6.0%}"
)
print("-" * len(header))
print(
f"{'sum of stores':<40} {sum(p_counts.values()):>10,} "
f"{mb(sum(p_sizes.values()))} {mb(sum(e_sizes.values()))} "
f"{1 - sum(e_sizes.values()) / sum(p_sizes.values()):>6.0%}"
)
print(
f"{'used_memory (dump stores)':<40} {'':>10} {mb(p_used)} {mb(e_used)} "
f"{1 - e_used / p_used:>6.0%}"
)
# Only comparable between passes on a server restarted between them,
# which is what --only is for.
print(f"{'used_memory_rss (absolute)':<40} {'':>10} {mb(p_rss)} {mb(e_rss)}")
p_value_total, e_value_total = sum(p_values.values()), sum(e_values.values())
print(
f"\nstored value bytes: {mb(p_value_total)} -> {mb(e_value_total)} "
f"({p_value_total / e_value_total:.2f}x)"
)
print(
f"\n{'decode (us/record, mean stored bytes)':<40} {'plain':>16} {'encoded':>16}"
)
for key in benched:
before, after = p_latency.get(key), e_latency.get(key)
if not before or not after:
continue
print(
f"{key:<40} {before[0]:>7.2f} {before[1]:>8,.0f} "
f"{after[0]:>7.2f} {after[1]:>8,.0f}"
)
return 0
if __name__ == "__main__":
raise SystemExit(main())