Files
graphify/graphify/benchmark.py
T

122 lines
4.5 KiB
Python

"""Token-reduction benchmark - measures how much context graphify saves vs naive full-corpus approach."""
from __future__ import annotations
import sys
import networkx as nx
from graphify.serve import _query_graph_text, _read_graph
from graphify.paths import default_graph_json as _default_graph_json
_CHARS_PER_TOKEN = 4 # standard approximation
def _safe(unicode_char: str, ascii_fallback: str) -> str:
"""Return unicode_char if stdout can encode it, else ascii_fallback.
Windows consoles often default to cp1252 which cannot encode box-drawing
or arrow glyphs; printing them raises UnicodeEncodeError mid-output.
"""
encoding = getattr(sys.stdout, "encoding", None) or ""
try:
unicode_char.encode(encoding)
return unicode_char
except (UnicodeEncodeError, LookupError):
return ascii_fallback
def _hr(width: int = 50) -> str:
"""Horizontal rule that survives non-UTF-8 stdout (e.g. Windows cp1252 console)."""
return _safe("─", "-") * width
def _estimate_tokens(text: str) -> int:
return max(1, len(text) // _CHARS_PER_TOKEN)
def _query_subgraph_tokens(G: nx.Graph, question: str, depth: int = 3, token_budget: int = 2000) -> int:
"""Estimated tokens of the answer query_graph returns for ``question`` (BFS,
the MCP tool's defaults), 0 when nothing in the graph matches it. The
answer is the real one an agent receives: seeds, budget cut and notices."""
text = _query_graph_text(G, question, mode="bfs", depth=depth, token_budget=token_budget)
if text == "No matching nodes found.":
return 0
return _estimate_tokens(text)
_SAMPLE_QUESTIONS = [
"how does authentication work",
"what is the main entry point",
"how are errors handled",
"what connects the data layer to the api",
"what are the core abstractions",
]
def run_benchmark(
graph_path: str | None = None,
corpus_words: int | None = None,
questions: list[str] | None = None,
) -> dict:
"""Measure token reduction: corpus tokens vs graphify query tokens.
Args:
graph_path: path to the built graph
corpus_words: total word count from detect() output; if None, estimated from graph
questions: list of questions to benchmark; defaults to _SAMPLE_QUESTIONS
Returns dict with: corpus_tokens, avg_query_tokens, reduction_ratio, per_question
"""
graph_path = graph_path or _default_graph_json()
# Loaded exactly as the MCP server loads it (size cap, links/edges
# normalization for a raw --no-cluster graph, #2212), so every answer
# measured is the one query_graph returns.
G = _read_graph(str(graph_path))
if corpus_words is None:
# Rough estimate: each node label is ~3 words, plus source context
corpus_words = G.number_of_nodes() * 50
corpus_tokens = corpus_words * 100 // 75 # words → tokens (100 words ≈ 133 tokens)
qs = questions or _SAMPLE_QUESTIONS
per_question = []
for q in qs:
qt = _query_subgraph_tokens(G, q)
if qt > 0:
per_question.append({"question": q, "query_tokens": qt, "reduction": round(corpus_tokens / qt, 1)})
if not per_question:
return {"error": "No matching nodes found for sample questions. Build the graph first."}
avg_query_tokens = sum(p["query_tokens"] for p in per_question) // len(per_question)
reduction_ratio = round(corpus_tokens / avg_query_tokens, 1) if avg_query_tokens > 0 else 0
return {
"corpus_tokens": corpus_tokens,
"corpus_words": corpus_words,
"nodes": G.number_of_nodes(),
"edges": G.number_of_edges(),
"avg_query_tokens": avg_query_tokens,
"reduction_ratio": reduction_ratio,
"per_question": per_question,
}
def print_benchmark(result: dict) -> None:
"""Print a human-readable benchmark report."""
if "error" in result:
print(f"Benchmark error: {result['error']}")
return
print(f"\ngraphify token reduction benchmark")
print(_hr(50))
arrow = _safe("→", "->")
print(f" Corpus: {result['corpus_words']:,} words {arrow} ~{result['corpus_tokens']:,} tokens (naive)")
print(f" Graph: {result['nodes']:,} nodes, {result['edges']:,} edges")
print(f" Avg query cost: ~{result['avg_query_tokens']:,} tokens")
print(f" Reduction: {result['reduction_ratio']}x fewer tokens per query")
print(f"\n Per question:")
for p in result["per_question"]:
print(f" [{p['reduction']}x] {p['question'][:55]}")
print()