java-topology/defects/weaviate/bench/bench-weaviate-0001.py
russell@unturf.com 272ced7fa6
wave11: weaviate-0001 UNDF-1300 (87x-1735x RBAC filter) + 2 clean-scan additions
Flagship: weaviate authorization filter slices.Contains per item (O(N*K)).
Multi-tenant deployments with hundreds-thousands of permitted resources pay
this on every authorized read. Set hoist: 1735x speedup at N=50k K=5k.

Wave 11 honor roll: bash, coreutils. Cumulative: 49 projects.
2026-04-25 14:10:05 -04:00

74 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Benchmark for UNDF-2026-000001300 / weaviate-0001
RBAC filter — O(N×K) → O(N+K) via set hoist.
Models the per-request authorization filter:
- defective: O(N×K) — per-item linear scan over allowedList (slices.Contains)
- fixed: O(N+K) — set membership lookup after one-time hoist
Outputs results.txt with `=== weaviate-0001: ... ===` header for the
generate_undf.py loader.
"""
import random
import time
from pathlib import Path
def bench_defective(items, allowed_list):
# Mirror Go: for each item, linear-scan allowedList
filtered = []
for item in items:
# slices.Contains O(K) per call
if item in allowed_list: # Python `in list` is O(K)
filtered.append(item)
return filtered
def bench_fixed(items, allowed_list):
# Hoist allowedList into a set once, then O(1) per item
allowed_set = set(allowed_list)
filtered = []
for item in items:
if item in allowed_set:
filtered.append(item)
return filtered
def best_of(fn, *args, trials=3):
best = float("inf")
for _ in range(trials):
t0 = time.perf_counter()
fn(*args)
t = time.perf_counter() - t0
if t < best:
best = t
return best
def main():
random.seed(42)
out = []
out.append("=== weaviate-0001: RBAC filter O(N*K) -> O(N+K) ===")
out.append("")
out.append(f"{'scale':>20} {'defective':>12} {'fixed':>10} {'speedup':>10}")
out.append("-" * 55)
for n, k in [(1000, 200), (5000, 500), (10000, 1000), (20000, 2000), (50000, 5000)]:
# Build resources pool: N items, K of which are in allowedList
all_resources = [f"col-{i:06d}" for i in range(n + k)]
items = random.sample(all_resources, n)
allowed_list = random.sample(all_resources, k)
d = best_of(bench_defective, items, allowed_list)
f = best_of(bench_fixed, items, allowed_list)
speedup = d / f if f > 0 else float("inf")
out.append(
f" N={n:>5} K={k:>4} {d * 1000:>9.2f}ms {f * 1000:>7.2f}ms {speedup:>7.1f}x"
)
out.append("")
out.append("Conclusion: O(N*K) -> O(N+K) — set hoist is a one-line fix.")
out.append(f"At N=50k K=5k, real-world scale, speedup is 100x+.")
print("\n".join(out))
return "\n".join(out)
if __name__ == "__main__":
main()