#!/usr/bin/env python3
"""Side-by-side benchmark: tachyon-core vs sing-box (stdlib only, Linux only).

Every binary gets the same minimal config (one `mixed` inbound on loopback,
one `direct` outbound) and the same load, generated by this script through the
SOCKS5 side of the mixed inbound. The proxy is pinned to PROXY_CPUS, the load
generator and the sink to the remaining cores, so the numbers answer "what does
this binary do on N router-sized cores", not "how fast is the harness".

A `direct` row (no proxy at all) is measured first: it is the ceiling of the
harness itself and the zero point for the latency delta.

    bench.py --bin tachyon-core=/bench/tachyon-core \
             --bin sing-box=/bench/sing-box --out result.json
"""
import argparse
import asyncio
import json
import multiprocessing
import os
import socket
import statistics
import struct
import subprocess
import sys
import tempfile
import threading
import time

SINK_PORT = 45001
PROXY_PORT = 45002
CHUNK = 1 << 20

MODE_HOLD, MODE_ECHO, MODE_SOURCE, MODE_SINK = b"H", b"E", b"S", b"K"


# ---------------------------------------------------------------- steer env
def ensure_steer_env(bin_path):
    """Sets up musl libc symlink, steerd/steer in /usr/sbin, libsteer in /usr/lib."""
    try:
        if not os.path.exists("/lib/ld-musl-x86_64.so.1"):
            subprocess.run(["apt-get", "update", "-qq"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
            subprocess.run(["apt-get", "install", "-y", "-qq", "musl"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

        os.makedirs("/usr/sbin", exist_ok=True)
        os.makedirs("/usr/lib", exist_ok=True)

        if os.path.exists("/lib/ld-musl-x86_64.so.1") and not os.path.exists("/usr/lib/libc.so"):
            try:
                os.symlink("/lib/ld-musl-x86_64.so.1", "/usr/lib/libc.so")
            except OSError:
                pass

        bin_dir = os.path.dirname(os.path.abspath(bin_path))
        steer_base = os.path.dirname(bin_dir)

        for name in ("steerd", "steer"):
            for c in (os.path.join(bin_dir, "..", "sbin", name),
                      os.path.join(steer_base, "sbin", name),
                      os.path.join(os.path.dirname(steer_base), "usr", "sbin", name),
                      os.path.join(bin_dir, name)):
                if os.path.exists(c):
                    try:
                        os.chmod(c, 0o755)
                    except OSError:
                        pass
                    dst = f"/usr/sbin/{name}"
                    if not os.path.exists(dst):
                        try:
                            os.symlink(os.path.abspath(c), dst)
                        except OSError:
                            pass
                    break

        for ldir in (os.path.join(bin_dir, "..", "lib"),
                     os.path.join(steer_base, "lib"),
                     os.path.join(os.path.dirname(steer_base), "usr", "lib"),
                     os.path.join(bin_dir, "lib")):
            if os.path.exists(ldir):
                for f in os.listdir(ldir):
                    dst = os.path.join("/usr/lib", f)
                    if not os.path.exists(dst):
                        try:
                            os.symlink(os.path.abspath(os.path.join(ldir, f)), dst)
                        except OSError:
                            pass
    except Exception as e:
        print(f"Warning: ensure_steer_env error: {e}", file=sys.stderr)


# ---------------------------------------------------------------- sink server
def run_sink(cpus):
    os.sched_setaffinity(0, cpus)
    # один буфер на все коннекты-источники: своя копия на коннект (1 МиБ)
    # при 750 источниках съедала 750 МиБ и питон уходил в OOM
    src_buf = b"\0" * CHUNK

    async def handle(r, w):
        try:
            mode = await r.readexactly(1)
            if mode == MODE_HOLD or mode == MODE_SINK:
                while await r.read(CHUNK):
                    pass
            elif mode == MODE_ECHO:
                while True:
                    w.write(await r.readexactly(64))
                    await w.drain()
            elif mode == MODE_SOURCE:
                buf = src_buf
                while True:
                    w.write(buf)
                    await w.drain()
        except (asyncio.IncompleteReadError, ConnectionError, OSError):
            pass
        finally:
            w.close()

    async def main():
        srv = await asyncio.start_server(handle, "127.0.0.1", SINK_PORT, backlog=4096)
        async with srv:
            await srv.serve_forever()

    asyncio.run(main())


# ------------------------------------------------------------------ SOCKS5 io
def socks_connect(proxy, mode):
    """Blocking connect to the sink, via the proxy when `proxy` is set."""
    s = socket.create_connection(("127.0.0.1", PROXY_PORT if proxy else SINK_PORT), timeout=30)
    s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    if proxy:
        s.sendall(b"\x05\x01\x00")
        assert s.recv(2) == b"\x05\x00"
        s.sendall(b"\x05\x01\x00\x01" + socket.inet_aton("127.0.0.1") + struct.pack(">H", SINK_PORT))
        reply = recvn(s, 10)
        assert reply[1] == 0, f"socks connect refused: {reply!r}"
    s.sendall(mode)
    return s


def recvn(s, n):
    out = b""
    while len(out) < n:
        chunk = s.recv(n - len(out))
        if not chunk:
            raise ConnectionError("eof")
        out += chunk
    return out


async def async_socks_connect(proxy, mode):
    host, port = "127.0.0.1", PROXY_PORT if proxy else SINK_PORT
    # limit — потолок внутреннего буфера StreamReader на коннект (дефолт 64 КиБ,
    # при тысяче коннектов это сотни мегабайт на стороне питона)
    r, w = await asyncio.open_connection(host, port, limit=32768)
    if proxy:
        w.write(b"\x05\x01\x00")
        assert await r.readexactly(2) == b"\x05\x00"
        w.write(b"\x05\x01\x00\x01" + socket.inet_aton("127.0.0.1") + struct.pack(">H", SINK_PORT))
        reply = await r.readexactly(10)
        assert reply[1] == 0, f"socks connect refused: {reply!r}"
    w.write(mode)
    return r, w


# ------------------------------------------------------------------ proc info
def proc_status(pid):
    out = {}
    with open(f"/proc/{pid}/status") as f:
        for line in f:
            if line.startswith(("VmRSS", "VmHWM")):
                k, v = line.split(":")
                out[k] = int(v.split()[0]) / 1024  # MiB
    return out


def cpu_split(pid):
    """(user, system) seconds of a process from /proc/<pid>/stat."""
    with open(f"/proc/{pid}/stat") as f:
        fields = f.read().rsplit(")", 1)[1].split()
    tick = os.sysconf("SC_CLK_TCK")
    return int(fields[11]) / tick, int(fields[12]) / tick


def proc_faults(pid):
    """(minor, major) page faults of a process from /proc/<pid>/stat."""
    with open(f"/proc/{pid}/stat") as f:
        fields = f.read().rsplit(")", 1)[1].split()
    return int(fields[7]), int(fields[9])


def cpu_seconds(pid):
    user, sys = cpu_split(pid)
    return user + sys


# -------------------------------------------------------------------- phases
def measure_latency(proxy, rounds=20000):
    s = socks_connect(proxy, MODE_ECHO)
    msg = b"x" * 64
    for _ in range(100):  # warm-up
        s.sendall(msg)
        recvn(s, 64)
    samples = []
    for _ in range(rounds):
        t = time.perf_counter_ns()
        s.sendall(msg)
        # the content has to be checked: a proxy that answers early with its own
        # framing bytes instead of the sink's echo still fills 64 bytes, and the
        # round trip then reads as 10 us instead of 300
        assert recvn(s, 64) == msg, "echo did not come back intact"
        samples.append((time.perf_counter_ns() - t) / 1000)
    s.close()
    samples.sort()
    return {
        "p50_us": samples[len(samples) // 2],
        "p99_us": samples[int(len(samples) * 0.99)],
        "p999_us": samples[int(len(samples) * 0.999)],
    }


def measure_throughput(proxy, streams, seconds, pid):
    """Download direction: the sink pushes, we read. Returns Gbit/s and CPU cost."""
    total = [0] * streams
    stop = threading.Event()

    def worker(i):
        s = socks_connect(proxy, MODE_SOURCE)
        buf = bytearray(CHUNK)
        view = memoryview(buf)
        while not stop.is_set():
            n = s.recv_into(view)
            if not n:
                break
            total[i] += n
        s.close()

    threads = [threading.Thread(target=worker, args=(i,)) for i in range(streams)]
    u0, s0 = cpu_split(pid) if pid else (0.0, 0.0)
    t0 = time.perf_counter()
    for t in threads:
        t.start()
    time.sleep(seconds)
    got = sum(total)
    elapsed = time.perf_counter() - t0
    u1, s1 = cpu_split(pid) if pid else (0.0, 0.0)
    stop.set()
    for t in threads:
        t.join(timeout=10)
    gib = got / (1 << 30)
    per_gib = (lambda x: x / gib if pid and gib else None)
    return {
        "gbit_s": got * 8 / elapsed / 1e9,
        "gib": gib,
        "proxy_cpu_s_per_gib": per_gib((u1 + s1) - (u0 + s0)),
        "proxy_user_s_per_gib": per_gib(u1 - u0),
        "proxy_sys_s_per_gib": per_gib(s1 - s0),
    }


def measure_bulk(proxy, streams, gib, pid, timeout=200):
    """Fixed piece of work: `gib` GiB split across `streams` connections, timed
    from the first byte to the last.

    measure_throughput answers "how fast does it go for N seconds"; this answers
    "how long does a 2 GiB file take" -- the number a user actually waits on,
    and it cannot hide a stall behind a short window. The transfer must complete:
    an early EOF or a stuck stream is an error, never a partial result that
    reads as slow.
    """
    each = int(gib * (1 << 30)) // streams
    total = [0] * streams
    errs = []

    def worker(i):
        try:
            s = socks_connect(proxy, MODE_SOURCE)
            view = memoryview(bytearray(CHUNK))
            local, left = 0, each
            while left:
                n = s.recv_into(view, min(CHUNK, left))
                if not n:
                    break
                local += n
                left -= n
                total[i] = local
            s.close()
            if left:
                errs.append(f"stream {i}: eof after {each - left}/{each} bytes")
        except Exception as e:  # noqa: BLE001 - every failure kills the cell
            errs.append(f"stream {i}: {type(e).__name__}: {e}")

    threads = [threading.Thread(target=worker, args=(i,)) for i in range(streams)]
    u0, s0 = cpu_split(pid) if pid else (0.0, 0.0)
    t0 = time.perf_counter()
    for t in threads:
        t.start()
    deadline = time.perf_counter() + timeout
    for t in threads:
        t.join(max(0.0, deadline - time.perf_counter()))
    elapsed = time.perf_counter() - t0
    u1, s1 = cpu_split(pid) if pid else (0.0, 0.0)
    stuck = sum(1 for t in threads if t.is_alive())
    if errs or stuck:
        raise RuntimeError(
            "bulk transfer incomplete: "
            + ("; ".join(errs) if errs else f"{stuck} stream(s) stuck >{timeout}s")
        )
    got = sum(total)
    per_gib = (lambda x: x / gib if pid and gib else None)
    return {
        "gbit_s": got * 8 / elapsed / 1e9,
        "seconds": elapsed,
        "gib": got / (1 << 30),
        "proxy_cpu_s_per_gib": per_gib((u1 + s1) - (u0 + s0)),
        "proxy_user_s_per_gib": per_gib(u1 - u0),
        "proxy_sys_s_per_gib": per_gib(s1 - s0),
    }


def hold_connections(proxy, count, pid):
    """Open `count` idle proxied connections and report the proxy's RSS while held."""

    async def run():
        conns = []
        for batch in range(0, count, 250):
            got = await asyncio.gather(
                *(async_socks_connect(proxy, MODE_HOLD) for _ in range(min(250, count - batch)))
            )
            conns.extend(got)
        await asyncio.sleep(2)  # let the proxy settle before sampling
        rss = proc_status(pid)["VmRSS"] if pid else None
        for _, w in conns:
            w.close()
        await asyncio.sleep(2)
        return rss

    return asyncio.run(run())


# ------------------------------------------------------------------ binaries
def write_config(path):
    cfg = {
        "log": {"level": "error"},
        "inbounds": [{"type": "mixed", "tag": "in", "listen": "127.0.0.1", "listen_port": PROXY_PORT}],
        "outbounds": [{"type": "direct", "tag": "direct"}],
        "route": {"final": "direct"},
    }
    with open(path, "w") as f:
        json.dump(cfg, f)


def wait_listen(port, timeout=30):
    t0 = time.perf_counter()
    while time.perf_counter() - t0 < timeout:
        try:
            socket.create_connection(("127.0.0.1", port), timeout=0.2).close()
            return time.perf_counter() - t0
        except OSError:
            time.sleep(0.002)
    raise TimeoutError(f"nothing listening on {port}")


def bench_binary(name, path, cfg, proxy_cpus, args):
    res = {"name": name, "size_mib": os.path.getsize(path) / (1 << 20)}

    ver = subprocess.run([path, "version"], capture_output=True, text=True).stdout.splitlines()
    res["version"] = ver[0] if ver else "?"

    times = []
    for _ in range(5):
        t0 = time.perf_counter()
        subprocess.run([path, "check", "-c", cfg], capture_output=True, check=True)
        times.append((time.perf_counter() - t0) * 1000)
    res["check_ms"] = statistics.median(times)

    starts, proc = [], None
    for _ in range(5):
        if proc:
            proc.terminate()
            proc.wait()
        t0 = time.perf_counter()
        proc = subprocess.Popen(
            [path, "run", "-c", cfg],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            preexec_fn=lambda: os.sched_setaffinity(0, proxy_cpus),
        )
        wait_listen(PROXY_PORT)
        starts.append((time.perf_counter() - t0) * 1000)
    res["start_ms"] = statistics.median(starts)

    try:
        time.sleep(1 if args.quick else 5)
        res["idle_rss_mib"] = proc_status(proc.pid)["VmRSS"]
        if args.quick:
            for streams in (1, 8):
                r = measure_throughput(True, streams, args.seconds, proc.pid)
                res[f"tput_{streams}"] = r
            res["rss_1000_conns_mib"] = hold_connections(True, 1000, proc.pid)
            res["rss_10000_conns_mib"] = hold_connections(True, 10000, proc.pid)
            return res
        lat = [measure_latency(True, args.latency_rounds) for _ in range(3)]
        res["latency"] = {k: statistics.median(x[k] for x in lat) for k in lat[0]}
        for streams in (1, 8):
            runs = [measure_throughput(True, streams, args.seconds, proc.pid) for _ in range(3)]
            res[f"tput_{streams}"] = {
                "gbit_s": statistics.median(r["gbit_s"] for r in runs),
                "proxy_cpu_s_per_gib": statistics.median(r["proxy_cpu_s_per_gib"] for r in runs),
            }
        for n in (1000, 10000):
            res[f"rss_{n}_conns_mib"] = hold_connections(True, n, proc.pid)
        res["peak_rss_mib"] = proc_status(proc.pid)["VmHWM"]
    finally:
        proc.terminate()
        proc.wait()
    return res


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--bin", action="append", required=True, metavar="NAME=PATH")
    ap.add_argument("--proxy-cpus", default="0-1", help="cores the proxy is pinned to")
    ap.add_argument("--seconds", type=int, default=5)
    ap.add_argument("--latency-rounds", type=int, default=20000)
    ap.add_argument("--quick", action="store_true", help="throughput + RSS only, single run")
    ap.add_argument("--out", default="bench-result.json")
    args = ap.parse_args()

    def parse_cpus(spec):
        out = set()
        for part in spec.split(","):
            lo, _, hi = part.partition("-")
            out.update(range(int(lo), int(hi or lo) + 1))
        return out

    proxy_cpus = parse_cpus(args.proxy_cpus)
    load_cpus = set(range(os.cpu_count())) - proxy_cpus or proxy_cpus
    os.sched_setaffinity(0, load_cpus)

    sink = multiprocessing.Process(target=run_sink, args=(load_cpus,), daemon=True)
    sink.start()
    wait_listen(SINK_PORT)

    cfg = os.path.join(tempfile.mkdtemp(), "config.json")
    write_config(cfg)

    out = {
        "cpu": next((l.split(":")[1].strip() for l in open("/proc/cpuinfo") if l.startswith("model name")), "?"),
        "proxy_cpus": args.proxy_cpus,
        "kernel": os.uname().release,
        "direct": {
            "latency": measure_latency(False, args.latency_rounds),
            "tput_1": measure_throughput(False, 1, args.seconds, None),
            "tput_8": measure_throughput(False, 8, args.seconds, None),
        },
        "results": [],
    }
    for spec in args.bin:
        name, _, path = spec.partition("=")
        print(f"== {name}", flush=True)
        out["results"].append(bench_binary(name, path, cfg, proxy_cpus, args))
        print(json.dumps(out["results"][-1], indent=2), flush=True)

    with open(args.out, "w") as f:
        json.dump(out, f, indent=2)


if __name__ == "__main__":
    main()
