#!/usr/bin/env python3
"""The memory numbers (brief 0110): a made-up life, built, swept and asked. No network, no model.

    engine/tests/memory-bench               the made-up folder (about 5,500 files)
    engine/tests/memory-bench --scale 3     the stress copy (about 19,000 files)
    engine/tests/memory-bench --why        also list the questions whose file came second or lower
    engine/tests/memory-bench boost:named=1  one boost changed, to compare (see memory's BOOST)
    engine/tests/memory-bench --keep        keep the temporary folder and print where it is

Everything happens in a new temporary folder (tests/madeup.py writes it);
nothing real is read. Prints the build, sweep and one-file times, the size on
disk, the time a question takes, and how many questions find their file in
the first five: by kind (close, para, id, ar), with the plain lookup and the
Arabic folding, and without them (the 0109 spike's way) to compare; and,
when argoni-embed is there (brief 0111), meaning alone and the mixes at
several weights, with the build's vector time and CPU time.
Standard library only.
"""
import json
import os
import re
import resource
import shutil
import sqlite3
import subprocess
import sys
import tempfile
import time

sys.dont_write_bytecode = True
TESTS = os.path.dirname(os.path.abspath(__file__))
ENGINE = os.path.dirname(TESTS)
BIN = os.path.join(ENGINE, "bin")
sys.path.insert(0, TESTS)
import madeup  # noqa: E402

PY = sys.executable


def run(env, *args):
    t = time.time()
    p = subprocess.run([PY, os.path.join(BIN, "memory")] + list(args), capture_output=True, text=True, env=env)
    if p.returncode:
        sys.exit("memory %s exited %d: %s" % (args[0], p.returncode, (p.stderr or p.stdout)[-300:]))
    return p.stdout, time.time() - t


def first_five(res):
    out = []
    for r in res["lookup"] + res["found"]:
        if r.get("file") and r["file"] not in out:
            out.append(r["file"])
    return out[:5]


def plain_words(db, question, k=5):
    """The spike's way: raw words OR'ed, bm25, no lookup, no Arabic folding, no boosts."""
    words = [w for w in re.findall(r"[\w']+", question.lower()) if w not in STOP and len(w) > 1]
    if not words:
        return []
    m = "{title heading text} : (%s)" % " OR ".join('"%s"' % w for w in words)
    rows = db.execute("select f.path from fts join sections s on s.id = fts.rowid join files f on f.id = s.file"
                      " where fts match ? order by bm25(fts, 0.0, 4.0, 2.0, 1.0, 0.0) limit 50", (m,)).fetchall()
    out = []
    for (p,) in rows:
        if p not in out:
            out.append(p)
    return out[:k]


STOP = None
WEIGHTS = (0.5, 1.0, 2.0, 3.0)


def main(argv):
    global STOP
    scale = float(argv[argv.index("--scale") + 1]) if "--scale" in argv else 1.0
    tmp = tempfile.mkdtemp(prefix="argoni-memory-bench-")
    try:
        folder = os.path.join(tmp, "folder")
        subprocess.run([PY, os.path.join(BIN, "new-folder"), folder], check=True, capture_output=True)
        life = os.path.join(folder, "data")
        repos = os.path.join(tmp, "repos")
        t = time.time()
        files, qs = madeup.gen(life, repos, scale)
        gen_s = time.time() - t
        cfg_path = os.path.join(life, "config.json")
        cfg = json.load(open(cfg_path))
        cfg["repos_folders"] = [repos]
        json.dump(cfg, open(cfg_path, "w"), indent=2)
        env = {k: v for k, v in os.environ.items() if not k.startswith(("LIFE_FOLDER", "ENGINE_DIR"))}
        env.update(LIFE_FOLDER=life, ENGINE_DIR=ENGINE, ARGONI_SCAN_HOME=os.path.join(tmp, "home"))
        ru0 = resource.getrusage(resource.RUSAGE_CHILDREN)
        out, build_s = run(env, "build", "--wait")
        ru1 = resource.getrusage(resource.RUSAGE_CHILDREN)
        status = json.loads(run(env, "status", "--json")[0])
        _, sweep_s = run(env, "update")
        one = os.path.join(life, "projects", "pantry.md")
        with open(one, "a") as fh:
            fh.write("\n## Bench\n\nA line added by the bench.\n")
        _, one_s = run(env, "update", one)
        with open(one, "a") as fh:
            fh.write("\nAnother line.\n")
        _, sweep1_s = run(env, "update", "--wait")
        # Load the script once and time questions in-process, the way the helper will.
        sys.argv = ["memory"]
        os.environ.update(LIFE_FOLDER=life, ENGINE_DIR=ENGINE, ARGONI_SCAN_HOME=env["ARGONI_SCAN_HOME"])
        import importlib.machinery
        import importlib.util
        loader = importlib.machinery.SourceFileLoader("memory", os.path.join(BIN, "memory"))
        spec = importlib.util.spec_from_loader("memory", loader)
        mem = importlib.util.module_from_spec(spec)
        loader.exec_module(mem)
        STOP = mem.STOP
        for kv in [a for a in argv if a.startswith("boost:")]:   # boost:named=1.0, to compare
            key, val = kv[6:].split("=")
            mem.BOOST[key] = float(val)
        db = mem.connect()
        st = mem.settings()
        cfg = mem.lifecfg.config(life)
        meaning_on = mem.meaning_state(db, st)
        modes = ["spike way", "words", "meaning"] + ["mix %g" % w for w in WEIGHTS] if meaning_on[0] else \
            ["spike way", "words"]
        score = {m: {} for m in modes}
        times, mtimes, misses = [], [], []
        for q in qs:
            want = os.path.realpath(madeup.where(q["file"]))
            t = time.time()
            res = mem.find(db, q["q"], meaning=False)
            times.append(time.time() - t)
            got = {"words": [os.path.realpath(p) for p in first_five(res)],
                   "spike way": [os.path.realpath(p) for p in plain_words(db, q["q"])]}
            if meaning_on[0]:
                t = time.time()
                near = mem.meaning_search(q["q"])
                mtimes.append(time.time() - t)
                looked = [r for r in res["lookup"] if r.get("file")]
                words = mem.word_search(db, q["q"], mem.WIDE, cfg)
                marks = mem.terms(q["q"])[1]
                alone = mem.fuse(db, [], near, marks)
                got["meaning"] = [os.path.realpath(p) for p in first_five({"lookup": looked, "found": alone})]
                for w in WEIGHTS:
                    mix = mem.fuse(db, words, near, marks, w)
                    got["mix %g" % w] = [os.path.realpath(p) for p in first_five({"lookup": looked, "found": mix})]
            for m in modes:
                k = score[m].setdefault(q["kind"], [0, 0, 0])
                k[0] += want in got[m]
                k[1] += bool(got[m]) and got[m][0] == want
                k[2] += 1
            best = "mix %g" % mem.MEANING_WEIGHT if meaning_on[0] else "words"
            if want not in got[best]:
                misses.append("%s: %s" % (q["kind"], q["q"]))
            elif got[best][0] != want and "--why" in argv:
                misses.append("(second or lower, %s: %s; first was %s)" % (q["kind"], q["q"], os.path.basename(got[best][0])))
        times.sort()
        mtimes.sort()
        print("made-up folder, scale %g: %d files written in %.1f s" % (scale, files, gen_s))
        print("index: %d files, %d sections, %d vectors, %.1f MB on disk" % (
            status["files"], status["sections"], status.get("vectors", 0), status["mb"]))
        print("build %.1f s in all (%s); CPU %.0f s user + %.0f s system" % (
            build_s, "; ".join(l.split(": ", 1)[-1] for l in out.strip().splitlines()),
            ru1.ru_utime - ru0.ru_utime, ru1.ru_stime - ru0.ru_stime))
        print("sweep, nothing changed %.2f s; one file (update FILE) %.2f s; sweep and vectors, one changed %.2f s"
              % (sweep_s, one_s, sweep1_s))
        print("a question, words (in-process): median %.1f ms, slowest %.1f ms" % (
            times[len(times) // 2] * 1000, times[-1] * 1000))
        if mtimes:
            print("a question, meaning (argoni-embed search, a new process each): median %.0f ms, slowest %.0f ms"
                  % (mtimes[len(mtimes) // 2] * 1000, mtimes[-1] * 1000))
        print("%s" % meaning_on[1])
        print("\nright file in the first five (and in first place), %d questions:" % len(qs))
        kinds = [k for k in ("close", "para", "id", "ar", "aren") if any(k in score[m] for m in modes)]
        print("  %-10s" % "" + "".join("%11s" % k for k in kinds + ["all"]))
        for m in modes:
            row, tot = "", [0, 0, 0]
            for k in kinds:
                a, f, n = score[m].get(k, [0, 0, 0])
                tot = [tot[0] + a, tot[1] + f, tot[2] + n]
                row += "%11s" % ("%d/%d (%d)" % (a, n, f))
            row += "%11s" % ("%d/%d (%d)" % (tot[0], tot[2], tot[1]))
            print("  %-10s%s" % (m, row))
        if misses:
            print("\nmissed:\n  " + "\n  ".join(misses))
    finally:
        if "--keep" in argv:
            print("kept: %s" % tmp)
        else:
            shutil.rmtree(tmp, ignore_errors=True)
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
