115 lines
5.5 KiB
Python
115 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Summarize a Codex agent run against the stack (offline helper, not shipped).
|
|
|
|
codex_report.py RUN_DIR reads codex-events.jsonl, leleka-api.log (one `generation done:` line per request),
|
|
inference.log and env.tsv written by run-e2e.sh / run-codex.sh.
|
|
"""
|
|
import json
|
|
import os
|
|
import re
|
|
import statistics
|
|
import sys
|
|
|
|
run = sys.argv[1]
|
|
|
|
|
|
def lines(name):
|
|
try:
|
|
with open(os.path.join(run, name), errors="replace") as f:
|
|
return f.read().splitlines()
|
|
except OSError:
|
|
return []
|
|
|
|
|
|
def med(xs):
|
|
return round(statistics.median(xs), 2) if xs else None
|
|
|
|
|
|
def pct(xs, p):
|
|
if not xs:
|
|
return None
|
|
xs = sorted(xs)
|
|
return round(xs[min(len(xs) - 1, int(p * len(xs)))], 2)
|
|
|
|
|
|
# ---- Codex events -------------------------------------------------------------------------------------------------------
|
|
kinds, usage, tool_items, exits, errors = {}, {"input": 0, "cached": 0, "output": 0, "turns": 0}, 0, [], []
|
|
for line in lines("codex-events.jsonl"):
|
|
try:
|
|
e = json.loads(line)
|
|
except ValueError:
|
|
continue
|
|
t = e.get("type", "?")
|
|
kinds[t] = kinds.get(t, 0) + 1
|
|
if t == "turn.completed":
|
|
u = e.get("usage") or {}
|
|
usage["input"] += u.get("input_tokens", 0)
|
|
usage["cached"] += u.get("cached_input_tokens", 0)
|
|
usage["output"] += u.get("output_tokens", 0)
|
|
usage["turns"] += 1
|
|
if t in ("error", "turn.failed"):
|
|
errors.append(json.dumps(e)[:300])
|
|
item = e.get("item") or {}
|
|
if t == "item.completed":
|
|
it = item.get("type", "?")
|
|
kinds["item." + it] = kinds.get("item." + it, 0) + 1
|
|
if it == "command_execution":
|
|
exits.append(item.get("exit_code"))
|
|
print("== Codex events")
|
|
print(" event kinds:", json.dumps(kinds, sort_keys=True))
|
|
print(" Codex usage:", json.dumps(usage))
|
|
print(" command executions:", len(exits), "non-zero exit:", sum(1 for x in exits if x not in (0, None)))
|
|
print(" error events:", len(errors))
|
|
for e in errors[:5]:
|
|
print(" ", e)
|
|
|
|
# ---- per-request records from leleka-api ---------------------------------------------------------------------------------
|
|
pat = re.compile(r"generation done: finish=(\w+) prompt_tokens=(\d+) reused=(\d+) generated=(\d+) prompt_ms=([\d.]+) decode_ms=([\d.]+) disconnected=(\w+)")
|
|
reqs = []
|
|
for line in lines("leleka-api.log"):
|
|
m = pat.search(line)
|
|
if m:
|
|
reqs.append({"finish": m[1], "prompt": int(m[2]), "reused": int(m[3]), "gen": int(m[4]), "ttft_s": float(m[5]) / 1000, "decode_s": float(m[6]) / 1000, "disc": m[7] == "true"})
|
|
failed = [l for l in lines("leleka-api.log") if "generation failed:" in l]
|
|
print("== Requests seen by leleka-api:", len(reqs), " failures logged:", len(failed))
|
|
causes = {}
|
|
for l in failed:
|
|
k = l.split("generation failed:", 1)[1].strip()[:90]
|
|
causes[k] = causes.get(k, 0) + 1
|
|
for k, v in sorted(causes.items(), key=lambda x: -x[1])[:10]:
|
|
print(f" {v}x {k}")
|
|
if reqs:
|
|
ttfts = [r["ttft_s"] for r in reqs]
|
|
rates = [(r["gen"] - 1) / r["decode_s"] for r in reqs if r["gen"] > 8 and r["decode_s"] > 0.05]
|
|
pre = [r["prompt"] - r["reused"] for r in reqs]
|
|
total_p, total_r = sum(r["prompt"] for r in reqs), sum(r["reused"] for r in reqs)
|
|
print(f" TTFT s (engine first text, as seen by leleka-api): median {med(ttfts)} p90 {pct(ttfts, .9)} max {max(ttfts):.2f}")
|
|
print(f" decode tok/s per request (>8 tokens): median {med(rates)} p10 {pct(rates, .1)} p90 {pct(rates, .9)}")
|
|
gen = sum(r["gen"] for r in reqs)
|
|
dec = sum(r["decode_s"] for r in reqs)
|
|
print(f" decode tokens / sum of decode time: {round(gen / dec, 2) if dec else None} tok/s ({gen} tokens)")
|
|
pt = sum(r["ttft_s"] for r in reqs)
|
|
print(f" prompt tokens / sum of time-to-first-text: {round(total_p / pt, 1) if pt else None} tok/s (includes decode of the first tokens)")
|
|
print(f" prompt tokens total {total_p}, reused {total_r} ({100 * total_r / max(total_p, 1):.1f} %), re-prefilled {total_p - total_r}")
|
|
print(f" re-prefilled tokens per request: median {med(pre)} p90 {pct(pre, .9)} max {max(pre)}")
|
|
print(f" prompt tokens per request: median {med([r['prompt'] for r in reqs])} max {max(r['prompt'] for r in reqs)}")
|
|
# growth of the conversation: re-prefill vs request index in 10 buckets
|
|
n = len(reqs)
|
|
for b in range(0, n, max(1, n // 10)):
|
|
chunk = reqs[b:b + max(1, n // 10)]
|
|
print(f" requests {b + 1}-{b + len(chunk)}: prompt avg {sum(r['prompt'] for r in chunk) // len(chunk)}, reused avg {sum(r['reused'] for r in chunk) // len(chunk)}, ttft avg {sum(r['ttft_s'] for r in chunk) / len(chunk):.2f}s")
|
|
print(" finish reasons:", {k: sum(1 for r in reqs if r["finish"] == k) for k in {r["finish"] for r in reqs}})
|
|
|
|
# ---- memory -------------------------------------------------------------------------------------------------------------
|
|
rows = [l.split("\t") for l in lines("env.tsv")[1:] if l.count("\t") >= 8]
|
|
print("== Memory over the run (env.tsv)")
|
|
for name in sorted({r[1] for r in rows}):
|
|
rs = [r for r in rows if r[1] == name]
|
|
rss = [int(r[2]) for r in rs]
|
|
print(f" {name}: samples {len(rs)}, VmRSS MiB first {rss[0]} / max {max(rss)} / last {rss[-1]}, VmLck last {rs[-1][6]}, RssShmem last {rs[-1][5]}")
|
|
if rows:
|
|
gpu = [int(r[7]) for r in rows if r[7].isdigit()]
|
|
avail = [int(r[8]) for r in rows if r[8].isdigit()]
|
|
if gpu:
|
|
print(f" GPU used MiB: first {gpu[0]} / max {max(gpu)} / last {gpu[-1]}; MemAvailable MiB: first {avail[0]} / min {min(avail)} / last {avail[-1]}")
|