java-topology/defects/selenium/bench/bench-selenium-0002.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

66 lines
1.9 KiB
Python

#!/usr/bin/env python3
# bench-selenium-0002.py
# ChromiumOptions.mergeInPlace: args/extensions dedup via list.contains inside
# forEach across four merge paths. Fixed version threads all paths through
# addArgumentsUnique(Collection) / addEncodedExtensionsUnique(Collection)
# helpers that build a HashSet view once per merge.
import sys
import time
def bench_defective(n, m, passes=4):
"""Four merge paths each do O(N*M) dedup against growing args list."""
args = [f"--existing-{i}" for i in range(m)]
incoming = [f"--new-{i}" for i in range(n)]
t0 = time.perf_counter()
for _ in range(passes):
for arg in incoming:
if arg not in args:
args.append(arg)
return time.perf_counter() - t0
def bench_fixed(n, m, passes=4):
"""Each merge pass uses addArgumentsUnique: O(N+M) via HashSet view."""
args = [f"--existing-{i}" for i in range(m)]
incoming = [f"--new-{i}" for i in range(n)]
t0 = time.perf_counter()
for _ in range(passes):
seen = set(args)
for arg in incoming:
if arg not in seen:
seen.add(arg)
args.append(arg)
return time.perf_counter() - t0
TRIALS = 3
SIZES = [10, 50, 100, 500, 1000]
def run():
lines = []
header = "=== selenium-0002: ChromiumOptions merge paths List.contains vs HashSet ==="
print(header)
lines.append(header)
for n in SIZES:
m = n
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()