java-topology/defects/simplex/bench/bench-simplex-chat-0001.py
russell@unturf.com 99be1fbed9 bench backfill: close 36 orphan-project dirs + fix rubocop MOAD misclass
Previously 52 registered defects had no project dir locally because their
patches live under a sibling project (e.g. ans-* under defects/ansible/,
geth-0001 under defects/go-ethereum/, cfe-* under defects/cfengine/).

Created defects/{stem}/bench/ for each orphan stem (ans, argo, cel, cfe,
element, geth, go-stdlib, hv, igraph, nats, nx, openscad, otel, pre, r,
rmq, simplex, sm, solargraph, tf, tf-aws) and wrote the standard list-vs-set
benches against each defect. Patches stay where they are; bench_status
looks up by defect-id prefix, not patch location.

Also added defects/rubocop/ with benches for rubocop-0001 and rubocop-0002
(Array -> Set with compare_by_identity). Both were misclassified as
MOAD-0011 ReDoS in generate_undf.py; the tickets show they're CWE-407
Sedimentary (O(N*S) -> O(N+S) via identity Set).

Coverage: 1243 -> 1281 (96.0% -> 98.9%). The 14 remaining are truly
missing — no patch anywhere in the tree: dragonflybsd-0001..0005,
jami-daemon, jitsi-videobridge, netbsd-0001..0004, regamedll-cs-0001,
supertuxkart-0002, hadoop-rpc-0001.
2026-04-23 12:37:51 -04:00

50 lines
1.4 KiB
Python

#!/usr/bin/env python3
# bench-simplex-chat-0001.py
# CWE-407: list-scan inside loop in simplex-chat-0001 (generic model)
# Models O(N*k) -> O(N+k) via linear-scan membership inside a loop vs set/dict.
import sys
import time
def bench_defective(n, k):
pool = list(range(k))
items = list(range(n))
t0 = time.perf_counter()
seen = []
for x in items:
if x not in pool: # O(k)
seen.append(x)
return time.perf_counter() - t0
def bench_fixed(n, k):
pool_set = set(range(k))
items = list(range(n))
t0 = time.perf_counter()
seen = []
for x in items:
if x not in pool_set: # O(1)
seen.append(x)
return time.perf_counter() - t0
TRIALS = 3
CASES = [(100, 100), (500, 500), (1000, 1000), (2000, 2000)]
def run():
lines = []
header = "=== simplex-chat-0001: CWE-407: list-scan inside loop in simplex-chat-0001 (generic model) ==="
print(header); lines.append(header)
for n, k in CASES:
df = min(bench_defective(n, k) for _ in range(TRIALS))
fx = min(bench_fixed(n, k) for _ in range(TRIALS))
speedup = (df / fx) if fx > 0 else float("inf")
line = f"N={n:<5} k={k:<5}: defective={df*1000:.3f}ms fixed={fx*1000:.3f}ms speedup={speedup:.1f}x"
print(line); lines.append(line); sys.stdout.flush()
return lines
if __name__ == "__main__":
run()