-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathbench_vs_grep.py
More file actions
276 lines (240 loc) · 8.85 KB
/
Copy pathbench_vs_grep.py
File metadata and controls
276 lines (240 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python
"""Benchmark CodeRAG's indexed search against a raw grep baseline.
This makes the headline claim measurable: a warm, pre-indexed workspace answers a query
faster (one in-process call vs many ripgrep invocations) and more accurately on conceptual
/ natural-language queries than the agentic grep loop that tools like Claude Code and Codex
fall back to. It reuses the eval harness so the accuracy numbers are directly comparable to
``coderag eval``.
python scripts/bench_vs_grep.py \
--watched-dir . \
--dataset coderag/eval/datasets/coderag_self.jsonl
What it reports, both for CodeRAG (hybrid retrieval) and for grep:
* accuracy — recall@k / nDCG@k / MRR at the file level (via coderag.eval.evaluate)
* latency — mean / p50 / p95 wall-clock per query
* context — approximate tokens needed to surface the top-k context (CodeRAG returns
compact chunks; the grep baseline must read whole matched files)
The grep baseline models the agent's behaviour: extract salient terms from the query, run
ripgrep for each, rank files by match frequency — i.e. the floor that semantic search is
meant to beat. As the project's own strategy notes, grep wins on exact identifiers and
persistent edit tasks; CodeRAG's edge is conceptual queries on larger repos plus BM25 for
identifiers, with no code leaving the machine.
"""
from __future__ import annotations
import argparse
import os
import re
import subprocess # nosec B404 — benchmarking against the ripgrep CLI is the whole point
import time
from collections import Counter
from pathlib import Path
from statistics import mean
from typing import Callable, List, Sequence
from coderag.api import CodeRAG
from coderag.config import Config
from coderag.eval import EvalCase, evaluate, load_dataset
from coderag.eval.harness import EvalResult, format_table
from coderag.types import SearchHit
# Tiny stopword set so grep searches for content terms, not glue words.
_STOP = {
"the",
"and",
"for",
"with",
"that",
"this",
"from",
"into",
"are",
"was",
"use",
"add",
"fix",
"when",
"where",
"what",
"how",
"does",
"should",
"make",
"now",
}
_TOKEN = re.compile(r"[A-Za-z_][A-Za-z0-9_]{2,}")
def _query_terms(query: str, limit: int = 8) -> List[str]:
"""Salient search terms from a natural-language query (what an agent would grep for)."""
seen: List[str] = []
for tok in _TOKEN.findall(query):
low = tok.lower()
if low in _STOP or low in {t.lower() for t in seen}:
continue
seen.append(tok)
if len(seen) >= limit:
break
return seen
def make_grep_search(root: Path) -> Callable[[str, int], List[SearchHit]]:
"""A grep-backed retriever with the harness's ``(query, k) -> hits`` signature."""
def search(query: str, k: int) -> List[SearchHit]:
terms = _query_terms(query)
if not terms:
return []
counts: Counter = Counter()
for term in terms:
try:
proc = subprocess.run( # nosec B603,B607 — fixed argv, no shell
[
"rg",
"--count-matches",
"--no-messages",
"-i",
"-e",
term,
str(root),
],
capture_output=True,
text=True,
timeout=30,
)
except (FileNotFoundError, subprocess.TimeoutExpired):
continue
for line in proc.stdout.splitlines():
path, _, num = line.rpartition(":")
if not path:
continue
try:
counts[path] += int(num)
except ValueError:
# Not a "path:count" line (e.g. a path containing ':', or rg's
# summary output) — skip it rather than fail the whole query.
continue
hits: List[SearchHit] = []
for abs_path, score in counts.most_common(k):
rel = os.path.relpath(abs_path, root)
hits.append(
SearchHit(
chunk_id=0,
path=Path(rel).as_posix(),
symbol=None,
kind="window",
language="",
start_line=1,
end_line=1,
text="",
score=float(score),
similarity=0.0,
)
)
return hits
return search
def _timed(
fn: Callable[[str, int], List[SearchHit]], sink: List[float]
) -> Callable[[str, int], List[SearchHit]]:
def wrapped(query: str, k: int) -> List[SearchHit]:
start = time.perf_counter()
try:
return fn(query, k)
finally:
sink.append(time.perf_counter() - start)
return wrapped
def _percentile(values: Sequence[float], pct: float) -> float:
if not values:
return 0.0
ordered = sorted(values)
idx = min(len(ordered) - 1, int(round((pct / 100.0) * (len(ordered) - 1))))
return ordered[idx]
def _fmt_ms(values: Sequence[float]) -> str:
if not values:
return "n/a"
return (
f"mean {mean(values) * 1000:7.1f} "
f"p50 {_percentile(values, 50) * 1000:7.1f} "
f"p95 {_percentile(values, 95) * 1000:7.1f} (ms)"
)
def _file_chars(path: Path) -> int:
try:
return len(path.read_text(encoding="utf-8", errors="replace"))
except OSError:
return 0
def _context_tokens(
cr: CodeRAG,
grep_search: Callable[[str, int], List[SearchHit]],
cases: Sequence[EvalCase],
root: Path,
top_k: int,
) -> tuple[int, int]:
"""Approximate tokens (~chars/4) to surface top-k context for each retriever.
CodeRAG returns the matched chunks; the grep baseline must read whole matched files —
which is the token-cost argument in favour of indexed retrieval.
"""
cr_tokens = grep_tokens = 0
for case in cases:
cr_tokens += sum(len(h.text) for h in cr.search(case.query, top_k)) // 4
grep_tokens += (
sum(_file_chars(root / h.path) for h in grep_search(case.query, top_k)) // 4
)
return cr_tokens, grep_tokens
def main() -> int:
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("--watched-dir", default=".", help="Repo/directory to search.")
ap.add_argument(
"--dataset",
default="coderag/eval/datasets/coderag_self.jsonl",
help="JSONL query -> relevant_files dataset (file level).",
)
ap.add_argument(
"--store-dir", default=None, help="Index location (default ./.coderag)."
)
ap.add_argument("--model", default="BAAI/bge-small-en-v1.5")
ap.add_argument("--ks", default="1,5,10")
ap.add_argument(
"--top-k", type=int, default=10, help="k for latency/token sampling."
)
ap.add_argument("--no-index", action="store_true", help="Reuse the existing index.")
args = ap.parse_args()
root = Path(args.watched_dir).expanduser().resolve()
ks = tuple(int(k) for k in args.ks.split(","))
cases = load_dataset(args.dataset)
if not cases:
raise SystemExit(f"No eval cases in {args.dataset}.")
cfg = Config.from_env(
provider="fastembed",
model=args.model,
watched_dir=root,
store_dir=Path(args.store_dir).expanduser()
if args.store_dir
else root / ".coderag",
)
cr = CodeRAG(cfg)
if not args.no_index:
stats = cr.index()
print(f"Indexed {stats.total_files} files / {stats.total_chunks} chunks.\n")
grep_search = make_grep_search(root)
cr_times: List[float] = []
grep_times: List[float] = []
results: List[EvalResult] = [
evaluate(
_timed(cr.search, cr_times),
cases,
label="coderag (hybrid)",
ks=ks,
level="file",
),
evaluate(
_timed(grep_search, grep_times), cases, label="grep", ks=ks, level="file"
),
]
cr_tok, grep_tok = _context_tokens(cr, grep_search, cases, root, args.top_k)
print(f"Accuracy ({len(cases)} cases, file level)\n")
print(format_table(results))
print("\nLatency per query")
print(f" coderag (1 warm call) : {_fmt_ms(cr_times)}")
print(
f" grep ({len(_query_terms(cases[0].query)) or 'n'} rg calls/query) : {_fmt_ms(grep_times)}"
)
print(f"\nApprox context tokens for top-{args.top_k} (sum over cases)")
print(f" coderag (compact chunks): {cr_tok:>9,}")
print(f" grep (read whole files) : {grep_tok:>9,}")
if cr_tok:
print(f" -> grep needs ~{grep_tok / max(cr_tok, 1):.1f}x the context tokens")
cr.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())