"""从冻结 R0 源码与当前编译器准备 Model 四方对端;不覆盖基线或系统工具。""" import argparse import json import os from pathlib import Path import shutil import sqlite3 from scale import execute, fingerprint def build_resource(core, root, directory="rust-resource", *, source="resource.rs.txt", target=None, dependencies="", read_boundary=None): """为既有四方产物补建独立异步对端,不重建或覆盖原始产物。""" peer = root / directory peer.mkdir() shutil.copyfile(core / "test/performance/fixtures/model" / source, peer / "main.rs") shutil.copyfile(core / "test/performance/fixtures/sqlite_resource.rs.txt", peer / "sqlite_resource.rs") shutil.copyfile(core / "rust/Cargo.lock", peer / "Cargo.lock") features = [] if read_boundary: shutil.copyfile(core / "test/performance/fixtures/model/reads_boundary.rs.txt", peer / "boundary.rs") with sqlite3.connect(f"file:{read_boundary}?mode=ro", uri=True) as db: schemas = db.execute("SELECT name,definition FROM _dever_history ORDER BY name").fetchall() # 这些是两方共用的声明输入;结构核验由独立参考自行实现。 (peer / "schemas.rs").write_text("const SCHEMAS: &[(&str,&str)] = &[" + "".join(f"({json.dumps(name)},{json.dumps(sql)})," for name, sql in schemas) + "];\n") features = ["--features", "read-boundary"] manifest = '''[package] name = "model-resource-peer" version = "0.0.1" edition = "2024" [[bin]] name = "model-resource-peer" path = "main.rs" [dependencies] deadpool-sqlite = { version = "=0.14.0", features = ["bundled", "hooks", "functions"] } tokio = { version = "=1.53.1", features = ["rt", "time", "macros", "sync"] } tokio-util = { version = "=0.7.19", features = ["rt"] } percent-encoding = "=2.3.2" serde = { version = "=1.0.228", features = ["derive"] } serde_json = "=1.0.145" [features] read-boundary = [] [profile.release] lto = "fat" codegen-units = 1 overflow-checks = true strip = true ''' (peer / "Cargo.toml").write_text(manifest.replace("[features]", dependencies + "[features]")) row = execute(["cargo", "+1.95.0", "build", "--offline", "--release", *features, "--manifest-path", str(peer / "Cargo.toml"), "--target-dir", str(target or peer / "target")], peer, "build", 1800) (peer / "build.json").write_text(json.dumps(row, indent=2) + "\n") assert row["exit_code"] == 0, row return (target or peer / "target") / "release/model-resource-peer" def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--compiler", type=Path, required=True) parser.add_argument("--baseline", type=Path, required=True) parser.add_argument("--output", type=Path, required=True) args = parser.parse_args() core = Path(__file__).resolve().parents[2] baseline = args.baseline.resolve(strict=True) compiler = args.compiler.resolve(strict=True) root = args.output.absolute() root.mkdir(parents=True, exist_ok=False) manifest = json.loads((baseline / "baseline-source-manifest.json").read_text()) sources = {name: item["sha256"] for name, item in manifest["files"].items() if name.startswith(("rust/", "lib/"))} assert all(fingerprint(baseline / "restored" / name) == digest for name, digest in sources.items()) original = json.loads((baseline / "baseline-build.json").read_text())["compiler"] identity = dict(verified_sources=sources, original_compiler=original, action="Build frozen source to separate target; retain R0 outputs") (root / "baseline-identity.json").write_text(json.dumps(identity, indent=2) + "\n") report = dict(compiler=dict(path=str(compiler), sha256=fingerprint(compiler)), commands=[]) os.environ["CARGO_BUILD_JOBS"] = "2" def run(label, command): row = execute([str(value) for value in command], root, label, 1800) report["commands"].append(row) (root / "build.json").write_text(json.dumps(report, indent=2) + "\n") assert row["exit_code"] == 0, row old = root / "old-compiler-target/release/dever" run("old-compiler-build", ["cargo", "+1.95.0", "build", "--locked", "--offline", "--release", "--features", "postgres", "--bin", "dever", "--manifest-path", baseline / "restored/rust/Cargo.toml", "--target-dir", root / "old-compiler-target"]) fixture = Path(__file__).parent / "fixtures/model" for name, tool in [("new", compiler), ("old", old)]: project = root / name module = project / "module/benchmark/article" module.mkdir(parents=True) for source in ("api.dever", "model.dever", "app.dever"): target = module / source if name == "new" and source != "api.dever": target = module / ("data" if source == "model.dever" else "app") / "main.dever" target.parent.mkdir() shutil.copyfile(fixture / ("old.dever.txt" if name == "old" and source == "app.dever" else source), target) (project / "config").mkdir() (project / "data").mkdir() config = project / "config/setting.json" config.write_text(json.dumps({"database": {"default": { "type": "sqlite", "path": "data/model.db", "max_connections": 1}}}) + "\n") run(name + "-format", [tool, "fmt", project]) run(name + "-build", [tool, "build", project, "--output", root / (name + "-app")]) if name == "new": run(name + "-migrate", [root / "new-app", "--config", config, "migrate"]) else: run(name + "-initialize", [root / "old-app", "--config", config, "benchmark.article.count", "{}"]) peer = root / "rust" peer.mkdir() shutil.copyfile(fixture / "peer.rs.txt", peer / "main.rs") shutil.copyfile(core / "rust/Cargo.lock", peer / "Cargo.lock") (peer / "Cargo.toml").write_text('''[package] name = "model-peer" version = "0.0.1" edition = "2024" [[bin]] name = "model-peer" path = "main.rs" [dependencies] rusqlite = { version = "=0.40.2", features = ["bundled", "functions"] } serde = { version = "=1.0.228", features = ["derive"] } serde_json = "=1.0.145" [profile.release] lto = "fat" codegen-units = 1 overflow-checks = true strip = true ''') run("rust-build", ["cargo", "+1.95.0", "build", "--offline", "--release", "--manifest-path", peer / "Cargo.toml", "--target-dir", peer / "target"]) build_resource(core, root) peer = root / "go" peer.mkdir() shutil.copyfile(fixture / "peer.go.txt", peer / "main.go") for name in ("go.mod", "go.sum"): shutil.copyfile(fixture / (name + ".txt"), peer / name) run("go-download", ["go", "-C", peer, "mod", "download", "github.com/mattn/go-sqlite3"]) run("go-build", ["go", "-C", peer, "build", "-buildvcs=false", "-mod=readonly", "-trimpath", "-ldflags=-s -w", "-o", root / "go-app", "."]) assert all(fingerprint(baseline / "restored" / name) == digest for name, digest in sources.items()) report["old_compiler_sha256"] = fingerprint(old) (root / "build.json").write_text(json.dumps(report, indent=2) + "\n") if __name__ == "__main__": main()