131 lines
6.8 KiB
Python
131 lines
6.8 KiB
Python
"""TCP 同资源责任对照;保留原始裸 Rust、Go 和修改前 Dever,不替换基线。"""
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import statistics
|
|
import tomllib
|
|
|
|
from scale import execute, fingerprint
|
|
|
|
|
|
def measure(core, output, report, binaries):
|
|
def save():
|
|
(output / "results.json").write_text(json.dumps(report, indent=2) + "\n")
|
|
|
|
os.sched_setaffinity(0, {0})
|
|
names = list(binaries)
|
|
save()
|
|
for repeat in range(-1, 7):
|
|
assert all(fingerprint(core / path) == digest for path,digest in report["source_hashes"].items())
|
|
for offset in range(len(names)):
|
|
name = names[(repeat + 1 + offset) % len(names)]
|
|
command = [str(binaries[name])]
|
|
if name in ["dever", "previous"]:
|
|
command += ["main.main.bench", json.dumps(dict(n=report["size"]))]
|
|
else:
|
|
command += [str(report["size"])]
|
|
row = execute(command, output, f"{repeat}-{name}", 60)
|
|
row.update(name=name, repeat=repeat)
|
|
report["samples"].append(row)
|
|
save()
|
|
assert row["exit_code"] == 0 and not row["timeout"], row
|
|
answer = Path(row["stdout"]).read_text()
|
|
assert (json.loads(answer)["data"] if name in ["dever", "previous"] else int(answer)) == report["size"], row
|
|
row["checked"] = True
|
|
save()
|
|
report["medians"] = {name:dict(seconds=statistics.median(r["seconds"] for r in report["samples"] if r["name"]==name and r["repeat"]>=0),
|
|
cpu=statistics.median(r["user_cpu_seconds"]+r["system_cpu_seconds"] for r in report["samples"] if r["name"]==name and r["repeat"]>=0)) for name in names}
|
|
save()
|
|
print(json.dumps(report["medians"], indent=2))
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--compiler", type=Path, required=True)
|
|
parser.add_argument("--baseline", type=Path, required=True,
|
|
help="original loop.py --shape net result directory")
|
|
parser.add_argument("--previous", type=Path, required=True)
|
|
parser.add_argument("--output", type=Path, required=True)
|
|
args = parser.parse_args()
|
|
core = Path(__file__).resolve().parents[2]
|
|
output = args.output.absolute()
|
|
output.mkdir(parents=True, exist_ok=False)
|
|
original_path = args.baseline / "results.json"
|
|
original = json.loads(original_path.read_text())
|
|
report = dict(rounds=7, warmups=1, size=original["size"], cpu_affinity=[0],
|
|
contract="Persistent loopback TCP pair, TCP_NODELAY, a varying byte written/read/verified each iteration, checked count. Resource Rust owns alias-wide immediate socket close, serialized same-direction operations, cancelled-read release, optional total-operation deadlines, and partial-write cancellation that closes aliases before the queued writer proceeds. Uses independent Tokio/bytes/tokio-util, no Dever runtime.",
|
|
limitations="Successful sequential socket workload; not full listener/DNS/AsyncStream or language error/Scope equivalence. Original bare Tokio and Go remain separate results; resource parity cannot erase the gap to the bare peer. Wall/CPU cover process startup/shutdown, not individual-operation tail latency.",
|
|
original_baseline=dict(path=str(original_path.resolve()), sha256=fingerprint(original_path)),
|
|
source_hashes={}, builds=[], samples=[])
|
|
|
|
def save():
|
|
(output / "results.json").write_text(json.dumps(report, indent=2) + "\n")
|
|
|
|
def run(label, command, timeout=900):
|
|
row = execute(list(map(str, command)), output, label, timeout)
|
|
report["builds"].append(row)
|
|
save()
|
|
assert row["exit_code"] == 0 and not row["timeout"], row
|
|
|
|
sources = [Path(__file__), *Path(__file__).parent.glob("net_resource_*.rs"),
|
|
*core.joinpath("rust/src").rglob("*.rs"), *core.joinpath("lib").rglob("*.dv"),
|
|
core / "rust/Cargo.toml", core / "rust/Cargo.lock", core / "rust/src/runtime/Cargo.toml"]
|
|
for source in sources:
|
|
relative = source.relative_to(core)
|
|
report["source_hashes"][str(relative)] = fingerprint(source)
|
|
target = output / "inputs" / relative
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
shutil.copyfile(source, target)
|
|
reference = output / "reference"
|
|
reference.mkdir()
|
|
for source in Path(__file__).parent.glob("net_resource_*.rs"):
|
|
shutil.copyfile(source, reference / source.name)
|
|
manifest = '''[package]
|
|
name = "net-resource-reference"
|
|
version = "0.0.1"
|
|
edition = "2024"
|
|
[[bin]]
|
|
name = "net-resource-reference"
|
|
path = "net_resource_reference.rs"
|
|
[dependencies]
|
|
bytes = "=1.12.1"
|
|
tokio = { version = "=1.53.1", features = ["rt", "net", "io-util", "time", "macros", "sync"] }
|
|
tokio-util = { version = "=0.7.19", features = ["rt"] }
|
|
futures-util = { version = "=0.3.34", default-features = false, features = ["std", "async-await"] }
|
|
[profile.release]
|
|
lto = "fat"
|
|
codegen-units = 1
|
|
overflow-checks = true
|
|
strip = "symbols"
|
|
'''
|
|
(reference / "Cargo.toml").write_text(manifest)
|
|
shutil.copyfile(core / "rust/Cargo.lock", reference / "Cargo.lock")
|
|
cargo = ["cargo", "+1.95.0"]
|
|
common = ["--manifest-path", reference / "Cargo.toml", "--offline", "--target-dir", reference / "target"]
|
|
run("reference-test", [*cargo, "test", *common])
|
|
run("reference-clippy", [*cargo, "clippy", *common, "--locked", "--all-targets", "--", "-D", "warnings"])
|
|
run("reference-build", [*cargo, "build", *common, "--locked", "--release"])
|
|
locked = {(p["name"], p["version"], p.get("source")) for p in tomllib.loads((core / "rust/Cargo.lock").read_text())["package"]}
|
|
actual = tomllib.loads((reference / "Cargo.lock").read_text())["package"]
|
|
assert all(not p.get("source") or (p["name"], p["version"], p["source"]) in locked for p in actual)
|
|
report["reference_lock_sha256"] = fingerprint(reference / "Cargo.lock")
|
|
source = args.baseline / "net-dever/main.dever"
|
|
run("dever-build", [args.compiler, "build", source, "--output", output / "dever"])
|
|
shutil.copyfile(source, output / "workload.dever")
|
|
report["compiler_sha256"] = fingerprint(args.compiler)
|
|
binaries = dict(dever=output / "dever", previous=args.previous.resolve(strict=True),
|
|
resource=reference / "target/release/net-resource-reference")
|
|
for name in ["rust", "go"]:
|
|
info = next(row for row in original["builds"] if row["language"] == name and row["shape"] == "net")
|
|
binary = args.baseline / ("net-rust/target/release/collection-reference" if name == "rust" else "net-go/program")
|
|
assert fingerprint(binary) == info["binary_sha256"], "original reference changed"
|
|
binaries[name] = binary
|
|
report["binaries"] = {name:dict(path=str(path),sha256=fingerprint(path)) for name,path in binaries.items()}
|
|
measure(core, output, report, binaries)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|