Files
lean4/tests/combine.py
Garmelon 08eb78a5b2 chore: switch to new test/bench suite (#12590)
This PR sets up the new integrated test/bench suite. It then migrates
all benchmarks and some related tests to the new suite. There's also
some documentation and some linting.

For now, a lot of the old tests are left alone so this PR doesn't become
even larger than it already is. Eventually, all tests should be migrated
to the new suite though so there isn't a confusing mix of two systems.
2026-02-25 13:51:53 +00:00

81 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import json
import sys
from pathlib import Path
from typing import Any
def add_measurement(
values: dict[str, float],
units: dict[str, str | None],
data: dict[str, Any],
) -> None:
metric = data["metric"]
values[metric] = values.get(metric, 0) + data["value"]
units[metric] = data.get("unit")
def format_measurement(
values: dict[str, float],
units: dict[str, str | None],
name: str,
) -> dict[str, Any]:
value = values[name]
unit = units.get(name)
data: dict[str, Any] = {"metric": name, "value": value}
if unit is not None:
data["unit"] = unit
return data
def main() -> None:
parser = argparse.ArgumentParser(
description="Combine measurement files in the JSON Lines format, summing duplicated measurements like radar does.",
)
parser.add_argument(
"input",
nargs="*",
default=[],
help="input files to read measurements from. If none are specified, measurements are read from stdin.",
)
parser.add_argument(
"-o",
"--output",
type=Path,
help="output file to write measurements to. If not specified, the result is printed to stdout.",
)
args = parser.parse_args()
inputs: list[Path] = args.input
output: Path | None = args.output
values: dict[str, float] = {}
units: dict[str, str | None] = {}
# Read measurements
if inputs:
for input in inputs:
with open(input, "r") as f:
for line in f:
add_measurement(values, units, json.loads(line))
else:
for line in sys.stdin:
add_measurement(values, units, json.loads(line))
# Write measurements
if output:
with open(output, "w") as f:
for metric in sorted(values):
f.write(f"{json.dumps(format_measurement(values, units, metric))}\n")
else:
for metric in sorted(values):
print(json.dumps(format_measurement(values, units, metric)))
if __name__ == "__main__":
main()