Files
2026-11-27 06:03:56 +00:00

141 lines
7.2 KiB
Python

"""构建真实持久队列四方对端;冻结编译器仅复用,不改 bak 或系统工具。"""
import argparse
import json
from pathlib import Path
import shutil
import tomllib
from scale import execute, fingerprint
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--compiler", type=Path, required=True)
parser.add_argument("--old-compiler", type=Path, required=True)
parser.add_argument("--baseline-identity", type=Path, required=True,
help="existing R0 compiler identity; do not substitute an unrelated old executable")
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
core = Path(__file__).resolve().parents[2]
fixture = core / "test/performance/fixtures/job"
root = args.output.absolute()
root.mkdir(parents=True, exist_ok=False)
report = {"commands": [], "compilers": {}, "sources": {
str(p): fingerprint(p) for p in [Path(__file__), fixture.parent / "sqlite_resource.rs.txt", *fixture.iterdir()]}}
baseline = json.loads(args.baseline_identity.read_text())
assert fingerprint(args.old_compiler) == baseline["original_compiler"]["sha256"], "old compiler differs from frozen R0"
report["baseline_identity"] = baseline
def run(label, command):
row = execute(list(map(str, 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
for name, tool in [("new", args.compiler), ("old", args.old_compiler)]:
tool = tool.resolve(strict=True)
report["compilers"][name] = {"path": str(tool), "sha256": fingerprint(tool)}
project = root / name
module = project / "module/benchmark/queue"
module.mkdir(parents=True)
for role in ("model", "app"):
source = "old.dever.txt" if name == "old" and role == "app" else f"{role}.dever"
directory = "data" if role == "model" else role
target = module / (f"{directory}/main.dever" if name == "new" else f"{role}.dever")
target.parent.mkdir(exist_ok=True)
shutil.copyfile(fixture / source, target)
if name == "new":
shutil.copyfile(fixture / "api.dever", module / "api.dever")
else:
(module / "api.dever").write_text("cmd submit = app.submit\ncmd submit_all = app.submitAll\n")
(module / "job.dever").write_text(
"database default\njob publish(input: app.Payload) () retry(2) timeout(1000) { app.apply(input) }\n")
(project / "config").mkdir()
config = project / "config/setting.json"
config.write_text(json.dumps({"database": {"default": {"type": "sqlite",
"path": "queue.db" if name == "new" else "old/queue.db", "max_connections": 2}},
"job": {"workers": 1, "poll_ms": 1, "lease_ms": 10000,
"retry_base_ms": 1, "retry_max_ms": 10, "shutdown_ms": 5000}}) + "\n")
if name == "old":
settings = json.loads(config.read_text())
del settings["job"]["shutdown_ms"]
settings["runtime"] = {"mode": "worker", "shutdown_ms": 5000}
config.write_text(json.dumps(settings) + "\n")
run(name + "-fmt", [tool, "fmt", project])
run(name + "-build", [tool, "build", project, "--output", root / (name + "-app")])
command = [root / (name + "-app"), "--config", config]
run(name + "-initialize", command + (["migrate"] if name == "new" else ["benchmark.queue.submit_all", '{"inputs":[]}']))
build_references(core, root, run)
def build_references(core, root, run):
fixture = core / "test/performance/fixtures/job"
peer = root / "rust"
peer.mkdir()
shutil.copyfile(fixture / "peer.rs.txt", peer / "main.rs")
shutil.copyfile(fixture / "queue.rs.txt", peer / "queue.rs")
shutil.copyfile(core / "rust/Cargo.lock", peer / "Cargo.lock")
(peer / "Cargo.toml").write_text('''[package]
name = "job-peer"
version = "0.0.1"
edition = "2024"
[[bin]]
name = "job-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", "signal"] }
serde = { version = "=1.0.228", features = ["derive"] }
serde_json = "=1.0.145"
getrandom = "=0.2.17"
[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"])
locked = {(p["name"], p["version"]) for p in tomllib.loads((core / "rust/Cargo.lock").read_text())["package"]}
dependencies = tomllib.loads((peer / "Cargo.lock").read_text())["package"]
assert all((p["name"], p["version"]) in locked for p in dependencies if "source" in p)
peer = root / "go"
peer.mkdir()
shutil.copyfile(fixture / "peer.go.txt", peer / "main.go")
for name in ("go.mod", "go.sum"):
shutil.copyfile(core / "test/performance/fixtures/model" / (name + ".txt"), peer / name)
run("go-build", ["go", "-C", peer, "build", "-buildvcs=false", "-mod=readonly",
"-trimpath", "-ldflags=-s -w", "-o", root / "go-app", "."])
build_resource(core, root, run, target_dir=root / "rust/target")
build_resource(core, root, run, contract=True, target_dir=root / "rust/target")
def build_resource(core, root, run, contract=False, target_dir=None):
label = "rust-contract" if contract else "rust-resource"
binary_name = "job-contract-peer" if contract else "job-resource-peer"
peer = root / label
peer.mkdir()
for source, target in [("job/resource.rs.txt", "main.rs"), ("job/queue.rs.txt", "queue.rs"),
("sqlite_resource.rs.txt", "sqlite_resource.rs")]:
shutil.copyfile(core / "test/performance/fixtures" / source, peer / target)
if contract:
shutil.copyfile(core / "test/performance/fixtures/job/contract.rs.txt", peer / "contract.rs")
shutil.copyfile(core / "rust/Cargo.lock", peer / "Cargo.lock")
manifest = (root / "rust/Cargo.toml").read_text().replace("job-peer", binary_name)
manifest = manifest.replace("[profile.release]", 'tokio-util = { version = "=0.7.19", features = ["rt"] }\nfutures-util = "=0.3.34"\npercent-encoding = "=2.3.2"\n[profile.release]')
manifest += '\n[features]\ncontract = []\ndefault = ' + ('["contract"]' if contract else '[]') + '\n'
(peer / "Cargo.toml").write_text(manifest)
target_dir = target_dir or peer / "target"
run(label + "-build", ["cargo", "+1.95.0", "build", "--offline", "--release",
"--manifest-path", peer / "Cargo.toml", "--target-dir", target_dir])
if target_dir != peer / "target":
output = peer / "target/release"
output.mkdir(parents=True)
shutil.copy2(target_dir / "release" / binary_name, output / binary_name)
locked = {(p["name"],p["version"]) for p in tomllib.loads((core / "rust/Cargo.lock").read_text())["package"]}
assert all((p["name"],p["version"]) in locked for p in tomllib.loads((peer / "Cargo.lock").read_text())["package"] if "source" in p)
if __name__ == "__main__":
main()