#!/usr/bin/env python3
"""Recompute publication summaries and check the archived raw runs."""

import json
from pathlib import Path
import runpy
import subprocess
import sys


ROOT = Path(__file__).resolve().parents[1]
PUBLICATION = ROOT / "bench/results/publication"
SCRIPTS = ROOT / "scripts"


def require(condition, message):
    if not condition:
        raise SystemExit(message)


def load(path):
    return json.loads(path.read_text())


def verify_replay(expected, executable, *args):
    require(subprocess.check_output([executable, *args]) == expected.read_bytes(),
            f"Recomputed output differs from {expected.relative_to(ROOT)}")


def check_warmed_panel(directory, raw_directory, count, last, targets, paired):
    report = raw_directory / "lookup-results.json"
    summary = directory / "steady-summary.json"
    rows = load(report)["trials"]
    runner = runpy.run_path(SCRIPTS / "benchmark-lookup")
    require(len(rows) == count
            and len({(row["trial"], row["profile"]) for row in rows}) == count,
            f"{directory.name}: unexpected number of unique processes")
    expected_checksums = None
    for row in rows:
        stem = raw_directory / f"{row['trial']}-{row['profile']}"
        raw = load(stem.with_suffix(".stdout"))
        trace = stem.with_suffix(".stderr").read_text()
        jit_targets, jit_events = runner["jit_trace"](trace)
        compilation_ms = sum(map(int, runner["COMPILE_TIME"].findall(trace)))
        require(row["jit_targets"] == jit_targets
                and row["logged_guest_compilation_ms"] == compilation_ms,
                f"{stem.name}: raw compiler events differ from report")
        for field, value in raw.items():
            if field != "lookup_windows":
                require(row[field] == value, f"Raw {stem.name} differs for {field}")
        reported_windows = row["lookup_windows"]
        require(len(raw["lookup_windows"]) == len(reported_windows),
                f"{stem.name}: unexpected number of windows")
        for recorded, reported in zip(raw["lookup_windows"], reported_windows):
            events = runner["jit_status"](
                jit_targets, jit_events, recorded["measurement_start_unix_ns"], recorded["lookup_ns"]
            )
            require(reported == {**recorded, **events},
                    f"{stem.name}: raw timing window or compiler metadata differs from report")
        checksums = (raw["warmup_checksum"], *(w["checksum"] for w in reported_windows))
        if expected_checksums is None:
            expected_checksums = checksums
        require(checksums == expected_checksums, f"{stem.name}: output checksum mismatch")
    options = ["--last", str(last)]
    for target in targets:
        options.extend(("--target", target))
    verify_replay(summary, SCRIPTS / "summarize-warm-windows", report, *options)
    for reference, filename in paired.items():
        verify_replay(directory / filename, SCRIPTS / "summarize-lookup-pairs",
                      report, summary, "--reference", reference)
    print(f"{directory.name}: {count} processes, {count * last} verified warm windows")


def check_speedtest():
    directory = PUBLICATION / "speedtest-confirmation"
    report = load(directory / "report.json")
    runner = runpy.run_path(SCRIPTS / "benchmark-speedtest")
    expected_profiles = ("native-o0", "native-o2", "libffi", "panama", "typed-jni")
    testsets = ("main", "cte", "orm")
    rows = report["trials"]
    planned = {(testset, profile, trial) for testset in testsets
               for profile in expected_profiles for trial in range(1, 4)}
    observed = {(row["testset"], row["profile"], row["trial"]) for row in rows}
    require(len(rows) == len(planned) and observed == planned,
            "speedtest1: unexpected set of processes")
    for row in rows:
        output_dir = directory / row["logs"]
        stdout = (output_dir / "stdout.log").read_text()
        trace = (output_dir / "stderr.log").read_text()
        vdbe_compilations = [{"tier": int(tier), "code_bytes": int(size)}
                             for tier, size in runner["VDBE_DONE"].findall(trace)]
        graph_bailouts = len(runner["VDBE_SIZE_BAILOUT"].findall(trace))
        require(vdbe_compilations == row["vdbe_entry_compilations"]
                and graph_bailouts == row["vdbe_graph_size_bailouts"],
                f"speedtest1: raw compiler events differ for {row['logs']}")
        digest = runner["VERIFY_HASH"].search(stdout)
        total = runner["TOTAL"].search(stdout)
        cases = [{"case": int(case), "name": name.rstrip("."), "seconds": float(seconds)}
                 for case, name, seconds in runner["TEST_TIME"].findall(stdout)]
        require(digest and total
                and int(digest[1]) == row["verification_count"]
                and digest[2] == row["verification_hash"]
                and float(total[1]) == row["speedtest_seconds"]
                and cases == row["speedtest_cases"],
                f"speedtest1: raw output differs from report for {row['logs']}")
    expected = report["summary"]
    require(report["verification_matches"], "speedtest1: reported mismatch")
    for testset in testsets:
        summary = runner["summarise_testset"](
            [row for row in rows if row["testset"] == testset], expected_profiles
        )
        require(summary["verification_matches"] and summary == expected[testset],
                f"speedtest1: recomputed summary differs for {testset}")
    print("speedtest1: 45 processes matched native counts, digests and case order")


def main():
    lookup = PUBLICATION / "lookup-replication"
    references = ("graal-libffi", "graal-panama", "native-stock", "native-llvm-matched")
    require(load(lookup / "lookup-results.json")["order_seed"] == 2410, "Unexpected lookup seed")
    check_warmed_panel(lookup, lookup, 30, 5, (),
                       {reference: f"paired-{reference}.json" for reference in references})
    mutex = PUBLICATION / "mutex-pairs"
    check_warmed_panel(mutex, mutex / "trials", 24, 5, ("mutex_pairs",),
                       {"graal-libffi": "mutex-paired-libffi.json",
                        "graal-panama": "mutex-paired-panama.json"})
    overlays = PUBLICATION / "overlay-control"
    check_warmed_panel(overlays, overlays, 18, 5, ("mutex_pairs",),
                       {"graal-pristine": "paired-summary.json"})
    check_speedtest()
    accounting = PUBLICATION / "threadtest/diagnosis"
    verify_replay(accounting / "accounting.json", sys.executable,
                  ROOT / "runtime-patches/threadtest/account-missing.py",
                  accounting / "traced-aot-interpreted-2s.stdout",
                  accounting / "traced-aot-interpreted-2s.stderr")
    data = load(accounting / "accounting.json")
    require(len(data["busy_inserts"]) == 6 and len(data["unattempted_values"]) == 21
            and not data["unexplained_missing_values"] and not data["predicted_values_not_missing"],
            "threadtest5: unexpected missing-row accounting")
    probe = PUBLICATION / "aot/jni-smoke/result-quick"
    expected = b"adapter JNI bridge contract passed\n"
    require((probe / "native-image.stdout").read_bytes() == expected
            and (probe / "jvm.stdout").read_bytes() == expected,
            "JNI smoke: archived JVM and Native Image outputs differ")
    print("threadtest5: 27 missing rows accounted for")
    print("JNI bridge probe: archived JVM and Native Image outputs match")


if __name__ == "__main__":
    main()
