diff --git a/BENCH.bat b/BENCH.bat new file mode 100644 index 00000000..8d1ed0e8 --- /dev/null +++ b/BENCH.bat @@ -0,0 +1,16 @@ +@echo off +rem Benchmark this Strata install the way it is used and write a report you can share (docs/COMMUNITY_BENCHMARKS.md). +rem It starts the model with your settings (or uses the one already running), takes about 10 minutes and changes nothing. +rem Options: --clients 4 (several requests at once), --long (a 32K prompt), --compare . +setlocal +title Strata - benchmark +cd /d "%~dp0" +if not exist ".venv\Scripts\python.exe" ( + echo Strata is not set up here yet: run START-HERE.bat first. + pause + exit /b 1 +) +".venv\Scripts\python.exe" tools\strata_bench.py %* +set "RC=%ERRORLEVEL%" +pause +exit /b %RC% diff --git a/README.md b/README.md index a8184b66..f25b40e8 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,8 @@ download stops, run it again: it continues where it left off. Your browser opens its window to stop the model. `UPDATE.bat` (`./update.sh`) updates Strata without starting it. Updating, Docker, several cards, where the files go and every option: [docs/INSTALL.md](docs/INSTALL.md). +**Is it fast on your PC?** Run `BENCH.bat` (`./bench.sh`): about 10 minutes, it measures chat, 4K/16K prompts and an agent-style session with your own settings and writes a report to share ([how](docs/COMMUNITY_BENCHMARKS.md#run-the-benchmark)). + ## Which model should I pick? The installer recommends one for your RAM. The same model comes in several sizes, compressed more or less. Smaller diff --git a/bench.sh b/bench.sh new file mode 100755 index 00000000..de0e451a --- /dev/null +++ b/bench.sh @@ -0,0 +1,10 @@ +#!/bin/sh +# Benchmark this Strata install the way it is used and write a report you can share (docs/COMMUNITY_BENCHMARKS.md). +# It starts the model with your settings (or uses the one already running), takes about 10 minutes and changes nothing. +# Options: --clients 4 (several requests at once), --long (a 32K prompt), --compare . +cd "$(dirname "$0")" || exit 1 +if [ -x .venv/bin/python ]; then + exec .venv/bin/python tools/strata_bench.py "$@" +fi +echo "Strata is not set up here yet: run ./setup.sh first." +exit 1 diff --git a/docs/COMMUNITY_BENCHMARKS.md b/docs/COMMUNITY_BENCHMARKS.md index c3460fca..6f639605 100644 --- a/docs/COMMUNITY_BENCHMARKS.md +++ b/docs/COMMUNITY_BENCHMARKS.md @@ -3,6 +3,7 @@ Share measurements from your own hardware so other users can judge how Strata might run on a similar machine. This guide suggests a report format and a pull request workflow; a single model, GPU, or context length is enough to contribute. +The quickest way is the built-in benchmark: [`BENCH.bat` / `./bench.sh`](#run-the-benchmark) writes a report in this format. Existing [benchmark reports](../bench/results/) provide examples. The [engine 0.1.26 speed report](../bench/results/2026-09-29-speed-0126/README.md) @@ -45,6 +46,53 @@ and their limits. Report what you actually measured and label estimates separate - [2026-10-09: RTX 3060 12 GB, Core i7-12700, 64 GB RAM](../bench/results/2026-10-09-community-rtx-3060-0141/README.md): Strata v0.1.41 official Windows release, original Flash-Next Q2_0, IQ2_XS, IQ3_XXS and IQ3_S; prompt sweep from 128 to 128K tokens (260K for three of the four), plus recall, HumanEval 0-39, cache, MTP and concurrency checks. +## Run the benchmark + +The simplest report is one command after setup. Windows: double-click `BENCH.bat`. Linux: `./bench.sh`. Or +`python tools/strata_bench.py` from the Strata folder. Setup also offers it at the end of an interactive install +(default no). + +It starts the model with the settings you already have (the newest `strata-*.json`; `--config FILE` picks another one), +or uses a Strata server that is already running on that port (`--url http://127.0.0.1:8080` for another address). It then +sends a fixed suite through the HTTP API, three runs each, and reports medians with ranges. The default takes about +6 to 10 minutes once the model has loaded: + +| Test | What it sends | What it reports | +| --- | --- | --- | +| chat | three short questions, up to 100 new tokens | decode tok/s, time to first token | +| 4K and 16K prompt | a fresh word-list prompt of about 4,096 and 16,384 tokens (skipped when your context is smaller) | prompt tok/s, time to first token | +| agent session | a system prompt and four tools, then five turns that each add about 1,500 words of a project file and continue from the model's own earlier answers | tokens the conversation cache reused, tokens read again, time to first token per turn | +| `--clients N` | N requests at the same moment (the server needs `--batch` to run them together) | total tok/s, p50 and p95 first-token time and latency | +| `--long` | the prompt test at about 32,768 tokens | prompt tok/s, time to first token | + +Speeds are the engine's own timings (`prompt_per_second`, `predicted_per_second` in the answer); first-token time and +latency are measured by the client. Thinking is off for the suite so the answers are short and the runs comparable +(`--think` leaves it on). Every prompt carries a unique tag, so the fresh-prompt tests never get help from the cache. +Close other programs first: a browser or a game changes the numbers. + +The result goes to `bench/results/-community--/` (or `--out DIR`): + +- `README.md` in the format below, with the GPU, VRAM, CPU, RAM, OS, driver, Strata and engine versions, model and + quantization, context and your settings filled in from the machine and the server +- `results.json` with every request (tokens, timings, errors) and the machine facts +- `engine-log-excerpt.txt` with the engine's start-up lines (when the benchmark started the server) + +Home folders, user and host names, local network addresses and API keys are replaced by placeholders before anything is +written. Read the report once before you share it: the benchmark cannot know what else you consider private. + +To share it, open a pull request that adds the folder (see [Submit a report](#submit-a-report)), or paste +`README.md` into a GitHub issue. Add what the tool does not know: background load, power limits, storage. + +To compare with an earlier run on the same machine (before and after an update or a setting change): + +```bash +python tools/strata_bench.py --compare bench/results/2026-10-01-community-rtx-3060-qwen3-8-flash-next +python tools/strata_bench.py --compare OLD_FOLDER --compare-only NEW_FOLDER # two saved runs, nothing is started +``` + +The tool is a speed check, not a quality test. For recall and other checks, see [Optional correctness +checks](#optional-correctness-checks); for A/B tests of two builds use `tools/ab_engine.py`. + ## What to record Include enough information for someone else to repeat your run: diff --git a/setup.py b/setup.py index 5354f426..492b5567 100644 --- a/setup.py +++ b/setup.py @@ -5772,6 +5772,11 @@ def main() -> int: f"{'START-HERE.bat' if WIN else './setup.sh'} --calibrate tries again") if a.no_start: return 0 + # offered only when someone answers (--yes asks nothing), default no; the benchmark starts its own server and stops it + if not a.yes and sys.stdin.isatty() and ask( + "Run a quick benchmark now? It measures this install for about 10 minutes and writes a report you can share " + f"(later: {'BENCH.bat' if WIN else './bench.sh'})", ["y", "n"], "n", a.yes) == "y": + subprocess.call([sys.executable, str(ROOT / "tools" / "strata_bench.py"), "--config", str(cfg_path), "--port", str(port)]) return start(cfg_path, port) diff --git a/tools/strata_bench.py b/tools/strata_bench.py new file mode 100644 index 00000000..8fe0d5fc --- /dev/null +++ b/tools/strata_bench.py @@ -0,0 +1,919 @@ +#!/usr/bin/env python3 +"""One command that measures this Strata install the way it is used, and writes a report you can share. + + BENCH.bat (Windows) ./bench.sh (Linux) python tools/strata_bench.py + +It starts the server with your existing settings (the newest strata-*.json of this folder, or --config FILE), or uses +the one already running (it checks the config's port, or --url http://host:port), and sends a fixed suite through the +real HTTP API, three runs each, medians reported: + + 1. chat three short questions (about 100 tokens out): decode tok/s and time to first token + 2. 4K prompt a prompt of about 4,096 tokens: prompt tok/s and time to first token (fresh every run: no cache help) + 3. 16K prompt the same at about 16,384 tokens (skipped when the context is smaller) + 4. agent a coding-agent style session: system prompt + four tools, then five turns, each adding about 1,500 words + of a file to the conversation and continuing from the model's own earlier replies - how many prompt + tokens the conversation cache reused, how many had to be read again, time to first token per turn + 5. --clients N N requests at once (the server needs --batch for them to run together): total tok/s, p50/p95 latency + 6. --long 32K the same prompt test at about 32,768 tokens + +Speeds are the engine's own timings (the `timings` of the answer: prompt_per_second, predicted_per_second); time to first +token and latency are measured by this client. Thinking is off for the suite (--think turns it on), so the answers are +short and the runs comparable. The default run takes about 6-10 minutes after the model has loaded. + +The result goes to bench/results/-community--/ : README.md in the format of +docs/COMMUNITY_BENCHMARKS.md, results.json (every request) and an engine log excerpt. Home folders, user and host names, +IP addresses and API keys are replaced by placeholders before anything is written; read it once before you share it. +`--compare ` prints the two side by side. Standard library only. +""" +from __future__ import annotations + +import argparse +import datetime +import getpass +import json +import os +import platform +import re +import shutil +import socket +import statistics +import subprocess +import sys +import tempfile +import threading +import time +import urllib.error +import urllib.request +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from ab_engine import PROMPTS, long_prompt # noqa: E402 the same three short questions and word list +from agent_turns_bench import SYSTEM, TOOLS # noqa: E402 the same agent system prompt and tool list +import serve_load # noqa: E402 the burst client for --clients + +SCHEMA = 1 +CHAT_MAX_TOKENS = 100 +LONG_MAX_TOKENS = 24 +AGENT_MAX_TOKENS = 64 +AGENT_TURNS = 5 +AGENT_WORDS = 1500 +BURST_MAX_TOKENS = 128 + + +# --------------------------------------------------------------------------------------------------------- redaction +def redactor(extra: list[str] = (), key: str = ""): + """-> f(text) -> text with this machine's home folders, user and host names, the Strata folder, IP addresses and the + API key replaced. Applied to everything that is written or printed from the server's or the machine's own text.""" + subs: list[tuple[re.Pattern, str]] = [] + + def add(value, label, word=False): + if value and len(str(value)) >= 4: + v = str(value) + for form in sorted({v, v.replace("\\", "/"), v.replace("/", "\\")}, key=len, reverse=True): + pat = re.escape(form) + subs.append((re.compile(r"(?") + add(Path.home(), "") + add(tempfile.gettempdir(), "") + try: + add(getpass.getuser(), "", True) + except Exception: # noqa: BLE001 + pass + add(socket.gethostname(), "", True) + add(platform.node(), "", True) + for x in extra: + add(x, "", True) + add(key, "") + subs.sort(key=lambda p: -len(p[0].pattern)) + path_user = re.compile(r"(?i)([A-Za-z]:)?([\\/])(Users|home)([\\/])(?!<)[^\\/\s\"':;,]+") + ip4 = re.compile(r"\b(?:192\.168|10\.\d{1,3}|172\.(?:1[6-9]|2\d|3[01])|100\.(?:6[4-9]|[7-9]\d|1[01]\d|12[0-7])|169\.254)\.\d{1,3}\.\d{1,3}\b") + bearer = re.compile(r"(?i)(bearer\s+)[A-Za-z0-9._~+/=-]{8,}") + + def f(text): + if not isinstance(text, str): + return text + for pat, label in subs: + text = pat.sub(label, text) + text = path_user.sub(lambda m: f"{m.group(1) or ''}{m.group(2)}{m.group(3)}{m.group(4)}", text) + text = bearer.sub(r"\1", text) + return ip4.sub("", text) + return f + + +def redact_obj(obj, red): + if isinstance(obj, dict): + return {red(k) if isinstance(k, str) else k: redact_obj(v, red) for k, v in obj.items()} + if isinstance(obj, list): + return [redact_obj(v, red) for v in obj] + return red(obj) if isinstance(obj, str) else obj + + +# ---------------------------------------------------------------------------------------------------------- HTTP +def http_json(base: str, path: str, key: str = "", timeout: float = 20): + req = urllib.request.Request(base + path, headers={"Authorization": "Bearer " + key} if key else {}) + with urllib.request.urlopen(req, timeout=timeout) as r: + return json.loads(r.read()) + + +def try_json(base, path, key="", timeout=20): + try: + return http_json(base, path, key, timeout) + except Exception: # noqa: BLE001 + return None + + +def chat(base: str, key: str, messages: list, max_tokens: int, think: bool = False, tools=None, timeout: float = 3600) -> dict: + """One streamed chat request -> {first, total, text, usage, timings, err}. `first` is the seconds to the first + non-empty content or reasoning piece (keep-alives and empty deltas ignored).""" + body = {"model": "strata", "stream": True, "max_tokens": max_tokens, "temperature": 0, "messages": messages} + if tools: + body["tools"] = tools + if not think: + body["reasoning_effort"] = "none" + body["chat_template_kwargs"] = {"enable_thinking": False} + req = urllib.request.Request(base + "/v1/chat/completions", data=json.dumps(body).encode(), + headers={"Content-Type": "application/json", + **({"Authorization": "Bearer " + key} if key else {})}) + t0 = time.perf_counter() + first, text, usage, timings, err, pieces = None, [], None, None, None, 0 + try: + with urllib.request.urlopen(req, timeout=timeout) as r: + for raw in r: + line = raw.decode("utf-8", "replace").strip() + if not line.startswith("data:"): + continue + d = line[5:].strip() + if d == "[DONE]": + break + try: + j = json.loads(d) + except ValueError: + continue + if j.get("error"): + err = str(j["error"])[:300] + for ch in j.get("choices") or []: + dl = ch.get("delta") or {} + piece = (dl.get("content") or "") + (dl.get("reasoning_content") or "") + if piece: + if first is None: + first = time.perf_counter() - t0 + pieces += 1 + text.append(dl.get("content") or "") + if j.get("usage"): + usage = j["usage"] + if j.get("timings"): + timings = j["timings"] + except urllib.error.HTTPError as e: + err = f"HTTP {e.code}: {e.read()[:200]!r}" + except Exception as e: # noqa: BLE001 + err = repr(e)[:300] + total = time.perf_counter() - t0 + return {"first": first, "total": total, "text": "".join(text), "usage": usage, "timings": timings, "err": err, + "pieces": pieces} + + +def digest(r: dict) -> dict: + """The numbers of one request, the engine's own where it reported them.""" + u, t = r.get("usage") or {}, r.get("timings") or {} + gen = u.get("completion_tokens") or r.get("pieces") or 0 + prompt_tokens = u.get("prompt_tokens") + cached = (u.get("prompt_tokens_details") or {}).get("cached_tokens") + if cached is None: + cached = t.get("cache_n") + read = t.get("prompt_n") + if read is None and prompt_tokens is not None and cached is not None: + read = prompt_tokens - cached + dec, dsrc = t.get("predicted_per_second"), "engine" + if dec is None and r.get("first") is not None and gen > 1 and r["total"] > r["first"]: + dec, dsrc = round((gen - 1) / (r["total"] - r["first"]), 1), "client" + pp, psrc = t.get("prompt_per_second"), "engine" + if pp is None: + pp, psrc = None, None + return {"ok": r.get("err") is None and r.get("first") is not None, + "err": r.get("err"), "ttft_s": None if r.get("first") is None else round(r["first"], 3), + "total_s": round(r["total"], 3), "prompt_tokens": prompt_tokens, "reused_tokens": cached, + "read_tokens": read, "generated_tokens": gen, "prompt_tps": pp, "prompt_tps_source": psrc, + "decode_tps": dec, "decode_tps_source": dsrc if dec is not None else None, + "prompt_ms": t.get("prompt_ms"), "decode_ms": t.get("predicted_ms"), + "drafts_offered": t.get("draft_n"), "drafts_accepted": t.get("draft_n_accepted")} + + +# ----------------------------------------------------------------------------------------------------- statistics +def stat(values) -> dict | None: + v = [x for x in values if isinstance(x, (int, float))] + if not v: + return None + return {"median": round(statistics.median(v), 3), "min": round(min(v), 3), "max": round(max(v), 3), "n": len(v)} + + +def fmt(s, digits=1, unit="") -> str: + if not s: + return "not measured" + d = digits + return f"{s['median']:.{d}f}{unit} ({s['min']:.{d}f}-{s['max']:.{d}f})" + + +def pctl(values, p): + return serve_load.pct(values, p) + + +# ---------------------------------------------------------------------------------------------------- the suite +class Suite: + def __init__(self, base, key, ctx, runs, think, log=print, want_clients=0, want_long=False, quick=False): + self.base, self.key, self.ctx, self.runs, self.think, self.log = base, key, ctx, runs, think, log + self.clients, self.want_long = want_clients, want_long + self.quick = quick + self.nonce = str(int(time.time())) + self.words_per_token = None + self.out: dict = {"chat": {}, "prompts": {}, "agent": {}, "clients": {}, "skipped": {}, "failures": []} + + def tag(self, *parts) -> str: + return "-".join([*map(str, parts), self.nonce]) + + def fail(self, where, d): + self.out["failures"].append({"where": where, "error": d.get("err") or "no token came back"}) + self.log(f" FAILED: {d.get('err') or 'no token came back'}") + + def calibrate(self): + """Words that make a prompt of a wanted size: one tiny request reads 400 words and says how many tokens that was.""" + r = digest(chat(self.base, self.key, [{"role": "user", "content": f"[{self.tag('cal')}]\n" + long_prompt(400)}], 1)) + pt = r.get("prompt_tokens") + self.words_per_token = 400 / pt if pt and pt > 20 else 0.9 + self.log(f" calibrated: about {1 / self.words_per_token:.2f} tokens per word") + + def prompt_for(self, tokens: int, run: int) -> list: + words = max(50, int((tokens - 40) * self.words_per_token)) + return [{"role": "user", "content": f"[bench {self.tag(run, tokens, 'p')}]\n" + long_prompt(words)}] + + def warm_up(self): + self.log("warming up (not measured) ...") + chat(self.base, self.key, [{"role": "user", "content": PROMPTS[0]}], 24, self.think) + self.calibrate() + chat(self.base, self.key, self.prompt_for(1024, -1), 8, self.think) + + def run_chat(self): + self.log(f"chat: {len(PROMPTS)} short questions x {self.runs} runs, up to {CHAT_MAX_TOKENS} new tokens") + rows = [] + for run in range(self.runs): + for i, p in enumerate(PROMPTS): + d = digest(chat(self.base, self.key, [{"role": "user", "content": f"[{self.tag(run, i, 'c')}] {p}"}], + CHAT_MAX_TOKENS, self.think)) + d.update(run=run, prompt=i) + rows.append(d) + if not d["ok"]: + self.fail("chat", d) + else: + self.log(f" run {run + 1} q{i + 1}: decode {d['decode_tps']} tok/s, first token {d['ttft_s']} s, " + f"{d['generated_tokens']} tokens") + good = [r for r in rows if r["ok"]] + self.out["chat"] = {"requests": rows, "decode_tps": stat(r["decode_tps"] for r in good), + "ttft_s": stat(r["ttft_s"] for r in good), "generated_tokens": stat(r["generated_tokens"] for r in good), + "prompt_tokens": stat(r["prompt_tokens"] for r in good)} + + def run_prompt(self, tokens: int, label: str): + if self.ctx and tokens + LONG_MAX_TOKENS + 256 > self.ctx: + self.out["skipped"][label] = f"the context is {self.ctx} tokens" + self.log(f"{label} prompt: skipped (the context is {self.ctx} tokens)") + return + self.log(f"{label} prompt: {self.runs} fresh prompts of about {tokens} tokens, {LONG_MAX_TOKENS} new tokens") + rows = [] + for run in range(self.runs): + d = digest(chat(self.base, self.key, self.prompt_for(tokens, run), LONG_MAX_TOKENS, self.think)) + d["run"] = run + rows.append(d) + if not d["ok"]: + self.fail(label, d) + else: + self.log(f" run {run + 1}: {d['prompt_tokens']} tokens, prompt {d['prompt_tps']} tok/s " + f"({d['reused_tokens'] or 0} reused), first token {d['ttft_s']} s") + good = [r for r in rows if r["ok"]] + self.out["prompts"][label] = {"target_tokens": tokens, "requests": rows, + "prompt_tps": stat(r["prompt_tps"] for r in good), + "ttft_s": stat(r["ttft_s"] for r in good), + "prompt_tokens": stat(r["prompt_tokens"] for r in good), + "reused_tokens": stat(r["reused_tokens"] or 0 for r in good)} + + def agent_files(self) -> list[tuple[str, str]]: + """Five pieces of text a coding agent would read: this folder's own documents when they exist (nothing personal), + else generated filler of the same size.""" + names = [] + for rel in ("README.md", "docs/HOW_IT_WORKS.md", "docs/DETAILS.md", "docs/INSTALL.md", "docs/MODELS.md", "docs/COMMUNITY_BENCHMARKS.md"): + p = ROOT / rel + if p.is_file(): + words = p.read_text(encoding="utf-8", errors="replace").split() + if len(words) >= AGENT_WORDS // 2: + names.append((rel, " ".join(words[:AGENT_WORDS]))) + i = 0 + while len(names) < AGENT_TURNS: + names.append((f"generated/part{i}.txt", long_prompt(AGENT_WORDS))) + i += 1 + return names[:AGENT_TURNS] + + def agent_session(self, run: int) -> list[dict]: + files = self.agent_files() + msgs = [{"role": "system", "content": f"{SYSTEM}\n(session {self.tag(run, 'a')})"}, + {"role": "user", "content": "I will show you files from a project one at a time. After each file, answer in " + "one or two sentences what it is for. Do not call tools."}] + turns = [] + for t in range(AGENT_TURNS + 1): + if t > 0: + rel, body = files[t - 1] + msgs.append({"role": "user", "content": f"File {rel}:\n{body}\n\nWhat is this file for? Two sentences."}) + r = chat(self.base, self.key, msgs, AGENT_MAX_TOKENS, self.think, tools=TOOLS) + d = digest(r) + d.update(run=run, turn=t) + turns.append(d) + if not d["ok"]: + self.fail("agent", d) + break + msgs.append({"role": "assistant", "content": r["text"] or "Understood."}) + return turns + + def run_agent(self): + self.log(f"agent: a session of {AGENT_TURNS + 1} turns with four tools, x {self.runs} runs") + sessions = [] + for run in range(self.runs): + turns = self.agent_session(run) + sessions.append(turns) + for d in turns: + if d["ok"]: + self.log(f" run {run + 1} turn {d['turn']}: prompt {d['prompt_tokens']}, reused {d['reused_tokens']}, " + f"read {d['read_tokens']}, first token {d['ttft_s']} s") + follow = [[d for d in s if d["ok"] and d["turn"] > 0] for s in sessions] + hit = [sum(d["reused_tokens"] or 0 for d in s) / max(1, sum(d["prompt_tokens"] or 0 for d in s)) * 100 + for s in follow if s] + reread = [sum(d["read_tokens"] or 0 for d in s) for s in follow if s] + self.out["agent"] = {"sessions": sessions, "hit_pct": stat(hit), "reread_tokens": stat(reread), + "turn_ttft_s": stat(d["ttft_s"] for s in follow for d in s), + "last_turn_prompt_tokens": stat(s[-1]["prompt_tokens"] for s in follow if s), + "turn_decode_tps": stat(d["decode_tps"] for s in follow for d in s), + "first_turn_ttft_s": stat(s[0]["ttft_s"] for s in sessions if s and s[0]["ok"])} + + def run_clients(self, n: int): + self.log(f"concurrency: {n} requests at once x {self.runs} rounds, {BURST_MAX_TOKENS} new tokens each") + rows = [] + for rd in range(self.runs): + res: dict = {} + th = [] + t0 = time.perf_counter() + for i in range(n): + t = threading.Thread(target=serve_load.one, args=(self.base, self.key, "strata", i, 400, BURST_MAX_TOKENS, res, {}, + self.think, f" {self.tag('b', n, rd)}")) + t.start() + th.append(t) + for t in th: + t.join() + wall = time.perf_counter() - t0 + toks = sum(r["tokens"] for r in res.values()) + errs = [r["err"] for r in res.values() if r["err"]] + rows.append({"round": rd, "wall_s": round(wall, 2), "tokens": toks, "total_tps": round(toks / wall, 2), + "first_s": [r["first"] for r in res.values() if r["first"] is not None], + "latency_s": [r["total"] for r in res.values()], "errors": errs[:3], "error_count": len(errs)}) + self.log(f" round {rd + 1}: {toks} tokens in {wall:.1f} s = {toks / wall:.1f} tok/s total" + + (f" ERRORS {errs[:1]}" if errs else "")) + firsts = [x for r in rows for x in r["first_s"]] + lats = [x for r in rows for x in r["latency_s"]] + self.out["clients"] = {"clients": n, "rounds": rows, "total_tps": stat(r["total_tps"] for r in rows), + "ttft_p50_s": round(pctl(firsts, .5), 3) if firsts else None, + "ttft_p95_s": round(pctl(firsts, .95), 3) if firsts else None, + "latency_p50_s": round(pctl(lats, .5), 3) if lats else None, + "latency_p95_s": round(pctl(lats, .95), 3) if lats else None, + "errors": sum(r["error_count"] for r in rows)} + + def run(self): + self.warm_up() + self.run_chat() + self.run_prompt(4096, "4K") + self.run_prompt(16384, "16K") + self.run_agent() + if self.clients: + self.run_clients(self.clients) + if self.want_long: + self.run_prompt(32768, "32K") + return self.out + + +def summary_metrics(out: dict) -> dict: + """The flat {name: stat} the --compare table and the README table read.""" + m = {"chat decode tok/s": out["chat"].get("decode_tps"), "chat first token (s)": out["chat"].get("ttft_s")} + for k, v in out["prompts"].items(): + m[f"{k} prompt tok/s"] = v.get("prompt_tps") + m[f"{k} first token (s)"] = v.get("ttft_s") + a = out["agent"] + if a: + m["agent cache hit %"] = a.get("hit_pct") + m["agent re-read tokens/session"] = a.get("reread_tokens") + m["agent turn first token (s)"] = a.get("turn_ttft_s") + m["agent decode tok/s"] = a.get("turn_decode_tps") + c = out["clients"] + if c: + m[f"{c['clients']} clients total tok/s"] = c.get("total_tps") + return m + + +# -------------------------------------------------------------------------------------------------- the machine +def run_cmd(cmd, timeout=15) -> str: + try: + r = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout, encoding="utf-8", errors="replace") + return r.stdout if r.returncode == 0 else "" + except (OSError, subprocess.SubprocessError): + return "" + + +def total_ram_gib() -> float | None: + try: + import psutil + return round(psutil.virtual_memory().total / 2 ** 30, 1) + except Exception: # noqa: BLE001 + pass + try: + for line in Path("/proc/meminfo").read_text().splitlines(): + if line.startswith("MemTotal:"): + return round(int(line.split()[1]) / 2 ** 20, 1) + except OSError: + pass + if os.name == "nt": + try: + import ctypes + + class MS(ctypes.Structure): + _fields_ = [("l", ctypes.c_ulong), ("load", ctypes.c_ulong), ("total", ctypes.c_ulonglong)] + [ + (n, ctypes.c_ulonglong) for n in ("a", "b", "c", "d", "e")] + ms = MS() + ms.l = ctypes.sizeof(MS) + ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(ms)) + return round(ms.total / 2 ** 30, 1) + except Exception: # noqa: BLE001 + pass + return None + + +def cpu_name() -> str | None: + try: + for line in Path("/proc/cpuinfo").read_text().splitlines(): + if line.lower().startswith("model name"): + return line.split(":", 1)[1].strip() + except OSError: + pass + if os.name == "nt": + out = run_cmd(["powershell", "-NoProfile", "-Command", "(Get-CimInstance Win32_Processor | Select-Object -First 1).Name"]) + if out.strip(): + return out.strip() + return platform.processor() or None + + +def gpu_info_local() -> dict: + out = run_cmd(["nvidia-smi", "--query-gpu=name,memory.total,driver_version", "--format=csv,noheader,nounits"]) + gpus = [] + for line in out.splitlines(): + p = [x.strip() for x in line.split(",")] + if len(p) >= 3: + gpus.append({"name": p[0], "vram_mib": int(float(p[1])) if p[1].replace(".", "").isdigit() else None, "driver": p[2]}) + info = {"gpus": gpus} + head = run_cmd(["nvidia-smi"]) + m = re.search(r"CUDA Version:\s*([\d.]+)", head) + if m: + info["cuda"] = m.group(1) + if not gpus: + rocm = run_cmd(["rocm-smi", "--showproductname"]) + names = re.findall(r"Card [Ss]eries:\s*(.+)", rocm) or re.findall(r"Card [Mm]odel:\s*(.+)", rocm) + info["gpus"] = [{"name": n.strip(), "vram_mib": None, "driver": None} for n in names] + v = run_cmd(["rocm-smi", "--showdriverversion"]) + m2 = re.search(r"Driver version:\s*(\S+)", v) + if m2: + info["rocm_driver"] = m2.group(1) + return info + + +QUANT = re.compile(r"(?i)(?:^|[-_.])((?:UD-)?(?:IQ\d_[A-Z]+|Q\d_[A-Z0-9_]+|Q\d_K(?:_[A-Z]+)?|IQ\d_?[A-Z]*|BF16|F16|Q\d))(?=[-_.]|$)") +SECRET_KEY = re.compile(r"(?i)key|token|secret|password|auth") + + +def base_if_path(v): + if isinstance(v, str) and (("/" in v or "\\" in v) or re.search(r"\.(gguf|bin|json|pack|txt|jinja|so|dll|exe)$", v, re.I)): + return re.split(r"[\\/]", v.rstrip("\\/"))[-1] + return v + + +def config_view(cfg: dict) -> dict: + """The settings worth reporting, with paths cut to file names and secrets dropped.""" + out = {} + for k, v in cfg.items(): + if SECRET_KEY.search(k) or k in ("log", "cwd", "env"): + continue + if k == "exe": + out[k] = base_if_path(v) + elif k == "args" and isinstance(v, list): + out[k] = [base_if_path(x) if isinstance(x, str) else x for x in v] + elif isinstance(v, (str, int, float, bool)) or v is None: + out[k] = base_if_path(v) + elif isinstance(v, (list, dict)): + out[k] = json.loads(json.dumps(v)) + env = cfg.get("env") + if isinstance(env, dict): + out["env"] = {k: base_if_path(str(v)) for k, v in env.items() if k.startswith(("STRATA_", "HIP_", "CUDA_", "ROCR_", "ONEAPI_", "SYCL_")) and not SECRET_KEY.search(k)} + return out + + +def launch_line(view: dict) -> str: + args = " ".join(f'"{a}"' if " " in str(a) else str(a) for a in view.get("args", [])) + return f"{view.get('exe', 'strata')} {args}".strip() + + +def detect_quant(view: dict) -> str | None: + names = [a for a in view.get("args", []) if isinstance(a, str) and re.search(r"\.(gguf|pack)$|^[\w.-]+$", a)] + names += [str(view.get("model_name") or "")] + for n in names: + m = QUANT.search(n) + if m: + return m.group(1).upper() + return None + + +def project_version() -> str | None: + try: + m = re.search(r"project\(strata VERSION ([\d.]+)", (ROOT / "CMakeLists.txt").read_text()) + return m.group(1) if m else None + except OSError: + return None + + +def git_commit() -> str | None: + if not (ROOT / ".git").exists(): + return None + out = run_cmd(["git", "-C", str(ROOT), "rev-parse", "--short=8", "HEAD"]).strip() + return out or None + + +def collect_machine(base: str, key: str, cfg: dict | None) -> dict: + status = try_json(base, "/v1/status", key) or {} + metrics = try_json(base, "/metrics", key, 30) or {} + props = try_json(base, "/props", key) or {} + models = try_json(base, "/v1/models", key) or {} + static = metrics.get("hardware_static") or {} + eng = metrics.get("engine") or {} + local = gpu_info_local() + view = config_view(cfg) if cfg else {} + gpu_names = [g["name"] for g in local["gpus"]] or ([static["gpu_name"]] if static.get("gpu_name") else []) + m = status.get("machine") or {} + vram_mib = (m.get("gpu") or {}).get("total_mib") or None + if local["gpus"] and local["gpus"][0].get("vram_mib"): + vram_mib = sum(g.get("vram_mib") or 0 for g in local["gpus"]) + n_ctx = None + for d in models.get("data") or []: + n_ctx = (d.get("meta") or {}).get("n_ctx") or n_ctx + return { + "gpu": gpu_names, "gpu_vram_mib": vram_mib, "gpu_driver": (local["gpus"][0].get("driver") if local["gpus"] else local.get("rocm_driver")), + "cuda": local.get("cuda"), "cpu": static.get("cpu_name") or cpu_name(), "cpu_cores": static.get("cores"), + "cpu_threads": static.get("threads") or os.cpu_count(), "ram_gib": total_ram_gib(), + "os": f"{platform.system()} {platform.release()} ({platform.version()})" if os.name == "nt" else f"{platform.system()} {platform.release()}", + "python": platform.python_version(), + "strata_version": project_version(), "strata_commit": git_commit(), + "engine_version": (eng.get("version") or status.get("engine") or str(props.get("build_info", "")).replace("Strata ", "") or None), + "model": view.get("model_name") or status.get("model") or props.get("model_alias"), + "quant": detect_quant(view) if view else None, + "context": n_ctx or eng.get("max_context"), "parallel_slots": props.get("total_slots"), + "config": view, "launch": launch_line(view) if view else None, + "engine_info": {k: v for k, v in eng.items() if isinstance(v, (str, int, float, bool, dict)) and k not in ("model_path",)}, + "vram_after": (status.get("vram") or {}), + } + + +# --------------------------------------------------------------------------------------------------- the server +def find_configs(folder: Path = ROOT) -> list[Path]: + out = [] + for p in folder.glob("strata-*.json"): + if p.name.endswith(".shared-settings.json"): + continue + try: + c = json.loads(p.read_text(encoding="utf-8-sig")) + except (OSError, ValueError): + continue + if isinstance(c, dict) and c.get("exe") and isinstance(c.get("args"), list): + out.append(p) + return sorted(out, key=lambda p: p.stat().st_mtime, reverse=True) + + +def is_strata(base: str, key: str) -> bool: + h = try_json(base, "/health", key, 5) + return bool(h and h.get("service") == "strata") + + +def start_server(cfg_path: Path, port: int, gpu: str | None, log_path: Path): + cmd = [sys.executable, str(ROOT / "serve" / "server.py"), "--engine", "strata", "--config", str(cfg_path), "--port", str(port)] + if gpu: + cmd += ["--gpu", gpu] + logf = open(log_path, "w", encoding="utf-8", errors="replace") + return subprocess.Popen(cmd, cwd=str(ROOT), stdout=logf, stderr=subprocess.STDOUT) + + +def wait_ready(base: str, key: str, proc, timeout: float, log=print) -> bool: + t0, last = time.time(), 0 + while time.time() - t0 < timeout: + if proc is not None and proc.poll() is not None: + return False + h = try_json(base, "/health", key, 5) + if h and h.get("service") == "strata" and h.get("loaded"): + return True + if time.time() - last > 30: + log(f" still loading ({int(time.time() - t0)} s) ...") + last = time.time() + time.sleep(3) + return False + + +def stop_server(proc): + if proc is None or proc.poll() is not None: + return + proc.terminate() + try: + proc.wait(90) + except subprocess.TimeoutExpired: + proc.kill() + + +def log_excerpt(path: Path, limit: int = 80) -> list[str]: + try: + lines = path.read_text(encoding="utf-8", errors="replace").splitlines() + except OSError: + return [] + keep = re.compile(r"(?i)expert cache|slots|vram|ready|resident|kv |context|backend|gpu|prefill|mtp|spec|batch|version|hit") + out = [x for x in lines if keep.search(x) and "[strata] done:" not in x] + return out[:limit] + + +# --------------------------------------------------------------------------------------------------- the report +def slug(text: str, limit: int = 40) -> str: + s = re.sub(r"[^a-z0-9]+", "-", (text or "").lower()).strip("-") + return s[:limit].strip("-") or "unknown" + + +def gpu_slug(machine: dict) -> str: + names = machine.get("gpu") or [] + if not names: + return "cpu" + n = re.sub(r"(?i)\b(nvidia|geforce|amd|radeon|intel|graphics|corporation)\b", " ", names[0]) + s = slug(n, 30) + return s if len(names) == 1 else f"{len(names)}x-{s}" + + +def results_dir_name(machine: dict, date: str) -> str: + return f"{date}-community-{gpu_slug(machine)}-{slug((machine.get('model') or 'model') + ('-' + machine['quant'] if machine.get('quant') else ''), 40)}" + + +def unique_dir(parent: Path, name: str) -> Path: + p, n = parent / name, 2 + while p.exists(): + p = parent / f"{name}-{n}" + n += 1 + return p + + +def row(label, prompt_tokens, reused, gen, runs, pp, dec, ttft): + return f"| {label} | {prompt_tokens} | {reused} | {gen} | {runs} | {pp} | {dec} | {ttft} |" + + +def results_table(out: dict) -> list[str]: + lines = ["| Configuration | Actual prompt tokens | Reused tokens | Generated tokens | Runs | Prompt tok/s median and range | " + "Decode tok/s median and range | TTFT seconds median and range |", + "| --- | ---: | ---: | ---: | ---: | --- | --- | --- |"] + c = out["chat"] + if c: + lines.append(row("short chat", fmt(c.get("prompt_tokens"), 0), "0", fmt(c.get("generated_tokens"), 0), + (c.get("ttft_s") or {}).get("n", 0), "not measured", fmt(c.get("decode_tps")), fmt(c.get("ttft_s"), 2))) + for k, v in out["prompts"].items(): + lines.append(row(f"{k} prompt, fresh", fmt(v.get("prompt_tokens"), 0), fmt(v.get("reused_tokens"), 0), str(LONG_MAX_TOKENS) + " cap", + (v.get("ttft_s") or {}).get("n", 0), fmt(v.get("prompt_tps")), "not measured", fmt(v.get("ttft_s"), 2))) + a = out["agent"] + if a: + n = len([s for s in a["sessions"] if len(s) > 1]) + lines.append(row("agent session, follow-up turns", fmt(a.get("last_turn_prompt_tokens"), 0) + " at the last turn", + "see below", f"up to {AGENT_MAX_TOKENS}", n, "see below", fmt(a.get("turn_decode_tps")), + fmt(a.get("turn_ttft_s"), 2))) + return lines + + +def render_readme(res: dict) -> str: + mc, out, p = res["machine"], res["results"], res["params"] + gpu = " + ".join(mc.get("gpu") or []) or "CPU only" + vram = f"{mc['gpu_vram_mib'] / 1024:.0f} GiB VRAM" if mc.get("gpu_vram_mib") else "VRAM not read" + model = " ".join(x for x in (mc.get("model"), mc.get("quant")) if x) or "model not read" + L = [f"# Community benchmark on {gpu}", "", + f"Measured on {res['date']} with `tools/strata_bench.py` (suite {SCHEMA}, {p['runs']} runs per test, medians with ranges). " + f"Speeds are the engine's own timings; first-token times are measured by the client over HTTP. Speed only: no " + f"correctness check was run.", "", + "## Hardware and software", "", + f"- GPU and VRAM: {gpu}, {vram}", + f"- CPU: {mc.get('cpu') or 'not read'} ({mc.get('cpu_cores') or '?'} cores, {mc.get('cpu_threads') or '?'} threads); " + f"installed RAM: {mc.get('ram_gib') or '?'} GiB; storage: not measured", + f"- OS: {mc.get('os')}; GPU driver: {mc.get('gpu_driver') or 'not read'}; CUDA: {mc.get('cuda') or 'not read'}", + f"- Strata: project {mc.get('strata_version') or '?'}, commit {mc.get('strata_commit') or 'not a git checkout'}; " + f"engine {mc.get('engine_version') or 'not read'}", + "- Background workloads and power limits: not recorded", "", + "## Model and configuration", "", + f"- Model and quantization: {model}", + f"- Context: {mc.get('context') or 'not read'} tokens; parallel slots: {mc.get('parallel_slots') or 1}", + f"- Settings: the install's own configuration, unchanged (`{mc.get('config', {}).get('backend', 'default backend')}` backend); thinking " + f"{'on' if p['think'] else 'off'} for the suite; temperature 0", ""] + if mc.get("launch"): + L += ["```text", mc["launch"], "```", ""] + env = (mc.get("config") or {}).get("env") + if env: + L += ["Environment of the config: " + ", ".join(f"`{k}={v}`" for k, v in env.items()), ""] + L += ["## Method", "", + f"Run by `{p['command']}` against the HTTP API of a Strata server " + f"({'started by the benchmark from the install configuration' if p['started_server'] else 'already running'}); model loading is not " + f"timed. One warm-up chat and a 1K prompt are discarded first. Every prompt starts with a unique tag, so the conversation " + f"cache cannot help the fresh-prompt tests.", "", + f"- short chat: the three questions of `tools/ab_engine.py`, up to {CHAT_MAX_TOKENS} new tokens, {p['runs']} runs", + f"- 4K / 16K{' / 32K' if 'prompt' in res['results'] and '32K' in res['results']['prompts'] else ''} prompts: generated word lists sized by a calibration request, " + f"{LONG_MAX_TOKENS} new tokens, {p['runs']} fresh prompts each", + f"- agent session: system prompt and four tools, then {AGENT_TURNS} turns that each add about {AGENT_WORDS} words of a project file " + f"and continue from the model's own earlier answers (the way agent clients resend a conversation), {AGENT_MAX_TOKENS} new tokens per turn, " + f"{p['runs']} sessions; the cache numbers come from the answer's usage (`cached_tokens`) and timings (`prompt_n`)", + "- concurrency (when run): N requests sent at the same moment, total tokens over the wall time of the burst, " + "latency over all requests", "", + "## Results", ""] + L += results_table(out) + L += ["", "Ranges are the lowest and highest of the runs; `Runs` counts requests that returned a token."] + ag = out["agent"] + if ag and ag.get("sessions"): + L += ["", "### Agent session (conversation cache)", "", + f"Cache hit over the follow-up turns: {fmt(ag.get('hit_pct'), 1, ' %')}; prompt tokens read again per session: " + f"{fmt(ag.get('reread_tokens'), 0)}; first turn (nothing cached) first token: {fmt(ag.get('first_turn_ttft_s'), 2, ' s')}.", "", + "| Turn (run 1) | Prompt tokens | Reused | Read | Prompt tok/s | First token (s) | Decode tok/s |", "| ---: | ---: | ---: | ---: | ---: | ---: | ---: |"] + for d in ag["sessions"][0]: + if d["ok"]: + L.append(f"| {d['turn']} | {d['prompt_tokens']} | {d['reused_tokens']} | {d['read_tokens']} | {d['prompt_tps'] or '-'} | {d['ttft_s']} | {d['decode_tps'] or '-'} |") + cl = out["clients"] + if cl: + L += ["", f"### Concurrency: {cl['clients']} clients", "", + f"Total {fmt(cl.get('total_tps'), 1, ' tok/s')}; first token p50 {cl.get('ttft_p50_s')} s, p95 {cl.get('ttft_p95_s')} s; " + f"request latency p50 {cl.get('latency_p50_s')} s, p95 {cl.get('latency_p95_s')} s; errors {cl.get('errors')}. " + f"Server parallel slots: {mc.get('parallel_slots') or 1} (requests beyond that wait their turn)."] + if out["skipped"] or out["failures"]: + L += ["", "### Skipped and failed", ""] + L += [f"- skipped {k}: {v}" for k, v in out["skipped"].items()] + L += [f"- failed {f['where']}: {f['error']}" for f in out["failures"]] + L += ["", "Per-request data: `results.json`. Engine log lines from the start: `engine-log-excerpt.txt` (when this run started the server).", "", + "## Correctness and limitations", "", + "Not measured: this is a speed suite. The conversation uses the model's own replies, so token counts differ a little between runs. " + "Machine details come from the server, `nvidia-smi`/`rocm-smi` and the OS; background load and power limits are not recorded. " + f"Total time of the suite: {res['elapsed_s']:.0f} s.", ""] + return "\n".join(L) + + +def compare_table(old: dict, new: dict) -> str: + a, b = summary_metrics(old["results"]), summary_metrics(new["results"]) + rows = [f"{'':32s} {'old':>12s} {'new':>12s} {'change':>9s}"] + for k in dict.fromkeys([*a, *b]): + x, y = (a.get(k) or {}).get("median"), (b.get(k) or {}).get("median") + if x is None and y is None: + continue + ch = f"{(y - x) / x * 100:+.1f}%" if x and y is not None else "" + rows.append(f"{k:32s} {('-' if x is None else format(x, '.2f')):>12s} {('-' if y is None else format(y, '.2f')):>12s} {ch:>9s}") + ho, hn = old["machine"], new["machine"] + notes = [f" {n}: {ho.get(f)} -> {hn.get(f)}" for n, f in (("GPU", "gpu"), ("engine", "engine_version"), ("model", "model"), ("quant", "quant"), + ("context", "context")) if ho.get(f) != hn.get(f)] + return "\n".join(rows + (["", "differences:"] + notes if notes else [])) + + +def load_results(path: Path) -> dict: + p = Path(path) + if p.is_dir(): + p = p / "results.json" + return json.loads(p.read_text(encoding="utf-8")) + + +def short_table(res: dict) -> str: + m = summary_metrics(res["results"]) + lines = [] + for k, v in m.items(): + if v: + lines.append(f" {k:32s} {v['median']:>10.2f} ({v['min']:.2f} - {v['max']:.2f}, n={v['n']})") + return "\n".join(lines) + + +def write_results(res: dict, parent: Path, red, log_lines: list[str]) -> Path: + d = unique_dir(parent, results_dir_name(res["machine"], res["date"])) + d.mkdir(parents=True) + clean = redact_obj(res, red) + (d / "results.json").write_text(json.dumps(clean, indent=1, ensure_ascii=False), encoding="utf-8") + (d / "README.md").write_text(red(render_readme(res)), encoding="utf-8") + if log_lines: + (d / "engine-log-excerpt.txt").write_text(red("\n".join(log_lines)) + "\n", encoding="utf-8") + return d + + +# --------------------------------------------------------------------------------------------------------- main +def main(argv=None) -> int: + ap = argparse.ArgumentParser(description="Measure this Strata install and write a shareable report (see the top of this file).") + ap.add_argument("--config", help="the model config to run (default: the newest strata-*.json of this folder)") + ap.add_argument("--url", help="use the server already running at this address (http://127.0.0.1:8080) instead of starting one") + ap.add_argument("--port", type=int, help="the port to start the server on (default: the config's)") + ap.add_argument("--gpu", help="start the server on this GPU only (as the server's --gpu)") + ap.add_argument("--api-key", default=os.environ.get("STRATA_KEY", ""), help="the server's API key, if it has one (or STRATA_KEY)") + ap.add_argument("--runs", type=int, default=3, help="runs per test (default 3)") + ap.add_argument("--clients", type=int, default=0, metavar="N", help="also send N requests at once") + ap.add_argument("--long", action="store_true", help="also a prompt of about 32,768 tokens") + ap.add_argument("--think", action="store_true", help="leave thinking on (the default suite turns it off)") + ap.add_argument("--out", help="where to write the results folder (default: bench/results)") + ap.add_argument("--compare", metavar="DIR", help="a results folder (or its results.json) to put beside this run") + ap.add_argument("--compare-only", metavar="NEW", help="print --compare of two existing results folders and stop") + ap.add_argument("--ready-timeout", type=float, default=1800, help="seconds to wait for the model to load") + ap.add_argument("--quiet", action="store_true") + a = ap.parse_args(argv) + say = (lambda *x, **k: None) if a.quiet else (lambda *x, **k: print(*x, **k, flush=True)) + + if a.compare_only: + if not a.compare: + ap.error("--compare-only NEW needs --compare OLD") + print(compare_table(load_results(Path(a.compare)), load_results(Path(a.compare_only)))) + return 0 + + cfg_path, cfg, proc, log_path = None, None, None, None + if a.config or not a.url: + if a.config: + cfg_path = Path(a.config) + if not cfg_path.is_absolute() and not cfg_path.exists(): + cfg_path = ROOT / a.config + else: + found = find_configs() + if found: + cfg_path = found[0] + if len(found) > 1: + say(f"Several models are installed; using {cfg_path.name} (the one used last). Pick another with --config FILE.") + if cfg_path is None or not cfg_path.is_file(): + print("No Strata model is set up in this folder: run START-HERE.bat (Windows) or ./setup.sh (Linux) first, " + "or give --url of a running server.", file=sys.stderr) + return 2 + cfg = json.loads(cfg_path.read_text(encoding="utf-8-sig")) + key = a.api_key or ((cfg or {}).get("api_key") or "") + if a.url: + base = a.url.rstrip("/") + else: + port = a.port or int(cfg.get("port", 8080)) + base = f"http://127.0.0.1:{port}" + red = redactor(key=key) + started = False + try: + if is_strata(base, key): + say(f"Using the Strata server already running at {base}.") + elif a.url: + print(f"No Strata server answers at {base}.", file=sys.stderr) + return 2 + else: + if try_json(base, "/health", key, 3) is not None: + print(f"Port {port} is used by something that is not Strata: pass --port N.", file=sys.stderr) + return 2 + log_path = Path(tempfile.gettempdir()) / f"strata-bench-{os.getpid()}.log" + say(f"Starting {cfg_path.name} on port {port} (loading takes 1-3 minutes; the PC may be slow meanwhile) ...") + proc = start_server(cfg_path, port, a.gpu, log_path) + started = True + if not wait_ready(base, key, proc, a.ready_timeout, say): + tail = log_path.read_text(encoding="utf-8", errors="replace").splitlines()[-15:] if log_path.exists() else [] + print("The server did not become ready. Last log lines:\n" + red("\n".join(tail)), file=sys.stderr) + return 1 + t0 = time.time() + pre = collect_machine(base, key, cfg) + suite = Suite(base, key, pre.get("context"), max(1, a.runs), a.think, log=say, want_clients=a.clients, want_long=a.long) + slots = pre.get("parallel_slots") or 1 + if a.clients and a.clients > slots: + say(f"Note: the server runs {slots} request(s) at a time (--batch), so {a.clients} clients will partly wait their turn.") + say("Running the suite (about 6-10 minutes) ...") + out = suite.run() + machine = collect_machine(base, key, cfg) # again: VRAM and the cache after the work + if not machine.get("gpu"): + machine["gpu"] = pre.get("gpu") + res = {"schema": SCHEMA, "tool": "tools/strata_bench.py", "date": datetime.date.today().isoformat(), + "params": {"runs": suite.runs, "think": a.think, "clients": a.clients, "long": a.long, "started_server": started, + "command": "python tools/strata_bench.py" + "".join( + f" {x}" for x in (f"--clients {a.clients}" if a.clients else "", "--long" if a.long else "", + "--think" if a.think else "", f"--runs {a.runs}" if a.runs != 3 else "") if x)}, + "elapsed_s": time.time() - t0, "machine": machine, "results": out} + parent = Path(a.out) if a.out else ROOT / "bench" / "results" + d = write_results(res, parent, red, log_excerpt(log_path) if log_path else []) + say("\nResults (median, range over the runs):") + say(red(short_table(res))) + if out["failures"] or out["skipped"]: + say(f" ({len(out['failures'])} failed request(s), {len(out['skipped'])} skipped test(s): see the report)") + if a.compare: + say("\nCompared with " + a.compare + ":") + say(red(compare_table(load_results(Path(a.compare)), redact_obj(res, lambda s: s)))) + try: + shown = d.relative_to(ROOT) + except ValueError: + shown = d + say(f"\nReport: {shown / 'README.md'}\nShare it: open a pull request that adds the folder {shown} (see docs/COMMUNITY_BENCHMARKS.md), " + f"or paste README.md into a GitHub issue. Home folders, user and host names and IP addresses are already replaced; " + f"read it once before you share it.") + return 1 if out["failures"] and not any(out[k] for k in ("chat", "prompts", "agent")) else 0 + finally: + if started: + stop_server(proc) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/test_strata_bench.py b/tools/test_strata_bench.py new file mode 100644 index 00000000..02c0e417 --- /dev/null +++ b/tools/test_strata_bench.py @@ -0,0 +1,261 @@ +"""Tests for tools/strata_bench.py with a fake engine behind a real HTTP server: the suite runner, the redaction, the report +writing and the comparison. Light: no GPU, no Strata server, a few seconds.""" +from __future__ import annotations + +import json +import os +import sys +import tempfile +import threading +import unittest +from unittest import mock +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import strata_bench as B # noqa: E402 + + +def text_of(messages) -> str: + return "".join(m.get("content") or "" for m in messages) + + +class FakeStrata(ThreadingHTTPServer): + """Speaks the parts of the Strata API the benchmark reads. The conversation cache is the longest common prefix with the + previous prompt (4 characters = 1 token).""" + daemon_threads = True + + def __init__(self, ctx=32768, fail_after=None): + super().__init__(("127.0.0.1", 0), Handler) + self.ctx, self.prev, self.requests, self.fail_after = ctx, "", 0, fail_after + self.lock = threading.Lock() + threading.Thread(target=self.serve_forever, daemon=True).start() + + def stop(self): + self.shutdown() + self.server_close() + + @property + def base(self): + return f"http://127.0.0.1:{self.server_address[1]}" + + +class Handler(BaseHTTPRequestHandler): + def log_message(self, *a): + pass + + def send_json(self, obj, code=200): + body = json.dumps(obj).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + def do_GET(self): + s = self.server + if self.path == "/health": + self.send_json({"status": "ok", "service": "strata", "loaded": True}) + elif self.path == "/v1/models": + self.send_json({"object": "list", "data": [{"id": "fake", "meta": {"n_ctx": s.ctx}}]}) + elif self.path == "/props": + self.send_json({"total_slots": 4, "model_alias": "fake-model", "build_info": "Strata 9.9.9"}) + elif self.path == "/v1/status": + self.send_json({"machine": {"gpu": {"name": "NVIDIA GeForce RTX 9999", "total_mib": 12288}, "ram": {"total_gib": 64.0}}, + "engine": "9.9.9", "vram": {"free_mib": 1000}}) + elif self.path == "/metrics": + self.send_json({"engine": {"version": "9.9.9", "max_context": s.ctx, "model_path": r"C:\Users\Someone\models\x.gguf"}, + "hardware_static": {"gpu_name": "NVIDIA GeForce RTX 9999", "cpu_name": "Fake CPU 9", "cores": 8, "threads": 16}}) + else: + self.send_json({}, 404) + + def do_POST(self): + s = self.server + req = json.loads(self.rfile.read(int(self.headers["Content-Length"]))) + text = text_of(req["messages"]) + with s.lock: + s.requests += 1 + n = s.requests + common = 0 + for x, y in zip(s.prev, text): + if x != y: + break + common += 1 + s.prev = text + if s.fail_after is not None and n > s.fail_after: + self.send_json({"error": "boom"}, 500) + return + prompt = max(1, len(text) // 4) + cached = min(prompt, common // 4) + gen = min(req.get("max_tokens", 16), 12) + self.send_response(200) + self.send_header("Content-Type", "text/event-stream") + self.end_headers() + + def chunk(delta, finish=None, **extra): + d = {"id": "x", "choices": [{"index": 0, "delta": delta, "finish_reason": finish}], **extra} + self.wfile.write(b"data: " + json.dumps(d).encode() + b"\n\n") + self.wfile.flush() + chunk({"role": "assistant"}) + for _ in range(gen): + chunk({"content": "word "}) + chunk({}, "stop", usage={"prompt_tokens": prompt, "completion_tokens": gen, "total_tokens": prompt + gen, + "prompt_tokens_details": {"cached_tokens": cached}}, + timings={"cache_n": cached, "prompt_n": prompt - cached, "prompt_ms": 100.0, + "prompt_per_second": 1000.0 + n, "predicted_n": gen, "predicted_ms": 200.0, "predicted_per_second": 60.0}) + self.wfile.write(b"data: [DONE]\n\n") + + +class SuiteTests(unittest.TestCase): + def setUp(self): + self.srv = FakeStrata() + self.addCleanup(self.srv.stop) + + def suite(self, **kw): + return B.Suite(self.srv.base, "", kw.pop("ctx", 32768), kw.pop("runs", 3), False, log=lambda *a: None, **kw) + + def test_full_suite_shapes_and_medians(self): + out = self.suite(want_clients=3).run() + self.assertEqual(out["failures"], []) + self.assertEqual(out["chat"]["decode_tps"]["median"], 60.0) + self.assertEqual(out["chat"]["decode_tps"]["n"], 9) # 3 questions x 3 runs + self.assertEqual(set(out["prompts"]), {"4K", "16K"}) + for v in out["prompts"].values(): + self.assertEqual(v["prompt_tps"]["n"], 3) + self.assertLess(v["reused_tokens"]["median"], v["prompt_tokens"]["median"] / 100) # a fresh tag at the start: next to nothing reused + p4, p16 = (out["prompts"][k]["prompt_tokens"]["median"] for k in ("4K", "16K")) + self.assertAlmostEqual(p16 / p4, 4, delta=0.6) # the sizes are calibrated, not guessed + self.assertEqual(out["clients"]["clients"], 3) + self.assertEqual(out["clients"]["errors"], 0) + self.assertEqual(out["clients"]["rounds"][0]["tokens"], 3 * 12) + + def test_agent_session_reuses_the_conversation(self): + out = self.suite(runs=2).run() + ag = out["agent"] + self.assertEqual(len(ag["sessions"]), 2) + turns = ag["sessions"][0] + self.assertEqual([t["turn"] for t in turns], list(range(B.AGENT_TURNS + 1))) + self.assertEqual(turns[0]["reused_tokens"], 0) + for t in turns[1:]: + self.assertGreater(t["reused_tokens"], 0) + self.assertLess(t["read_tokens"], t["prompt_tokens"]) + self.assertGreater(ag["hit_pct"]["median"], 50) + self.assertGreater(turns[-1]["prompt_tokens"], turns[1]["prompt_tokens"]) # the conversation grows + + def test_small_context_skips_the_long_prompts(self): + self.srv.ctx = 8192 + out = self.suite(ctx=8192, runs=1, want_long=True).run() + self.assertIn("4K", out["prompts"]) + self.assertEqual(set(out["skipped"]), {"16K", "32K"}) + + def test_failures_are_reported_not_hidden(self): + self.srv.fail_after = 3 # warm-up and calibration succeed, then every request fails + out = self.suite(runs=1).run() + self.assertTrue(out["failures"]) + self.assertTrue(all("HTTP 500" in f["error"] for f in out["failures"])) + self.assertIsNone(out["chat"]["decode_tps"]) + + +class RedactionTests(unittest.TestCase): + def test_paths_names_ips_and_keys(self): + red = B.redactor(extra=["Jane Roe Laptop"], key="sk-secret-123456") + user = os.environ.get("USERNAME") or os.environ.get("USER") or "tester" + cases = [r"C:\Users\someone_else\models\a.gguf", "/home/bob/strata/x.json", "/Users/alice/Library", + "peer 192.168.1.20 and 10.0.0.7 and 100.101.102.103", "Authorization: Bearer abcdefghijkl12345", + "key sk-secret-123456 here", "name Jane Roe Laptop"] + text = red("\n".join(cases)) + for s in ("someone_else", "bob", "alice", "192.168.1.20", "10.0.0.7", "100.101.102.103", "abcdefghijkl12345", + "sk-secret-123456", "Jane Roe Laptop"): + self.assertNotIn(s, text) + self.assertIn("", text) + self.assertIn("", text) + self.assertEqual(red("127.0.0.1:8080 and version 0.1.40.3 and driver 32.0.15.7"), "127.0.0.1:8080 and version 0.1.40.3 and driver 32.0.15.7") + self.assertNotIn(str(B.ROOT), red(f"loading {B.ROOT / 'strata-x.json'}")) + self.assertIn("", red(f"loading {B.ROOT / 'strata-x.json'}")) + self.assertNotIn(str(Path.home()), red(f"in {Path.home() / 'data'}")) + if len(user) >= 4: + self.assertNotIn(user, red(f"hello {user} world")) + + def test_objects_are_redacted_deeply(self): + red = B.redactor() + out = B.redact_obj({"a": [{"p": r"D:\Users\zed\x"}], "n": 3}, red) + self.assertNotIn("zed", json.dumps(out)) + self.assertEqual(out["n"], 3) + + def test_config_view_drops_secrets_and_cuts_paths(self): + cfg = {"exe": r"C:\Users\me\Strata\engine\strata.exe", "args": ["--native", "/home/me/models/m-IQ3_XXS.gguf", "--ctx", "65536"], + "api_key": "hunter2", "model_name": "Fake Model", "log": "/tmp/x.log", "port": 8080, + "env": {"STRATA_FOO": "/home/me/z", "PATH": "/usr/bin", "STRATA_TOKEN": "t"}} + v = B.config_view(cfg) + self.assertEqual(v["exe"], "strata.exe") + self.assertEqual(v["args"], ["--native", "m-IQ3_XXS.gguf", "--ctx", "65536"]) + self.assertNotIn("api_key", v) + self.assertNotIn("log", v) + self.assertEqual(v["env"], {"STRATA_FOO": "z"}) + self.assertEqual(B.detect_quant(v), "IQ3_XXS") + self.assertNotIn("hunter2", json.dumps(v)) + + +class ReportTests(unittest.TestCase): + def setUp(self): + p = mock.patch.object(B, "gpu_info_local", return_value={"gpus": []}) # the server's own GPU name, not this PC's + p.start() + self.addCleanup(p.stop) + + def run_main(self, srv, out_dir, *extra): + argv = ["--url", srv.base, "--out", out_dir, "--runs", "2", "--quiet", *extra] + return B.main(argv) + + def test_main_writes_a_redacted_report(self): + srv = FakeStrata() + self.addCleanup(srv.stop) + with tempfile.TemporaryDirectory() as d: + self.assertEqual(self.run_main(srv, d, "--clients", "2"), 0) + dirs = list(Path(d).iterdir()) + self.assertEqual(len(dirs), 1) + name = dirs[0].name + self.assertRegex(name, r"^\d{4}-\d\d-\d\d-community-rtx-9999") + readme = (dirs[0] / "README.md").read_text(encoding="utf-8") + for part in ("# Community benchmark on NVIDIA GeForce RTX 9999", "## Hardware and software", "## Model and configuration", + "## Method", "## Results", "## Correctness and limitations", "### Agent session", "### Concurrency: 2 clients", + "| Configuration | Actual prompt tokens | Reused tokens |"): + self.assertIn(part, readme) + raw = json.loads((dirs[0] / "results.json").read_text(encoding="utf-8")) + self.assertEqual(raw["schema"], B.SCHEMA) + self.assertEqual(raw["machine"]["engine_version"], "9.9.9") + self.assertEqual(raw["results"]["chat"]["decode_tps"]["median"], 60.0) + blob = readme + json.dumps(raw) + self.assertNotIn("Someone", blob) # the engine's model_path never reaches the report + # a second run the same day does not overwrite the first + self.assertEqual(self.run_main(srv, d), 0) + self.assertEqual(len(list(Path(d).iterdir())), 2) + + def test_compare(self): + srv = FakeStrata() + self.addCleanup(srv.stop) + with tempfile.TemporaryDirectory() as d: + self.run_main(srv, d) + old = next(Path(d).iterdir()) + data = json.loads((old / "results.json").read_text(encoding="utf-8")) + data["results"]["chat"]["decode_tps"]["median"] = 30.0 + data["machine"]["engine_version"] = "1.0" + (old / "results.json").write_text(json.dumps(data), encoding="utf-8") + self.run_main(srv, d) + new = max(Path(d).iterdir(), key=lambda p: p.name) + table = B.compare_table(B.load_results(old), B.load_results(new)) + self.assertIn("chat decode tok/s", table) + self.assertIn("+100.0%", table) + self.assertIn("engine: 1.0 -> 9.9.9", table) + + def test_names(self): + m = {"gpu": ["NVIDIA GeForce RTX 3060"], "model": "Qwen3.8 Flash-Next", "quant": "IQ3_XXS"} + self.assertEqual(B.results_dir_name(m, "2026-10-10"), "2026-10-10-community-rtx-3060-qwen3-8-flash-next-iq3-xxs") + self.assertEqual(B.gpu_slug({"gpu": ["A B 4090", "A B 4090"]}), "2x-a-b-4090") + self.assertEqual(B.gpu_slug({"gpu": []}), "cpu") + + def test_no_server_is_an_error_not_a_crash(self): + self.assertEqual(B.main(["--url", "http://127.0.0.1:9", "--quiet"]), 2) + + +if __name__ == "__main__": + unittest.main()