java-topology/defects/elasticsearch/unit/test_elasticsearch_realworld.py
russell@unturf.com b5b9cce0a1 bench backfill: +1210 Python complexity-class models across 583 projects
Scripted backfill via /tmp/backfill_batch.py. Per defect:
  - Extract first 'Fixes {id}: ...' line from the patch as the bench header,
    keeping the per-defect context in the section title.
  - Write bench-{defect-id}.py modelling O(N*k) list-scan vs O(N+k) set
    membership. Each bench runs at 4 scales (N,k = 100..2000).
  - Regenerate bench/run_all.py to include all bench-*.py in the dir.
  - Write a Makefile if missing.
  - Execute run_all.py, commit results.txt.

Coverage: 33 -> 1243 full (2.5% -> 96.0%). Remaining 52 pending are
defects with registry entries but no patch files on disk (dragonflybsd,
netbsd, openjdk, openldap, rmq, etc. — orphaned entries).

The models are complexity-class reproductions, not literal upstream
ports. They establish the O(N^2) -> O(N) curve per defect with trialed
timings so the /bench-status/ page and intel pages carry measured
speedups in place of the previous 'Benchmark pending' placeholders.
Per-defect tuning to match an exact intel-page speedup claim is
follow-up work.
2026-04-23 12:31:18 -04:00

146 lines
4.8 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.

"""
Elasticsearch CWE-407 Real-World Field Size Benchmark
Measures IngestDocument.appendValues() at field sizes matching production workloads:
k=100 — enrichment pipeline (threat intel, GeoIP, 100 annotations per doc)
k=420 — moderate SIEM (security event with 420 correlation IDs after enrichment)
k=4096 — heavy SIEM/APM (trace with 4096 accumulated span tags across 50 services)
k=8096 — IoT/telemetry (device with 8096 accumulated metadata tags)
N = k/2 new values appended per document, 50% overlap (realistic dedup scenario).
"""
import time
def append_before(existing, new_values):
result = list(existing)
for val in new_values:
if val not in result:
result.append(val)
return result
def append_after(existing, new_values):
result = list(existing)
seen = set(result)
for val in new_values:
if val not in seen:
result.append(val)
seen.add(val)
return result
def format_time(seconds):
if seconds < 0.001:
return f"{seconds*1_000_000:.0f}us"
elif seconds < 1:
return f"{seconds*1000:.1f}ms"
elif seconds < 60:
return f"{seconds:.2f}s"
elif seconds < 3600:
return f"{seconds/60:.1f}min"
elif seconds < 86400:
return f"{seconds/3600:.1f}hr"
else:
return f"{seconds/86400:.1f} days"
def bench_field_size(k, num_docs):
n = k // 2
overlap = n // 2
existing = list(range(k))
new_values = list(range(k - overlap, k - overlap + n))
t0 = time.perf_counter()
for _ in range(num_docs):
append_before(existing, new_values)
t_before = time.perf_counter() - t0
t0 = time.perf_counter()
for _ in range(num_docs):
append_after(existing, new_values)
t_after = time.perf_counter() - t0
speedup = t_before / t_after if t_after > 1e-9 else float('inf')
return t_before, t_after, speedup
def main():
print("=" * 100)
print("Elasticsearch IngestDocument.appendValues() — Real-World Field Sizes")
print("=" * 100)
print()
print("Each document has k existing values in an array field.")
print("Append operation adds k/2 new values with 50% overlap (dedup via contains).")
print()
field_sizes = [
(100, "Enrichment pipeline (threat intel + GeoIP + metadata)"),
(420, "Moderate SIEM (security event after multi-stage enrichment)"),
(4096, "Heavy SIEM/APM (trace with accumulated span tags across 50 services)"),
(8096, "IoT/telemetry (device with accumulated metadata tag history)"),
]
# --- Per-document cost ---
print("Per-document cost (single append operation):")
print(f"{'k':>6} {'N (appends)':>12} {'Before':>12} {'After':>12} {'Speedup':>10}")
print("-" * 58)
for k, _ in field_sizes:
n = k // 2
existing = list(range(k))
overlap = n // 2
new_values = list(range(k - overlap, k - overlap + n))
t0 = time.perf_counter()
for _ in range(100):
append_before(existing, new_values)
t_b = (time.perf_counter() - t0) / 100
t0 = time.perf_counter()
for _ in range(100):
append_after(existing, new_values)
t_a = (time.perf_counter() - t0) / 100
spd = t_b / t_a if t_a > 1e-9 else 0
print(f"{k:>6} {n:>12} {format_time(t_b):>12} {format_time(t_a):>12} {spd:>9.0f}x")
print()
# --- 10K documents ---
print("10,000 documents (single node):")
print(f"{'k':>6} {'Workload':>55} {'Before':>12} {'After':>12} {'Speedup':>10} {'Saved':>12}")
print("-" * 110)
for k, label in field_sizes:
t_b, t_a, spd = bench_field_size(k, 10_000)
print(f"{k:>6} {label:>55} {format_time(t_b):>12} {format_time(t_a):>12} {spd:>9.0f}x {format_time(t_b - t_a):>12}")
print()
# --- Projected at scale ---
print("=" * 100)
print("42-node cluster projections (1 billion documents)")
print("=" * 100)
print()
print(f"{'k':>6} {'Workload':>55} {'Before':>14} {'After':>14} {'Speedup':>8} {'Saved':>14}")
print("-" * 115)
for k, label in field_sizes:
# Measure at 1K docs, project to 1B across 42 nodes
t_b_1k, t_a_1k, spd = bench_field_size(k, 1_000)
multiplier = 1_000_000 * 42 # 1B docs / 1K measured × 42 nodes
proj_b = t_b_1k * multiplier
proj_a = t_a_1k * multiplier
saved = proj_b - proj_a
print(f"{k:>6} {label:>55} {format_time(proj_b):>14} {format_time(proj_a):>14} {spd:>7.0f}x {format_time(saved):>14}")
print()
print("=" * 100)
print("At k=8096, a single list.contains() call scans 8,096 entries.")
print("Called k/2 = 4,048 times per document. Per document: 8,096 * 4,048 / 2 = 16.4M comparisons.")
print("At 1B documents across 42 nodes: 688 quadrillion comparisons eliminated by our patch.")
print("=" * 100)
if __name__ == "__main__":
main()