"""Compare a bounded exact-integer model with one pinned upstream Java method. Only the recording expansion is extracted. Histogram storage is replaced by a list; this does not test HdrHistogram binning, percentile APIs or concurrency. Run beside latency-correction.mjs and latency-measurement.mjs. Requires Python 3, Java/Javac and Node, plus HTTPS access to the pinned public-domain source. """ import hashlib import json from datetime import datetime, timezone from pathlib import Path import subprocess import tempfile from urllib.request import urlopen URL = "https://raw.githubusercontent.com/HdrHistogram/HdrHistogram/3ae7ae18d81887484974011c1c7a2bdc9178ab09/src/main/java/org/HdrHistogram/AbstractHistogram.java" EXPECTED = "40020b2a9a19e826d3eae13c18a69b38d96ccb3fbc1ecdc9eb6d749f8e6a0e69" ROOT = Path(__file__).resolve().parent def run(): with urlopen(URL, timeout=30) as response: source = response.read(500000) if hashlib.sha256(source).hexdigest() != EXPECTED: raise ValueError("Pinned source hash mismatch") text = source.decode() start = text.index(" private void recordSingleValueWithExpectedInterval(") end = text.index("\n //", start) method = text[start:end] harness = """import java.util.*; public class CorrectionProbe { private final List entries = new ArrayList<>(); private void recordSingleValue(long value) { entries.add(value); } """ + method + """ public static void main(String[] args) { CorrectionProbe probe = new CorrectionProbe(); for (long interval = 0; interval <= 25; interval++) { for (long value = 0; value <= 200; value++) { probe.entries.clear(); probe.recordSingleValueWithExpectedInterval(value, interval); System.out.println(probe.entries); } } probe.entries.clear(); probe.recordSingleValueWithExpectedInterval(101,10); for(int i=0;i<189;i++) probe.recordSingleValueWithExpectedInterval(1,10); System.out.println(probe.entries); List once = new ArrayList<>(probe.entries); probe.entries.clear(); for(long value : once) probe.recordSingleValueWithExpectedInterval(value,10); System.out.println(probe.entries); } } """ with tempfile.TemporaryDirectory(prefix="hesela-correction-") as temporary: root = Path(temporary) (root / "CorrectionProbe.java").write_text(harness) subprocess.run(["javac", "CorrectionProbe.java"], cwd=root, check=True, timeout=30) native = subprocess.check_output(["java", "-cp", str(root), "CorrectionProbe"], text=True, timeout=30) program = """import {correctExpectedInterval as correct,correctionExample} from './latency-correction.mjs'; for(let interval=0;interval<=25;interval++) for(let value=0;value<=200;value++) console.log(JSON.stringify(correct([value],interval))); const once=correctionExample.rows[1].entries; console.log(JSON.stringify(once)); console.log(JSON.stringify(correct(once,10))); """ model = subprocess.check_output(["node", "--input-type=module", "-e", program], cwd=ROOT, text=True, timeout=30) actual = [json.loads(line) for line in native.splitlines()] expected = [json.loads(line) for line in model.splitlines()] if len(actual) != 5228 or actual != expected: raise ValueError("Upstream expansion and model disagree") files = ["latency-correction.mjs","latency-correction.test.mjs","latency-measurement.mjs","verify-latency-correction.py"] return { "generatedAt": datetime.now(timezone.utc).isoformat(), "sourceLicense": "CC0/public-domain dedication in the upstream file header", "scope": "source-function conformance; not a complete histogram or storage benchmark", "sourceUrl": URL, "sourceSha256": EXPECTED, "comparisons": len(actual), "closedCount": 190, "correctedCount": len(actual[-2]), "twiceCorrectedCount": len(actual[-1]), "artifacts": {name: hashlib.sha256((ROOT/name).read_bytes()).hexdigest() for name in files}, "java": subprocess.check_output(["java","-version"], stderr=subprocess.STDOUT,text=True).splitlines()[0], "node": subprocess.check_output(["node","--version"],text=True).strip(), } if __name__ == "__main__": print(json.dumps(run(),indent=2))