java-topology/defects/webdriverio/bench/bench-webdriverio-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

80 lines
2.4 KiB
Python

#!/usr/bin/env python3
# bench-webdriverio-0001.py
# xpath-conditions extractOrConditions: orMatches.find + values.includes per
# regex match vs Map<attr, Set<values>> dedup.
import sys
import time
def bench_defective(k, v):
"""Array-of-objects: .find(O(K)) + .includes(O(V)) per match."""
or_matches = [] # [{attr, values: [str]}]
t0 = time.perf_counter()
# Simulate M = K*V regex matches, distributed across K distinct attrs
for i in range(k):
attr = f"attr{i}"
for j in range(v):
value_a = f"val{j}"
value_b = f"val{j + 1}"
# find existing entry: O(K)
existing = None
for m in or_matches:
if m["attr"] == attr:
existing = m
break
if existing is None:
or_matches.append({"attr": attr, "values": [value_a, value_b]})
else:
# includes on values list: O(V)
if value_a not in existing["values"]:
existing["values"].append(value_a)
if value_b not in existing["values"]:
existing["values"].append(value_b)
return time.perf_counter() - t0
def bench_fixed(k, v):
"""Map<attr, Set<values>>: O(1) get and Set.add per match."""
or_matches = {} # {attr: set}
t0 = time.perf_counter()
for i in range(k):
attr = f"attr{i}"
for j in range(v):
value_a = f"val{j}"
value_b = f"val{j + 1}"
if attr not in or_matches:
or_matches[attr] = set()
or_matches[attr].add(value_a)
or_matches[attr].add(value_b)
return time.perf_counter() - t0
TRIALS = 3
CASES = [(5, 5), (10, 10), (20, 20), (40, 40), (60, 60)]
def run():
lines = []
header = "=== webdriverio-0001: xpath-conditions Array.find+includes vs Map<attr,Set> ==="
print(header)
lines.append(header)
for k, v in CASES:
def_times = [bench_defective(k, v) for _ in range(TRIALS)]
fix_times = [bench_fixed(k, v) 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"K={k:<3} V={v:<3}: 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()