java-topology/defects/selenium/bench/bench-selenium-0001.py
russell@unturf.com 9a0253e724 browser-automation: 4 CWE-407 patches (selenium x2, playwright, webdriverio)
selenium-0001: SessionCapabilitiesMutator list.contains O(NxM) -> LinkedHashSet
  O(N+M). Grid Node session mutation hot path. Bench: 192x at N=M=1000.

selenium-0002: ChromiumOptions merge helpers consolidate four list.contains
  loops behind addArgumentsUnique/addEncodedExtensionsUnique. Bench: 254x
  at N=M=1000.

playwright-0001: roleUtils validRoles / allowsNameFromContent Array.includes
  on 20-70 element constant arrays per element. Converted to Set<string>
  at module load. Bench: 11x at N=10000 elements.

webdriverio-0001: xpath-conditions extractOrConditions orMatches.find +
  values.includes per regex match -> Map<attr, Set<values>>. Bench: 6x at
  K=V=60 in the 'mobileSelectorPerformanceOptimizer'.

Each defect ships: ticket, patch with complexity-gate header, Python
benchmark + correctness test, Makefile, outreach brief. All 16 tests
pass. UNDF IDs: 1276 (playwright), 1277 (selenium-0001), 1288
(selenium-0002), 1289 (webdriverio).
2026-04-22 18:14:39 -04:00

65 lines
1.9 KiB
Python

#!/usr/bin/env python3
# bench-selenium-0001.py
# SessionCapabilitiesMutator: ArrayList.contains inside forEach vs LinkedHashSet dedup.
#
# Models the Grid Node session mutator merging client-requested caps with slot
# stereotype caps. Defect: dedup via list.contains is O(M) per iteration; N
# iterations give O(N*M). Fixed version pre-builds a set for O(1) dedup.
import sys
import time
def bench_defective(n, m):
"""Model forEach(arg -> if !stereotype.contains(arg) stereotype.add(arg))."""
stereotype = [f"--stereo-{i}" for i in range(m)]
incoming = [f"--inc-{i}" for i in range(n)]
t0 = time.perf_counter()
for arg in incoming:
if arg not in stereotype:
stereotype.append(arg)
return time.perf_counter() - t0
def bench_fixed(n, m):
"""Pre-built set for O(1) dedup; list kept for ordering."""
stereotype = [f"--stereo-{i}" for i in range(m)]
incoming = [f"--inc-{i}" for i in range(n)]
t0 = time.perf_counter()
seen = set(stereotype)
for arg in incoming:
if arg not in seen:
seen.add(arg)
stereotype.append(arg)
return time.perf_counter() - t0
TRIALS = 3
SIZES = [10, 50, 100, 500, 1000]
def run():
lines = []
header = "=== selenium-0001: SessionCapabilitiesMutator List.contains vs Set ==="
print(header)
lines.append(header)
for n in SIZES:
m = n # N=M worst case: all incoming are new relative to stereotype
def_times = [bench_defective(n, m) for _ in range(TRIALS)]
fix_times = [bench_fixed(n, m) for _ in range(TRIALS)]
d_ms = min(def_times) * 1000
f_ms = min(fix_times) * 1000
speedup = d_ms / f_ms if f_ms > 0 else float("inf")
line = f"N=M={n:<5}: defective={d_ms:.3f}ms fixed={f_ms:.3f}ms speedup={speedup:.1f}x"
print(line)
lines.append(line)
sys.stdout.flush()
return lines
if __name__ == "__main__":
run()