Skip to content

Official Codebase of 3DCodeVerse

Python

bench: records() and session_stats read side-car JSON through proc.read_json_or_none — three try/json.loads copies go

gaoypeng8c8759c46b9a
diff --git a/eval/bench/_records.py b/eval/bench/_records.py
index a02ef729..8e3b6b3c 100644
--- a/eval/bench/_records.py
+++ b/eval/bench/_records.py
@@ -6,9 +6,9 @@ point counted a run twice, or (a bare ``rglob``) missed every symlinked cell."""
from __future__ import annotations
-import json
from pathlib import Path
+from codeverse3d.proc import read_json_or_none
from codeverse3d.record.record import unique_files
@@ -17,11 +17,8 @@ def records(root: Path, *, track: str | None = None) -> list[tuple[Path, dict]]:
disk; an unreadable file is skipped, and ``track`` keeps only that track's runs."""
out = []
for rec in unique_files(root, "record.json"):
- try:
- data = json.loads(rec.read_text())
- except (OSError, ValueError):
- continue
- if track is None or (data.get("spec") or {}).get("track") == track:
+ data = read_json_or_none(rec)
+ if data is not None and (track is None or (data.get("spec") or {}).get("track") == track):
out.append((rec, data))
return out
diff --git a/eval/bench/session_stats.py b/eval/bench/session_stats.py
index 4323e79e..31a3447a 100644
--- a/eval/bench/session_stats.py
+++ b/eval/bench/session_stats.py
@@ -16,7 +16,6 @@ directory (a glob that follows it doubles every number).
from __future__ import annotations
import argparse
-import json
import statistics
import sys
from collections import Counter
@@ -26,6 +25,7 @@ for _p in (Path(__file__).resolve().parents[2] / "harness", Path(__file__).resol
sys.path.insert(0, str(_p)) # this tree's codeverse3d (harness/) + the `bench` package (eval/)
from bench._records import records # noqa: E402
+from codeverse3d.proc import read_json_or_none # noqa: E402
from codeverse3d.record.record import unique_files # noqa: E402
#: gemini-cli names an MCP tool ``mcp_<server>_<tool>``; ours is the ``3dcode`` server
@@ -57,10 +57,7 @@ def tool_rates(sessions: list[Path]) -> tuple[Counter, Counter]:
calls: Counter = Counter()
failed: Counter = Counter()
for path in sessions:
- try:
- stats = (json.loads(path.read_text()) or {}).get("stats") or {}
- except (OSError, ValueError):
- continue
+ stats = (read_json_or_none(path) or {}).get("stats") or {}
for name, row in ((stats.get("tools") or {}).get("byName") or {}).items():
prefix = next((p for p in MCP_PREFIXES if name.startswith(p)), None)
if prefix is None:
@@ -84,11 +81,7 @@ def token_rates(sessions: list[Path]) -> dict[str, float]:
prompt = cached = requests = 0
for path in sessions:
- try:
- stdout = json.loads(path.read_text()) or {}
- except (OSError, ValueError):
- continue
- models = ((stdout.get("stats") or {}).get("models")) or {}
+ models = (((read_json_or_none(path) or {}).get("stats") or {}).get("models")) or {}
if not models:
continue
main = max(models, key=lambda n: int(((models[n] or {}).get("api") or {}).get("totalRequests") or 0))