"""真实项目模块增删、重命名及干净构建对照;保留每次源码和输出。""" import argparse import json import os from pathlib import Path import resource import shutil from scale import execute, fingerprint, source def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--compiler", type=Path, required=True) parser.add_argument("--output", type=Path, required=True) parser.add_argument("--profile", choices=["dev", "release"], required=True) parser.add_argument("--syntax", choices=["old", "next"], default="next") parser.add_argument("--sizes", nargs="+", type=int, default=[100, 1000, 10000]) parser.add_argument("--shape", choices=["owned", "linear"], default="owned") args = parser.parse_args() if args.syntax == "old" and args.profile != "release": parser.error("old syntax has no public dev build profile") compiler = args.compiler.resolve(strict=True) root = args.output.absolute() root.mkdir(parents=True, exist_ok=False) resource.setrlimit(resource.RLIMIT_AS, (6 * 1024 ** 3, 6 * 1024 ** 3)) os.environ["CARGO_BUILD_JOBS"] = "1" report = dict(compiler=str(compiler), compiler_sha256=fingerprint(compiler), rounds=3, profile=args.profile, syntax=args.syntax, shape=args.shape, samples=[]) (root / "modules.py").write_bytes(Path(__file__).read_bytes()) (root / "scale.py").write_bytes(Path(__file__).with_name("scale.py").read_bytes()) cgroup = Path("/sys/fs/cgroup") report["environment"] = dict(cpu_max=(cgroup / "cpu.max").read_text(), memory_max=(cgroup / "memory.max").read_text(), virtual_limit_bytes=6 * 1024 ** 3) initial_oom = dict(line.split() for line in (cgroup / "memory.events").read_text().splitlines())["oom_kill"] for count in args.sizes: original, expected = source(count, args.shape) body, ending = original.rsplit("\n}", 1) added = body + "\n answer = answer + support.value.app.increment()\n}" + ending renamed = added.replace("support.value.app.increment()", "support.renamed.app.increment()") for repeat in range(3): case = root / f"{count}-{repeat}" domain = case / "project/module/main/main" (domain / "app").mkdir(parents=True) (domain / "api.dever").write_text("cmd main = app.main\n") extra = case / "project/module/support/value/app/main.dever" moved = case / "project/module/support/renamed/app/main.dever" for scenario, text, answer in [("first", original, expected), ("module-added", added, expected + 7), ("module-renamed", renamed, expected + 7), ("module-removed", original, expected)]: if scenario == "module-added": extra.parent.mkdir(parents=True) extra.write_text("increment() (answer: Int) { answer = 7 }\n") elif scenario == "module-renamed": extra.parent.parent.rename(moved.parent.parent) elif scenario == "module-removed": moved.unlink() (domain / "app/main.dever").write_text(text) formatted = execute([str(compiler), "fmt", str(case / "project")], case, scenario + "-fmt", 900) assert formatted["exit_code"] == 0, formatted snapshot = case / (scenario + "-sources.json") snapshot.write_text(json.dumps({str(p.relative_to(case / "project")): p.read_text() for p in (case / "project").rglob("*.dever")}, indent=2) + "\n") binary = case / scenario command = [str(compiler), "build", str(case / "project"), "--output", str(binary)] if args.syntax == "next": command += ["--profile", args.profile] row = dict(count=count, repeat=repeat, scenario=scenario, expected=answer, sources_sha256=fingerprint(snapshot), **execute(command, case, scenario, 900)) report["samples"].append(row) (root / "results.json").write_text(json.dumps(report, indent=2) + "\n") assert row["exit_code"] == 0, row ran = execute([str(binary), "main.main.main", "{}"], case, scenario + "-run", 60) row["execution"] = ran row["binary_sha256"] = fingerprint(binary) value = json.loads(Path(ran["stdout"]).read_text()) if ran["exit_code"] == 0 else None row["correct"] = value is not None and value["code"] == 0 and value["data"] == answer (root / "results.json").write_text(json.dumps(report, indent=2) + "\n") assert row["correct"], row if scenario != "first": # 只复制源码,避免把增量缓存带入干净构建。 clean = case / (scenario + "-clean-project") shutil.copytree(case / "project/module", clean / "module") clean_binary = case / (scenario + "-clean") clean_command = [str(compiler), "build", str(clean), "--output", str(clean_binary)] if args.syntax == "next": clean_command += ["--profile", args.profile] row["clean_build"] = execute(clean_command, case, scenario + "-clean", 900) (root / "results.json").write_text(json.dumps(report, indent=2) + "\n") assert row["clean_build"]["exit_code"] == 0, row clean_run = execute([str(clean_binary), "main.main.main", "{}"], case, scenario + "-clean-run", 60) row["clean_execution"] = clean_run row["clean_binary_sha256"] = fingerprint(clean_binary) row["clean_correct"] = clean_run["exit_code"] == 0 and json.loads( Path(clean_run["stdout"]).read_text()) == value (root / "results.json").write_text(json.dumps(report, indent=2) + "\n") assert row["clean_correct"], row assert dict(line.split() for line in (cgroup / "memory.events").read_text().splitlines())["oom_kill"] == initial_oom print(count, repeat, scenario, row["seconds"], flush=True) if __name__ == "__main__": main()